From 306d7379bb87236a903a6b2bb68ec53eeb2487ea Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 25 Jun 2019 21:04:16 +0200 Subject: [PATCH] Add type safe getters to execution context Issue #718 --- .../batch/item/ExecutionContext.java | 51 ++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) 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 a7b7479e7..8ebe1ce1c 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 @@ -123,7 +123,7 @@ public class ExecutionContext implements Serializable { public void put(String key, @Nullable Object value) { if (value != null) { Object result = this.map.put(key, value); - this.dirty = result == null || result != null && !result.equals(value); + this.dirty = result == null || !result.equals(value); } else { Object result = this.map.remove(key); @@ -148,7 +148,7 @@ public class ExecutionContext implements Serializable { */ public String getString(String key) { - return (String) readAndValidate(key, String.class); + return readAndValidate(key, String.class); } /** @@ -174,7 +174,7 @@ public class ExecutionContext implements Serializable { */ public long getLong(String key) { - return (Long) readAndValidate(key, Long.class); + return readAndValidate(key, Long.class); } /** @@ -200,7 +200,7 @@ public class ExecutionContext implements Serializable { */ public int getInt(String key) { - return (Integer) readAndValidate(key, Integer.class); + return readAndValidate(key, Integer.class); } /** @@ -225,7 +225,7 @@ public class ExecutionContext implements Serializable { * @return The Double value */ public double getDouble(String key) { - return (Double) readAndValidate(key, Double.class); + return readAndValidate(key, Double.class); } /** @@ -255,6 +255,43 @@ public class ExecutionContext implements Serializable { return this.map.get(key); } + /** + * Typesafe getter for the value represented by the provided key, with cast to given class. + * + * @param key The key to get a value for + * @param clazz The class of return type + * @param Type of returned value + * @return The value of given type represented by the given key or {@code null} if the key + * is not present + */ + @Nullable + public V get(String key, Class clazz) { + Object value = this.map.get(key); + if (value == null) { + return null; + } + return get(key, clazz, null); + } + + /** + * Typesafe getter for the value represented by the provided key, with cast to given class. + * + * @param key The key to get a value for + * @param type The class of return type + * @param defaultValue Default value in case element is not present + * @param Type of returned value + * @return The value of given type represented by the given key or {@code null} if the key + * is not present + */ + @Nullable + public V get(String key, Class clazz, @Nullable V defaultValue) { + Object value = this.map.get(key); + if (value == null) { + return defaultValue; + } + return clazz.cast(value); + } + /** * Utility method that attempts to take a value represented by a given key and * validate it as a member of the specified type. @@ -262,7 +299,7 @@ public class ExecutionContext implements Serializable { * @param type Class against which value should be validated * @return Value typed to the specified Class */ - private Object readAndValidate(String key, Class type) { + private V readAndValidate(String key, Class type) { Object value = get(key); @@ -271,7 +308,7 @@ public class ExecutionContext implements Serializable { + (value == null ? null : "(" + value.getClass() + ")" + value) + "]"); } - return value; + return type.cast(value); } /**