1 | /* |
2 | * Copyright 2006-2007 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 | * @since 1.0 |
37 | */ |
38 | @SuppressWarnings("serial") |
39 | public class JobParameters implements Serializable { |
40 | |
41 | private final Map<String,JobParameter> parameters; |
42 | |
43 | public JobParameters() { |
44 | this.parameters = new LinkedHashMap<String, JobParameter>(); |
45 | } |
46 | |
47 | public JobParameters(Map<String,JobParameter> parameters) { |
48 | this.parameters = new LinkedHashMap<String,JobParameter>(parameters); |
49 | } |
50 | |
51 | /** |
52 | * Typesafe Getter for the Long represented by the provided key. |
53 | * |
54 | * @param key The key to get a value for |
55 | * @return The <code>Long</code> value |
56 | */ |
57 | public long getLong(String key){ |
58 | if (!parameters.containsKey(key)) { |
59 | return 0L; |
60 | } |
61 | Object value = parameters.get(key).getValue(); |
62 | return value==null ? 0L : ((Long)value).longValue(); |
63 | } |
64 | |
65 | /** |
66 | * Typesafe Getter for the Long represented by the provided key. If the |
67 | * key does not exist, the default value will be returned. |
68 | * |
69 | * @param key to return the value for |
70 | * @param defaultValue to return if the value doesn't exist |
71 | * @return the parameter represented by the provided key, defaultValue |
72 | * otherwise. |
73 | */ |
74 | public long getLong(String key, long defaultValue){ |
75 | if(parameters.containsKey(key)){ |
76 | return getLong(key); |
77 | } |
78 | else{ |
79 | return defaultValue; |
80 | } |
81 | } |
82 | |
83 | /** |
84 | * Typesafe Getter for the String represented by the provided key. |
85 | * |
86 | * @param key The key to get a value for |
87 | * @return The <code>String</code> value |
88 | */ |
89 | public String getString(String key){ |
90 | JobParameter value = parameters.get(key); |
91 | return value==null ? null : value.toString(); |
92 | } |
93 | |
94 | /** |
95 | * Typesafe Getter for the String represented by the provided key. If the |
96 | * key does not exist, the default value will be returned. |
97 | * |
98 | * @param key to return the value for |
99 | * @param defaultValue to return if the value doesn't exist |
100 | * @return the parameter represented by the provided key, defaultValue |
101 | * otherwise. |
102 | */ |
103 | public String getString(String key, String defaultValue){ |
104 | if(parameters.containsKey(key)){ |
105 | return getString(key); |
106 | } |
107 | else{ |
108 | return defaultValue; |
109 | } |
110 | } |
111 | |
112 | /** |
113 | * Typesafe Getter for the Long represented by the provided key. |
114 | * |
115 | * @param key The key to get a value for |
116 | * @return The <code>Double</code> value |
117 | */ |
118 | public double getDouble(String key){ |
119 | if (!parameters.containsKey(key)) { |
120 | return 0L; |
121 | } |
122 | Double value = (Double)parameters.get(key).getValue(); |
123 | return value==null ? 0.0 : value.doubleValue(); |
124 | } |
125 | |
126 | /** |
127 | * Typesafe Getter for the Double represented by the provided key. If the |
128 | * key does not exist, the default value will be returned. |
129 | * |
130 | * @param key to return the value for |
131 | * @param defaultValue to return if the value doesn't exist |
132 | * @return the parameter represented by the provided key, defaultValue |
133 | * otherwise. |
134 | */ |
135 | public double getDouble(String key, double defaultValue){ |
136 | if(parameters.containsKey(key)){ |
137 | return getDouble(key); |
138 | } |
139 | else{ |
140 | return defaultValue; |
141 | } |
142 | } |
143 | |
144 | /** |
145 | * Typesafe Getter for the Date represented by the provided key. |
146 | * |
147 | * @param key The key to get a value for |
148 | * @return The <code>java.util.Date</code> value |
149 | */ |
150 | public Date getDate(String key){ |
151 | return this.getDate(key,null); |
152 | } |
153 | |
154 | /** |
155 | * Typesafe Getter for the Date represented by the provided key. If the |
156 | * key does not exist, the default value will be returned. |
157 | * |
158 | * @param key to return the value for |
159 | * @param defaultValue to return if the value doesn't exist |
160 | * @return the parameter represented by the provided key, defaultValue |
161 | * otherwise. |
162 | */ |
163 | public Date getDate(String key, Date defaultValue){ |
164 | if(parameters.containsKey(key)){ |
165 | return (Date)parameters.get(key).getValue(); |
166 | } |
167 | else{ |
168 | return defaultValue; |
169 | } |
170 | } |
171 | |
172 | /** |
173 | * Get a map of all parameters, including string, long, and date. |
174 | * |
175 | * @return an unmodifiable map containing all parameters. |
176 | */ |
177 | public Map<String, JobParameter> getParameters(){ |
178 | return new LinkedHashMap<String, JobParameter>(parameters); |
179 | } |
180 | |
181 | /** |
182 | * @return true if the parameters is empty, false otherwise. |
183 | */ |
184 | public boolean isEmpty(){ |
185 | return parameters.isEmpty(); |
186 | } |
187 | |
188 | @Override |
189 | public boolean equals(Object obj) { |
190 | if(obj instanceof JobParameters == false){ |
191 | return false; |
192 | } |
193 | |
194 | if(obj == this){ |
195 | return true; |
196 | } |
197 | |
198 | JobParameters rhs = (JobParameters)obj; |
199 | return this.parameters.equals(rhs.parameters); |
200 | } |
201 | |
202 | @Override |
203 | public int hashCode() { |
204 | return 17 + 23 * parameters.hashCode(); |
205 | } |
206 | |
207 | @Override |
208 | public String toString() { |
209 | return parameters.toString(); |
210 | } |
211 | } |