RESOLVED - issue BATCH-287: add double job parameter type
http://jira.springframework.org/browse/BATCH-287
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ public class JobParametersTests extends TestCase {
|
||||
Map longMap;
|
||||
|
||||
Map dateMap;
|
||||
|
||||
Map doubleMap;
|
||||
|
||||
Date date1 = new Date(4321431242L);
|
||||
|
||||
@@ -43,12 +45,16 @@ public class JobParametersTests extends TestCase {
|
||||
longMap = new HashMap();
|
||||
longMap.put("long.key1", new Long(1));
|
||||
longMap.put("long.key2", new Long(2));
|
||||
|
||||
doubleMap = new HashMap();
|
||||
doubleMap.put("double.key1", new Double(1.1));
|
||||
doubleMap.put("double.key2", new Double(2.2));
|
||||
|
||||
dateMap = new HashMap();
|
||||
dateMap.put("date.key1", date1);
|
||||
dateMap.put("date.key2", date2);
|
||||
|
||||
return new JobParameters(stringMap, longMap, dateMap);
|
||||
return new JobParameters(stringMap, longMap, doubleMap, dateMap);
|
||||
}
|
||||
|
||||
public void testBadLongKeyException() throws Exception {
|
||||
@@ -57,7 +63,7 @@ public class JobParametersTests extends TestCase {
|
||||
badLongMap.put(new Long(0), new Long(1));
|
||||
|
||||
try {
|
||||
new JobParameters(stringMap, badLongMap, dateMap);
|
||||
new JobParameters(stringMap, badLongMap, doubleMap, dateMap);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
@@ -71,7 +77,21 @@ public class JobParametersTests extends TestCase {
|
||||
badLongMap.put("key", "bad long");
|
||||
|
||||
try {
|
||||
new JobParameters(stringMap, badLongMap, dateMap);
|
||||
new JobParameters(stringMap, badLongMap, doubleMap, dateMap);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testBadDoubleConstructorException() throws Exception {
|
||||
|
||||
Map badDoubleMap = new HashMap();
|
||||
badDoubleMap.put("key", "bad double");
|
||||
|
||||
try {
|
||||
new JobParameters(stringMap, longMap, badDoubleMap, dateMap);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
@@ -85,7 +105,7 @@ public class JobParametersTests extends TestCase {
|
||||
badMap.put("key", new Integer(2));
|
||||
|
||||
try {
|
||||
new JobParameters(badMap, longMap, dateMap);
|
||||
new JobParameters(badMap, longMap, doubleMap, dateMap);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
@@ -99,7 +119,7 @@ public class JobParametersTests extends TestCase {
|
||||
badMap.put("key", new java.sql.Date(System.currentTimeMillis()));
|
||||
|
||||
try {
|
||||
new JobParameters(stringMap, longMap, badMap);
|
||||
new JobParameters(stringMap, longMap, doubleMap, badMap);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
@@ -126,6 +146,16 @@ public class JobParametersTests extends TestCase {
|
||||
assertEquals(new Long(1), parameters.getLongParameters().get("long.key1"));
|
||||
assertEquals(new Long(2), parameters.getLongParameters().get("long.key2"));
|
||||
}
|
||||
|
||||
public void testGetDouble() {
|
||||
assertEquals(new Double(1.1), parameters.getDouble("double.key1"));
|
||||
assertEquals(new Double(2.2), parameters.getDouble("double.key2"));
|
||||
}
|
||||
|
||||
public void testGetDoubleParameters() {
|
||||
assertEquals(new Double(1.1), parameters.getDoubleParameters().get("double.key1"));
|
||||
assertEquals(new Double(2.2), parameters.getDoubleParameters().get("double.key2"));
|
||||
}
|
||||
|
||||
public void testGetDate() {
|
||||
assertEquals(date1, parameters.getDate("date.key1"));
|
||||
@@ -184,12 +214,16 @@ public class JobParametersTests extends TestCase {
|
||||
longMap = new HashMap();
|
||||
longMap.put("long.key2", new Long(2));
|
||||
longMap.put("long.key1", new Long(1));
|
||||
|
||||
doubleMap = new HashMap();
|
||||
doubleMap.put("double.key2", new Double(2.2));
|
||||
doubleMap.put("double.key1", new Double(1.1));
|
||||
|
||||
dateMap = new HashMap();
|
||||
dateMap.put("date.key2", date2);
|
||||
dateMap.put("date.key1", date1);
|
||||
|
||||
JobParameters testProps = new JobParameters(stringMap, longMap, dateMap);
|
||||
JobParameters testProps = new JobParameters(stringMap, longMap, doubleMap, dateMap);
|
||||
|
||||
props = testProps.getParameters();
|
||||
stringBuilder = new StringBuilder();
|
||||
|
||||
Reference in New Issue
Block a user