Skip to content

Commit 306d737

Browse files
IlyaNerdfmbenhassine
authored andcommitted
Add type safe getters to execution context
Issue #718
1 parent cc233e6 commit 306d737

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void putDouble(String key, double value) {
123123
public void put(String key, @Nullable Object value) {
124124
if (value != null) {
125125
Object result = this.map.put(key, value);
126-
this.dirty = result == null || result != null && !result.equals(value);
126+
this.dirty = result == null || !result.equals(value);
127127
}
128128
else {
129129
Object result = this.map.remove(key);
@@ -148,7 +148,7 @@ public boolean isDirty() {
148148
*/
149149
public String getString(String key) {
150150

151-
return (String) readAndValidate(key, String.class);
151+
return readAndValidate(key, String.class);
152152
}
153153

154154
/**
@@ -174,7 +174,7 @@ public String getString(String key, String defaultString) {
174174
*/
175175
public long getLong(String key) {
176176

177-
return (Long) readAndValidate(key, Long.class);
177+
return readAndValidate(key, Long.class);
178178
}
179179

180180
/**
@@ -200,7 +200,7 @@ public long getLong(String key, long defaultLong) {
200200
*/
201201
public int getInt(String key) {
202202

203-
return (Integer) readAndValidate(key, Integer.class);
203+
return readAndValidate(key, Integer.class);
204204
}
205205

206206
/**
@@ -225,7 +225,7 @@ public int getInt(String key, int defaultInt) {
225225
* @return The <code>Double</code> value
226226
*/
227227
public double getDouble(String key) {
228-
return (Double) readAndValidate(key, Double.class);
228+
return readAndValidate(key, Double.class);
229229
}
230230

231231
/**
@@ -255,14 +255,51 @@ public Object get(String key) {
255255
return this.map.get(key);
256256
}
257257

258+
/**
259+
* Typesafe getter for the value represented by the provided key, with cast to given class.
260+
*
261+
* @param key The key to get a value for
262+
* @param clazz The class of return type
263+
* @param <V> Type of returned value
264+
* @return The value of given type represented by the given key or {@code null} if the key
265+
* is not present
266+
*/
267+
@Nullable
268+
public <V> V get(String key, Class<V> clazz) {
269+
Object value = this.map.get(key);
270+
if (value == null) {
271+
return null;
272+
}
273+
return get(key, clazz, null);
274+
}
275+
276+
/**
277+
* Typesafe getter for the value represented by the provided key, with cast to given class.
278+
*
279+
* @param key The key to get a value for
280+
* @param type The class of return type
281+
* @param defaultValue Default value in case element is not present
282+
* @param <V> Type of returned value
283+
* @return The value of given type represented by the given key or {@code null} if the key
284+
* is not present
285+
*/
286+
@Nullable
287+
public <V> V get(String key, Class<V> clazz, @Nullable V defaultValue) {
288+
Object value = this.map.get(key);
289+
if (value == null) {
290+
return defaultValue;
291+
}
292+
return clazz.cast(value);
293+
}
294+
258295
/**
259296
* Utility method that attempts to take a value represented by a given key and
260297
* validate it as a member of the specified type.
261298
* @param key The key to validate a value for
262299
* @param type Class against which value should be validated
263300
* @return Value typed to the specified <code>Class</code>
264301
*/
265-
private Object readAndValidate(String key, Class<?> type) {
302+
private <V> V readAndValidate(String key, Class<V> type) {
266303

267304
Object value = get(key);
268305

@@ -271,7 +308,7 @@ private Object readAndValidate(String key, Class<?> type) {
271308
+ (value == null ? null : "(" + value.getClass() + ")" + value) + "]");
272309
}
273310

274-
return value;
311+
return type.cast(value);
275312
}
276313

277314
/**

0 commit comments

Comments
 (0)