Add support for Java 8 Date/Time APIs as job parameters

The support to use any type as a job parameter was added
in #3960.

This commit introduces convenience methods where appropriate
to use Java 8 Date/Time APIs as job parameters.

Resolves #1035
This commit is contained in:
Mahmoud Ben Hassine
2022-10-04 22:37:33 +02:00
parent fd9dca3504
commit 6ca1657d1b
2 changed files with 180 additions and 0 deletions

View File

@@ -17,6 +17,9 @@
package org.springframework.batch.core;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
@@ -208,6 +211,111 @@ public class JobParameters implements Serializable {
}
}
/**
* Typesafe getter for the {@link LocalDate} represented by the provided key.
* @param key The key for which to get a value.
* @return the {@link LocalDate} value or {@code null} if the key is absent.
*/
@Nullable
public LocalDate getLocalDate(String key) {
if (!parameters.containsKey(key)) {
return null;
}
JobParameter<?> jobParameter = parameters.get(key);
if (!jobParameter.getType().equals(LocalDate.class)) {
throw new IllegalArgumentException("Key " + key + " is not of type java.time.LocalDate");
}
return (LocalDate) jobParameter.getValue();
}
/**
* Typesafe getter for the {@link LocalDate} represented by the provided key. If the
* key does not exist, the default value is returned.
* @param key The key for which to return the value.
* @param defaultValue The default value to return if the value does not exist.
* @return the parameter represented by the provided key or, if that is missing, the
* default value.
*/
@Nullable
public LocalDate getLocalDate(String key, @Nullable LocalDate defaultValue) {
if (parameters.containsKey(key)) {
return getLocalDate(key);
}
else {
return defaultValue;
}
}
/**
* Typesafe getter for the {@link LocalTime} represented by the provided key.
* @param key The key for which to get a value.
* @return the {@link LocalTime} value or {@code null} if the key is absent.
*/
@Nullable
public LocalTime getLocalTime(String key) {
if (!parameters.containsKey(key)) {
return null;
}
JobParameter<?> jobParameter = parameters.get(key);
if (!jobParameter.getType().equals(LocalTime.class)) {
throw new IllegalArgumentException("Key " + key + " is not of type java.time.LocalTime");
}
return (LocalTime) jobParameter.getValue();
}
/**
* Typesafe getter for the {@link LocalTime} represented by the provided key. If the
* key does not exist, the default value is returned.
* @param key The key for which to return the value.
* @param defaultValue The default value to return if the value does not exist.
* @return the parameter represented by the provided key or, if that is missing, the
* default value.
*/
@Nullable
public LocalTime getLocalTime(String key, @Nullable LocalTime defaultValue) {
if (parameters.containsKey(key)) {
return getLocalTime(key);
}
else {
return defaultValue;
}
}
/**
* Typesafe getter for the {@link LocalDateTime} represented by the provided key.
* @param key The key for which to get a value.
* @return the {@link LocalDateTime} value or {@code null} if the key is absent.
*/
@Nullable
public LocalDateTime getLocalDateTime(String key) {
if (!parameters.containsKey(key)) {
return null;
}
JobParameter<?> jobParameter = parameters.get(key);
if (!jobParameter.getType().equals(LocalDateTime.class)) {
throw new IllegalArgumentException("Key " + key + " is not of type java.time.LocalDateTime");
}
return (LocalDateTime) jobParameter.getValue();
}
/**
* Typesafe getter for the {@link LocalDateTime} represented by the provided key. If
* the key does not exist, the default value is returned.
* @param key The key for which to return the value.
* @param defaultValue The default value to return if the value does not exist.
* @return the parameter represented by the provided key or, if that is missing, the
* default value.
*/
@Nullable
public LocalDateTime getLocalDateTime(String key, @Nullable LocalDateTime defaultValue) {
if (parameters.containsKey(key)) {
return getLocalDateTime(key);
}
else {
return defaultValue;
}
}
@Nullable
public JobParameter<?> getParameter(String key) {
Assert.notNull(key, "key must not be null");

View File

@@ -16,6 +16,9 @@
package org.springframework.batch.core;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
@@ -131,6 +134,75 @@ public class JobParametersBuilder {
return this;
}
/**
* Add a new identifying {@link LocalDate} parameter for the given key.
* @param key The parameter name.
* @param parameter The runtime parameter. Must not be {@code null}.
* @return a reference to this object.
*/
public JobParametersBuilder addLocalDate(String key, @NonNull LocalDate parameter) {
return addLocalDate(key, parameter, true);
}
/**
* Add a new {@link LocalDate} parameter for the given key.
* @param key The parameter name.
* @param parameter The runtime parameter. Must not be {@code null}.
* @param identifying Indicates if the parameter is used as part of identifying a job
* instance
* @return a reference to this object.
*/
public JobParametersBuilder addLocalDate(String key, @NonNull LocalDate parameter, boolean identifying) {
this.parameterMap.put(key, new JobParameter(parameter, LocalDate.class, identifying));
return this;
}
/**
* Add a new identifying {@link LocalTime} parameter for the given key.
* @param key The parameter name.
* @param parameter The runtime parameter. Must not be {@code null}.
* @return a reference to this object.
*/
public JobParametersBuilder addLocalTime(String key, @NonNull LocalTime parameter) {
return addLocalTime(key, parameter, true);
}
/**
* Add a new {@link LocalTime} parameter for the given key.
* @param key The parameter name.
* @param parameter The runtime parameter. Must not be {@code null}.
* @param identifying Indicates if the parameter is used as part of identifying a job
* instance
* @return a reference to this object.
*/
public JobParametersBuilder addLocalTime(String key, @NonNull LocalTime parameter, boolean identifying) {
this.parameterMap.put(key, new JobParameter(parameter, LocalTime.class, identifying));
return this;
}
/**
* Add a new identifying {@link LocalDateTime} parameter for the given key.
* @param key The parameter name.
* @param parameter The runtime parameter. Must not be {@code null}.
* @return a reference to this object.
*/
public JobParametersBuilder addLocalDateTime(String key, @NonNull LocalDateTime parameter) {
return addLocalDateTime(key, parameter, true);
}
/**
* Add a new {@link LocalDateTime} parameter for the given key.
* @param key The parameter name.
* @param parameter The runtime parameter. Must not be {@code null}.
* @param identifying Indicates if the parameter is used as part of identifying a job
* instance
* @return a reference to this object.
*/
public JobParametersBuilder addLocalDateTime(String key, @NonNull LocalDateTime parameter, boolean identifying) {
this.parameterMap.put(key, new JobParameter(parameter, LocalDateTime.class, identifying));
return this;
}
/**
* Add a new identifying {@link Long} parameter for the given key.
* @param key The parameter accessor.