|
17 | 17 | package org.springframework.batch.core;
|
18 | 18 |
|
19 | 19 | import java.io.Serializable;
|
| 20 | +import java.time.LocalDate; |
| 21 | +import java.time.LocalDateTime; |
| 22 | +import java.time.LocalTime; |
20 | 23 | import java.util.ArrayList;
|
21 | 24 | import java.util.Collections;
|
22 | 25 | import java.util.Date;
|
@@ -208,6 +211,111 @@ public Date getDate(String key, @Nullable Date defaultValue) {
|
208 | 211 | }
|
209 | 212 | }
|
210 | 213 |
|
| 214 | + /** |
| 215 | + * Typesafe getter for the {@link LocalDate} represented by the provided key. |
| 216 | + * @param key The key for which to get a value. |
| 217 | + * @return the {@link LocalDate} value or {@code null} if the key is absent. |
| 218 | + */ |
| 219 | + @Nullable |
| 220 | + public LocalDate getLocalDate(String key) { |
| 221 | + if (!parameters.containsKey(key)) { |
| 222 | + return null; |
| 223 | + } |
| 224 | + JobParameter<?> jobParameter = parameters.get(key); |
| 225 | + if (!jobParameter.getType().equals(LocalDate.class)) { |
| 226 | + throw new IllegalArgumentException("Key " + key + " is not of type java.time.LocalDate"); |
| 227 | + } |
| 228 | + return (LocalDate) jobParameter.getValue(); |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Typesafe getter for the {@link LocalDate} represented by the provided key. If the |
| 233 | + * key does not exist, the default value is returned. |
| 234 | + * @param key The key for which to return the value. |
| 235 | + * @param defaultValue The default value to return if the value does not exist. |
| 236 | + * @return the parameter represented by the provided key or, if that is missing, the |
| 237 | + * default value. |
| 238 | + */ |
| 239 | + @Nullable |
| 240 | + public LocalDate getLocalDate(String key, @Nullable LocalDate defaultValue) { |
| 241 | + if (parameters.containsKey(key)) { |
| 242 | + return getLocalDate(key); |
| 243 | + } |
| 244 | + else { |
| 245 | + return defaultValue; |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * Typesafe getter for the {@link LocalTime} represented by the provided key. |
| 251 | + * @param key The key for which to get a value. |
| 252 | + * @return the {@link LocalTime} value or {@code null} if the key is absent. |
| 253 | + */ |
| 254 | + @Nullable |
| 255 | + public LocalTime getLocalTime(String key) { |
| 256 | + if (!parameters.containsKey(key)) { |
| 257 | + return null; |
| 258 | + } |
| 259 | + JobParameter<?> jobParameter = parameters.get(key); |
| 260 | + if (!jobParameter.getType().equals(LocalTime.class)) { |
| 261 | + throw new IllegalArgumentException("Key " + key + " is not of type java.time.LocalTime"); |
| 262 | + } |
| 263 | + return (LocalTime) jobParameter.getValue(); |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * Typesafe getter for the {@link LocalTime} represented by the provided key. If the |
| 268 | + * key does not exist, the default value is returned. |
| 269 | + * @param key The key for which to return the value. |
| 270 | + * @param defaultValue The default value to return if the value does not exist. |
| 271 | + * @return the parameter represented by the provided key or, if that is missing, the |
| 272 | + * default value. |
| 273 | + */ |
| 274 | + @Nullable |
| 275 | + public LocalTime getLocalTime(String key, @Nullable LocalTime defaultValue) { |
| 276 | + if (parameters.containsKey(key)) { |
| 277 | + return getLocalTime(key); |
| 278 | + } |
| 279 | + else { |
| 280 | + return defaultValue; |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + * Typesafe getter for the {@link LocalDateTime} represented by the provided key. |
| 286 | + * @param key The key for which to get a value. |
| 287 | + * @return the {@link LocalDateTime} value or {@code null} if the key is absent. |
| 288 | + */ |
| 289 | + @Nullable |
| 290 | + public LocalDateTime getLocalDateTime(String key) { |
| 291 | + if (!parameters.containsKey(key)) { |
| 292 | + return null; |
| 293 | + } |
| 294 | + JobParameter<?> jobParameter = parameters.get(key); |
| 295 | + if (!jobParameter.getType().equals(LocalDateTime.class)) { |
| 296 | + throw new IllegalArgumentException("Key " + key + " is not of type java.time.LocalDateTime"); |
| 297 | + } |
| 298 | + return (LocalDateTime) jobParameter.getValue(); |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * Typesafe getter for the {@link LocalDateTime} represented by the provided key. If |
| 303 | + * the key does not exist, the default value is returned. |
| 304 | + * @param key The key for which to return the value. |
| 305 | + * @param defaultValue The default value to return if the value does not exist. |
| 306 | + * @return the parameter represented by the provided key or, if that is missing, the |
| 307 | + * default value. |
| 308 | + */ |
| 309 | + @Nullable |
| 310 | + public LocalDateTime getLocalDateTime(String key, @Nullable LocalDateTime defaultValue) { |
| 311 | + if (parameters.containsKey(key)) { |
| 312 | + return getLocalDateTime(key); |
| 313 | + } |
| 314 | + else { |
| 315 | + return defaultValue; |
| 316 | + } |
| 317 | + } |
| 318 | + |
211 | 319 | @Nullable
|
212 | 320 | public JobParameter<?> getParameter(String key) {
|
213 | 321 | Assert.notNull(key, "key must not be null");
|
|
0 commit comments