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 c50d69e3a..661ed65b2 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 @@ -91,7 +91,17 @@ public class ExecutionContext implements Serializable { */ public void putLong(String key, long value) { - put(key, new Long(value)); + put(key, Long.valueOf(value)); + } + + /** + * Adds an Integer value to the context. + * + * @param key Key to add to context + * @param value Value to associate with key + */ + public void putInt(String key, int value) { + put(key, Integer.valueOf(value)); } /** @@ -102,7 +112,7 @@ public class ExecutionContext implements Serializable { */ public void putDouble(String key, double value) { - put(key, new Double(value)); + put(key, Double.valueOf(value)); } /** @@ -190,6 +200,34 @@ public class ExecutionContext implements Serializable { return ((Long) readAndValidate(key, Long.class)).longValue(); } + /** + * Typesafe Getter for the Integer represented by the provided key. + * + * @param key The key to get a value for + * @return The Integer value + */ + public int getInt(String key) { + + return ((Integer) readAndValidate(key, Integer.class)).intValue(); + } + + /** + * Typesafe Getter for the Integer 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 defaultInt Default to return if key is not represented + * @return The int value if key is represented, specified + * default otherwise + */ + public int getInt(String key, int defaultInt) { + if (!map.containsKey(key)) { + return defaultInt; + } + + return ((Integer) readAndValidate(key, Integer.class)).intValue(); + } + /** * Typesafe Getter for the Double represented by the provided key. * diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemWriter.java index f752f6791..8c093996b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemWriter.java @@ -101,9 +101,9 @@ public class MultiResourceItemWriter extends ExecutionContextUserSupport impl } public void open(ExecutionContext executionContext) throws ItemStreamException { - resourceIndex = Long.valueOf(executionContext.getLong(getKey(RESOURCE_INDEX_KEY), 1L)).intValue(); - currentResourceItemCount = Long.valueOf(executionContext.getLong(getKey(CURRENT_RESOURCE_ITEM_COUNT), 0L)) - .intValue(); + resourceIndex = executionContext.getInt(getKey(RESOURCE_INDEX_KEY), 1); + currentResourceItemCount = executionContext.getInt(getKey(CURRENT_RESOURCE_ITEM_COUNT), 0); + try { pointDelegateToNextResource(); } @@ -115,8 +115,8 @@ public class MultiResourceItemWriter extends ExecutionContextUserSupport impl public void update(ExecutionContext executionContext) throws ItemStreamException { delegate.update(executionContext); - executionContext.put(getKey(CURRENT_RESOURCE_ITEM_COUNT), Long.valueOf(currentResourceItemCount)); - executionContext.put(getKey(RESOURCE_INDEX_KEY), Long.valueOf(resourceIndex)); + executionContext.putInt(getKey(CURRENT_RESOURCE_ITEM_COUNT), currentResourceItemCount); + executionContext.putInt(getKey(RESOURCE_INDEX_KEY), resourceIndex); } /** 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 12fe0759d..6b71beed2 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 @@ -45,14 +45,17 @@ public class ExecutionContextTests { context.putString("2", "testString2"); context.putLong("3", 3); context.putDouble("4", 4.4); + context.putInt("5", 5); assertEquals("testString1", context.getString("1")); assertEquals("testString2", context.getString("2")); - assertEquals("defaultString", context.getString("5", "defaultString")); + assertEquals("defaultString", context.getString("55", "defaultString")); assertEquals(4.4, context.getDouble("4"), 0); - assertEquals(5.5, context.getDouble("5", 5.5), 0); + assertEquals(5.5, context.getDouble("55", 5.5), 0); assertEquals(3, context.getLong("3")); - assertEquals(5, context.getLong("5", 5)); + assertEquals(5, context.getLong("55", 5)); + assertEquals(5, context.getInt("5")); + assertEquals(6, context.getInt("55", 6)); } @Test @@ -138,6 +141,7 @@ public class ExecutionContextTests { context.putLong("3", 3); context.putDouble("4", 4.4); context.put("5", s); + context.putInt("6", 6); byte[] serialized = SerializationUtils.serialize(context);