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