1 | /** |
2 | * |
3 | */ |
4 | package org.springframework.batch.core; |
5 | |
6 | import java.io.Serializable; |
7 | import java.util.Collections; |
8 | import java.util.Date; |
9 | import java.util.Iterator; |
10 | import java.util.LinkedHashMap; |
11 | import java.util.Map; |
12 | import java.util.Map.Entry; |
13 | |
14 | import org.apache.commons.lang.builder.HashCodeBuilder; |
15 | |
16 | /** |
17 | * Value object representing runtime parameters to a batch job. Because the |
18 | * parameters have no individual meaning outside of the JobParameters they are |
19 | * contained within, it is a value object rather than an entity. It is also |
20 | * extremely important that a parameters object can be reliably compared to |
21 | * another for equality, in order to determine if one JobParameters object |
22 | * equals another. Furthermore, because these parameters will need to be |
23 | * persisted, it is vital that the types added are restricted. |
24 | * |
25 | * This class is immutable and therefore thread-safe. |
26 | * |
27 | * @author Lucas Ward |
28 | * @since 1.0 |
29 | */ |
30 | public class JobParameters implements Serializable { |
31 | |
32 | private final Map stringMap; |
33 | |
34 | private final Map longMap; |
35 | |
36 | private final Map doubleMap; |
37 | |
38 | private final Map dateMap; |
39 | |
40 | /** |
41 | * Default constructor. Creates a new empty JobRuntimeParameters. It should |
42 | * be noted that this constructor should only be used if an empty parameters |
43 | * is needed, since JobRuntimeParameters is immutable. |
44 | */ |
45 | public JobParameters() { |
46 | this.stringMap = new LinkedHashMap(); |
47 | this.longMap = new LinkedHashMap(); |
48 | this.doubleMap = new LinkedHashMap(); |
49 | this.dateMap = new LinkedHashMap(); |
50 | } |
51 | |
52 | /** |
53 | * Create a new parameters object based upon the maps for each of the |
54 | * supported data types. See {@link JobParametersBuilder} for an easier way |
55 | * to create parameters. |
56 | */ |
57 | public JobParameters(Map stringMap, Map longMap, Map doubleMap, Map dateMap) { |
58 | super(); |
59 | |
60 | validateMap(stringMap, String.class); |
61 | validateMap(longMap, Long.class); |
62 | validateMap(doubleMap, Double.class); |
63 | validateMap(dateMap, Date.class); |
64 | this.stringMap = new LinkedHashMap(stringMap); |
65 | this.longMap = new LinkedHashMap(longMap); |
66 | this.doubleMap = new LinkedHashMap(doubleMap); |
67 | this.dateMap = copyDateMap(dateMap); |
68 | } |
69 | |
70 | /** |
71 | * Typesafe Getter for the String represented by the provided key. |
72 | * |
73 | * @param key The key to get a value for |
74 | * @return The <code>String</code> value |
75 | */ |
76 | public String getString(String key) { |
77 | return (String) stringMap.get(key); |
78 | } |
79 | |
80 | /** |
81 | * Typesafe Getter for the Long represented by the provided key. |
82 | * |
83 | * @param key The key to get a value for |
84 | * @return The <code>Long</code> value |
85 | */ |
86 | public Long getLong(String key) { |
87 | return (Long) longMap.get(key); |
88 | } |
89 | |
90 | /** |
91 | * Typesafe Getter for the Long represented by the provided key. |
92 | * |
93 | * @param key The key to get a value for |
94 | * @return The <code>Double</code> value |
95 | */ |
96 | public Double getDouble(String key) { |
97 | return (Double) doubleMap.get(key); |
98 | } |
99 | |
100 | /** |
101 | * Typesafe Getter for the Date represented by the provided key. |
102 | * |
103 | * @param key The key to get a value for |
104 | * @return The <code>java.util.Date</code> value |
105 | */ |
106 | public Date getDate(String key) { |
107 | return (Date) dateMap.get(key); |
108 | } |
109 | |
110 | /** |
111 | * Get a map of all parameters, including string, long, and date. It should |
112 | * be noted that a Collections$UnmodifiableMap is returned, ensuring |
113 | * immutability. |
114 | * |
115 | * @return an unmodifiable map containing all parameters. |
116 | */ |
117 | public Map getParameters() { |
118 | Map tempMap = new LinkedHashMap(stringMap); |
119 | tempMap.putAll(longMap); |
120 | tempMap.putAll(doubleMap); |
121 | tempMap.putAll(dateMap); |
122 | return Collections.unmodifiableMap(tempMap); |
123 | } |
124 | |
125 | /** |
126 | * Get a map of only string parameters. |
127 | * |
128 | * @return String parameters. |
129 | */ |
130 | public Map getStringParameters() { |
131 | return Collections.unmodifiableMap(stringMap); |
132 | } |
133 | |
134 | /** |
135 | * Get a map of only Long parameters |
136 | * |
137 | * @return long parameters. |
138 | */ |
139 | public Map getLongParameters() { |
140 | return Collections.unmodifiableMap(longMap); |
141 | } |
142 | |
143 | /** |
144 | * Get a map of only Double parameters |
145 | * |
146 | * @return long parameters. |
147 | */ |
148 | public Map getDoubleParameters() { |
149 | return Collections.unmodifiableMap(doubleMap); |
150 | } |
151 | |
152 | /** |
153 | * Get a map of only Date parameters |
154 | * |
155 | * @return date parameters. |
156 | */ |
157 | public Map getDateParameters() { |
158 | return Collections.unmodifiableMap(dateMap); |
159 | } |
160 | |
161 | /** |
162 | * @return true if the prameters is empty, false otherwise. |
163 | */ |
164 | public boolean isEmpty() { |
165 | return (dateMap.isEmpty() && longMap.isEmpty() && doubleMap.isEmpty() && stringMap.isEmpty()); |
166 | } |
167 | |
168 | /* |
169 | * Convenience method for validating that a the provided map only contains a |
170 | * particular type as a value, with only a String as a key. |
171 | */ |
172 | private void validateMap(Map map, Class type) { |
173 | |
174 | for (Iterator it = map.entrySet().iterator(); it.hasNext();) { |
175 | |
176 | Entry entry = (Entry) it.next(); |
177 | if (entry.getKey() instanceof String == false) { |
178 | throw new IllegalArgumentException("All parameter keys must be strings."); |
179 | } |
180 | if (entry.getValue().getClass() != type) { |
181 | throw new IllegalArgumentException("The values in this map must be of type:[" + type + "]."); |
182 | } |
183 | } |
184 | } |
185 | |
186 | /* |
187 | * Convenience method for copying Date values to ensure immutability. |
188 | */ |
189 | private Map copyDateMap(Map dateMap) { |
190 | Map tempMap = new LinkedHashMap(); |
191 | |
192 | for (Iterator it = dateMap.entrySet().iterator(); it.hasNext();) { |
193 | Entry entry = (Entry) it.next(); |
194 | Date date = (Date) entry.getValue(); |
195 | tempMap.put(entry.getKey(), new Date(date.getTime())); |
196 | } |
197 | |
198 | return tempMap; |
199 | } |
200 | |
201 | public boolean equals(Object obj) { |
202 | |
203 | if (obj instanceof JobParameters == false) { |
204 | return false; |
205 | } |
206 | |
207 | if (this == obj) { |
208 | return true; |
209 | } |
210 | |
211 | JobParameters parameters = (JobParameters) obj; |
212 | |
213 | // Since the type contained by each map is known, it's safe to call |
214 | // Map.equals() |
215 | if (getParameters().equals(parameters.getParameters())) { |
216 | return true; |
217 | } |
218 | else { |
219 | return false; |
220 | } |
221 | } |
222 | |
223 | public int hashCode() { |
224 | return new HashCodeBuilder(7, 21).append(stringMap).append(longMap).append(doubleMap).append(dateMap) |
225 | .toHashCode(); |
226 | } |
227 | |
228 | public String toString() { |
229 | |
230 | return stringMap.toString() + longMap.toString() + doubleMap.toString() + dateMap.toString(); |
231 | } |
232 | } |