1 | /* |
2 | * Copyright 2006-2013 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | package org.springframework.batch.core; |
18 | |
19 | import java.io.Serializable; |
20 | import java.util.Date; |
21 | import java.util.LinkedHashMap; |
22 | import java.util.Map; |
23 | |
24 | /** |
25 | * Value object representing runtime parameters to a batch job. Because the |
26 | * parameters have no individual meaning outside of the JobParameters they are |
27 | * contained within, it is a value object rather than an entity. It is also |
28 | * extremely important that a parameters object can be reliably compared to |
29 | * another for equality, in order to determine if one JobParameters object |
30 | * equals another. Furthermore, because these parameters will need to be |
31 | * persisted, it is vital that the types added are restricted. |
32 | * |
33 | * This class is immutable and therefore thread-safe. |
34 | * |
35 | * @author Lucas Ward |
36 | * @author Michael Minella |
37 | * @since 1.0 |
38 | */ |
39 | @SuppressWarnings("serial") |
40 | public class JobParameters implements Serializable { |
41 | |
42 | private final Map<String,JobParameter> parameters; |
43 | |
44 | public JobParameters() { |
45 | this.parameters = new LinkedHashMap<String, JobParameter>(); |
46 | } |
47 | |
48 | public JobParameters(Map<String,JobParameter> parameters) { |
49 | this.parameters = new LinkedHashMap<String,JobParameter>(parameters); |
50 | } |
51 | |
52 | /** |
53 | * Typesafe Getter for the Long represented by the provided key. |
54 | * |
55 | * @param key The key to get a value for |
56 | * @return The <code>Long</code> value |
57 | */ |
58 | public Long getLong(String key){ |
59 | if (!parameters.containsKey(key)) { |
60 | return 0L; |
61 | } |
62 | Object value = parameters.get(key).getValue(); |
63 | return value==null ? 0L : ((Long)value).longValue(); |
64 | } |
65 | |
66 | /** |
67 | * Typesafe Getter for the Long represented by the provided key. If the |
68 | * key does not exist, the default value will be returned. |
69 | * |
70 | * @param key to return the value for |
71 | * @param defaultValue to return if the value doesn't exist |
72 | * @return the parameter represented by the provided key, defaultValue |
73 | * otherwise. |
74 | */ |
75 | public Long getLong(String key, long defaultValue){ |
76 | if(parameters.containsKey(key)){ |
77 | return getLong(key); |
78 | } |
79 | else{ |
80 | return defaultValue; |
81 | } |
82 | } |
83 | |
84 | /** |
85 | * Typesafe Getter for the String represented by the provided key. |
86 | * |
87 | * @param key The key to get a value for |
88 | * @return The <code>String</code> value |
89 | */ |
90 | public String getString(String key){ |
91 | JobParameter value = parameters.get(key); |
92 | return value==null ? null : value.toString(); |
93 | } |
94 | |
95 | /** |
96 | * Typesafe Getter for the String represented by the provided key. If the |
97 | * key does not exist, the default value will be returned. |
98 | * |
99 | * @param key to return the value for |
100 | * @param defaultValue to return if the value doesn't exist |
101 | * @return the parameter represented by the provided key, defaultValue |
102 | * otherwise. |
103 | */ |
104 | public String getString(String key, String defaultValue){ |
105 | if(parameters.containsKey(key)){ |
106 | return getString(key); |
107 | } |
108 | else{ |
109 | return defaultValue; |
110 | } |
111 | } |
112 | |
113 | /** |
114 | * Typesafe Getter for the Long represented by the provided key. |
115 | * |
116 | * @param key The key to get a value for |
117 | * @return The <code>Double</code> value |
118 | */ |
119 | public Double getDouble(String key){ |
120 | if (!parameters.containsKey(key)) { |
121 | return 0.0; |
122 | } |
123 | Double value = (Double)parameters.get(key).getValue(); |
124 | return value==null ? 0.0 : value.doubleValue(); |
125 | } |
126 | |
127 | /** |
128 | * Typesafe Getter for the Double represented by the provided key. If the |
129 | * key does not exist, the default value will be returned. |
130 | * |
131 | * @param key to return the value for |
132 | * @param defaultValue to return if the value doesn't exist |
133 | * @return the parameter represented by the provided key, defaultValue |
134 | * otherwise. |
135 | */ |
136 | public Double getDouble(String key, double defaultValue){ |
137 | if(parameters.containsKey(key)){ |
138 | return getDouble(key); |
139 | } |
140 | else{ |
141 | return defaultValue; |
142 | } |
143 | } |
144 | |
145 | /** |
146 | * Typesafe Getter for the Date represented by the provided key. |
147 | * |
148 | * @param key The key to get a value for |
149 | * @return The <code>java.util.Date</code> value |
150 | */ |
151 | public Date getDate(String key){ |
152 | return this.getDate(key,null); |
153 | } |
154 | |
155 | /** |
156 | * Typesafe Getter for the Date represented by the provided key. If the |
157 | * key does not exist, the default value will be returned. |
158 | * |
159 | * @param key to return the value for |
160 | * @param defaultValue to return if the value doesn't exist |
161 | * @return the parameter represented by the provided key, defaultValue |
162 | * otherwise. |
163 | */ |
164 | public Date getDate(String key, Date defaultValue){ |
165 | if(parameters.containsKey(key)){ |
166 | return (Date)parameters.get(key).getValue(); |
167 | } |
168 | else{ |
169 | return defaultValue; |
170 | } |
171 | } |
172 | |
173 | /** |
174 | * Get a map of all parameters, including string, long, and date. |
175 | * |
176 | * @return an unmodifiable map containing all parameters. |
177 | */ |
178 | public Map<String, JobParameter> getParameters(){ |
179 | return new LinkedHashMap<String, JobParameter>(parameters); |
180 | } |
181 | |
182 | /** |
183 | * @return true if the parameters is empty, false otherwise. |
184 | */ |
185 | public boolean isEmpty(){ |
186 | return parameters.isEmpty(); |
187 | } |
188 | |
189 | @Override |
190 | public boolean equals(Object obj) { |
191 | if(obj instanceof JobParameters == false){ |
192 | return false; |
193 | } |
194 | |
195 | if(obj == this){ |
196 | return true; |
197 | } |
198 | |
199 | JobParameters rhs = (JobParameters)obj; |
200 | return this.parameters.equals(rhs.parameters); |
201 | } |
202 | |
203 | @Override |
204 | public int hashCode() { |
205 | return 17 + 23 * parameters.hashCode(); |
206 | } |
207 | |
208 | @Override |
209 | public String toString() { |
210 | return parameters.toString(); |
211 | } |
212 | } |