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