diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java index 721dbd958..6bd0609e2 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java @@ -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 String 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 long 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 double 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. diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java index 543270981..81154c6d9 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java @@ -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(){