BATCH-508: Added default values to ExecutionContext

This commit is contained in:
lucasward
2008-04-11 20:26:20 +00:00
parent eb518799cf
commit f162c1bd21
2 changed files with 54 additions and 0 deletions

View File

@@ -127,6 +127,23 @@ public class ExecutionContext {
return (String) readAndValidate(key, String.class);
}
/**
* Typesafe Getter for the String represented by the provided key
* with default value to return if key is not represented.
*
* @param key The key to get a value for
* @param defaultString Default to return if key is not represented
* @return The <code>String</code> value if key is repreesnted,
* specified default otherwise
*/
public String getString(String key, String defaultString) {
if(!map.containsKey(key)) {
return defaultString;
}
return (String) readAndValidate(key, String.class);
}
/**
* Typesafe Getter for the Long represented by the provided key.
@@ -138,6 +155,23 @@ public class ExecutionContext {
return ((Long) readAndValidate(key, Long.class)).longValue();
}
/**
* Typesafe Getter for the Long represented by the provided key
* with default value to return if key is not represented.
*
* @param key The key to get a value for
* @param defaultLong Default to return if key is not represented
* @return The <code>long</code> value if key is represented,
* specified default otherwise
*/
public long getLong(String key, long defaultLong) {
if(!map.containsKey(key)) {
return defaultLong;
}
return ((Long) readAndValidate(key, Long.class)).longValue();
}
/**
* Typesafe Getter for the Double represented by the provided key.
@@ -148,6 +182,23 @@ public class ExecutionContext {
public double getDouble(String key) {
return ((Double) readAndValidate(key, Double.class)).doubleValue();
}
/**
* Typesafe Getter for the Double represented by the provided key
* with default value to return if key is not represented.
*
* @param key The key to get a value for
* @param defaultDouble Default to return if key is not represented
* @return The <code>double</code> value if key is represented,
* specified default otherwise
*/
public double getDouble(String key, double defaultDouble) {
if(!map.containsKey(key)) {
return defaultDouble;
}
return ((Double) readAndValidate(key, Double.class)).doubleValue();
}
/**
* Getter for the value represented by the provided key.

View File

@@ -40,8 +40,11 @@ public class ExecutionContextTests extends TestCase{
assertEquals("testString1", context.getString("1"));
assertEquals("testString2", context.getString("2"));
assertEquals("defaultString", context.getString("5", "defaultString"));
assertEquals(4.4, context.getDouble("4"), 0);
assertEquals(5.5, context.getDouble("5", 5.5), 0);
assertEquals(3, context.getLong("3"));
assertEquals(5, context.getLong("5", 5));
}
public void testInvalidCast(){