RESOLVED - issue BATCH-287: add double job parameter type

http://jira.springframework.org/browse/BATCH-287
This commit is contained in:
robokaso
2008-02-27 11:19:45 +00:00
parent ffcbb68f2a
commit 2f3cf7bec0
14 changed files with 157 additions and 51 deletions

View File

@@ -13,11 +13,13 @@ import java.util.Map.Entry;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
* Value object representing runtime parameters to a batch job. Because the parameters have no individual meaning
* outside of the JobIdentifier they are contained within, it is a value object rather than an entity. It is also
* extremely important that a parameters object can be reliably compared to another for equality, in order to determine
* if one JobIdentifier equals another. Furthermore, because these parameters will need to be persisted, it is vital
* that the types added are restricted.
* Value object representing runtime parameters to a batch job. Because the
* parameters have no individual meaning outside of the JobParameters they are
* contained within, it is a value object rather than an entity. It is also
* extremely important that a parameters object can be reliably compared to
* another for equality, in order to determine if one JobParameters object
* equals another. Furthermore, because these parameters will need to be
* persisted, it is vital that the types added are restricted.
*
* @author Lucas Ward
* @since 1.0
@@ -28,34 +30,37 @@ public class JobParameters {
private final Map longMap;
private final Map doubleMap;
private final Map dateMap;
/**
* Default constructor. Creates a new empty JobRuntimeParameters. It should be noted that this constructor should
* only be used if an empty parameters is needed, since JobRuntimeParameters is immutable.
* Default constructor. Creates a new empty JobRuntimeParameters. It should
* be noted that this constructor should only be used if an empty parameters
* is needed, since JobRuntimeParameters is immutable.
*/
public JobParameters() {
this.stringMap = new LinkedHashMap();
this.longMap = new LinkedHashMap();
this.doubleMap = new LinkedHashMap();
this.dateMap = new LinkedHashMap();
}
/**
* Create a new parameters object based upon three maps for each of the three data types. See
* {@link JobParametersBuilder} for an easier way to create parameters.
*
* @param stringMap
* @param longMap
* @param dateMap
* Create a new parameters object based upon the maps for each of the
* supported data types. See {@link JobParametersBuilder} for an easier way
* to create parameters.
*/
public JobParameters(Map stringMap, Map longMap, Map dateMap) {
public JobParameters(Map stringMap, Map longMap, Map doubleMap, Map dateMap) {
super();
validateMap(stringMap, String.class);
validateMap(longMap, Long.class);
validateMap(doubleMap, Double.class);
validateMap(dateMap, Date.class);
this.stringMap = new LinkedHashMap(stringMap);
this.longMap = new LinkedHashMap(longMap);
this.doubleMap = new LinkedHashMap(doubleMap);
this.dateMap = copyDateMap(dateMap);
}
@@ -79,6 +84,16 @@ public class JobParameters {
return (Long) longMap.get(key);
}
/**
* Typesafe Getter for the Long represented by the provided key.
*
* @param key The key to get a value for
* @return The <code>Double</code> value
*/
public Double getDouble(String key) {
return (Double) doubleMap.get(key);
}
/**
* Typesafe Getter for the Date represented by the provided key.
*
@@ -90,14 +105,16 @@ public class JobParameters {
}
/**
* Get a map of all parameters, including string, long, and date. It should be noted that a
* Collections$UnmodifiableMap is returned, ensuring immutability.
* Get a map of all parameters, including string, long, and date. It should
* be noted that a Collections$UnmodifiableMap is returned, ensuring
* immutability.
*
* @return an unmodifiable map containing all parameters.
*/
public Map getParameters() {
Map tempMap = new LinkedHashMap(stringMap);
tempMap.putAll(longMap);
tempMap.putAll(doubleMap);
tempMap.putAll(dateMap);
return Collections.unmodifiableMap(tempMap);
}
@@ -120,6 +137,15 @@ public class JobParameters {
return Collections.unmodifiableMap(longMap);
}
/**
* Get a map of only Double parameters
*
* @return long parameters.
*/
public Map getDoubleParameters() {
return Collections.unmodifiableMap(doubleMap);
}
/**
* Get a map of only Date parameters
*
@@ -133,12 +159,12 @@ public class JobParameters {
* @return true if the prameters is empty, false otherwise.
*/
public boolean isEmpty() {
return (dateMap.isEmpty() && longMap.isEmpty() && stringMap.isEmpty());
return (dateMap.isEmpty() && longMap.isEmpty() && doubleMap.isEmpty() && stringMap.isEmpty());
}
/*
* Convenience method for validating that a the provided map only contains a particular type as a value, with only a
* String as a key.
* Convenience method for validating that a the provided map only contains a
* particular type as a value, with only a String as a key.
*/
private void validateMap(Map map, Class type) {
@@ -181,20 +207,23 @@ public class JobParameters {
JobParameters parameters = (JobParameters) obj;
// Since the type contained by each map is known, it's safe to call Map.equals()
// Since the type contained by each map is known, it's safe to call
// Map.equals()
if (getParameters().equals(parameters.getParameters())) {
return true;
} else {
}
else {
return false;
}
}
public int hashCode() {
return new HashCodeBuilder(7, 21).append(stringMap).append(longMap).append(dateMap).toHashCode();
return new HashCodeBuilder(7, 21).append(stringMap).append(longMap).append(doubleMap).append(dateMap)
.toHashCode();
}
public String toString() {
return stringMap.toString() + longMap.toString() + dateMap.toString();
return stringMap.toString() + longMap.toString() + doubleMap.toString() + dateMap.toString();
}
}

View File

@@ -26,6 +26,8 @@ public class JobParametersBuilder {
private final Map stringMap;
private final Map longMap;
private final Map doubleMap;
private final Map dateMap;
@@ -36,6 +38,7 @@ public class JobParametersBuilder {
this.stringMap = new LinkedHashMap();
this.longMap = new LinkedHashMap();
this.doubleMap = new LinkedHashMap();
this.dateMap = new LinkedHashMap();
}
@@ -70,13 +73,26 @@ public class JobParametersBuilder {
*
* @param key - parameter accessor.
* @param parameter - runtime parameter
* @return a refernece to this object.
* @return a reference to this object.
*/
public JobParametersBuilder addLong(String key, Long parameter) {
Assert.notNull(parameter, "Parameter must not be null.");
longMap.put(key, parameter);
return this;
}
/**
* Add a new Double parameter for the given key.
*
* @param key - parameter accessor.
* @param parameter - runtime parameter
* @return a reference to this object.
*/
public JobParametersBuilder addDouble(String key, Double parameter) {
Assert.notNull(parameter, "Parameter must not be null.");
doubleMap.put(key, parameter);
return this;
}
/**
* Conversion method that takes the current state of this builder and
@@ -85,6 +101,6 @@ public class JobParametersBuilder {
* @return a valid JobRuntimeParameters object.
*/
public JobParameters toJobParameters() {
return new JobParameters(stringMap, longMap, dateMap);
return new JobParameters(stringMap, longMap, doubleMap, dateMap);
}
}