Skip to content

Commit 54239fe

Browse files
committed
Refine contribution #718
- Rename variables - Update javadoc - Add type check before cast - Update code formatting
1 parent 306d737 commit 54239fe

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,40 +256,44 @@ public Object get(String key) {
256256
}
257257

258258
/**
259-
* Typesafe getter for the value represented by the provided key, with cast to given class.
260-
*
259+
* Typesafe getter for the value represented by the provided key, with cast to given
260+
* class.
261261
* @param key The key to get a value for
262-
* @param clazz The class of return type
262+
* @param type The class of return type
263263
* @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
264+
* @return The value of given type represented by the given key or {@code null} if the
265+
* key is not present
266266
*/
267267
@Nullable
268-
public <V> V get(String key, Class<V> clazz) {
268+
public <V> V get(String key, Class<V> type) {
269269
Object value = this.map.get(key);
270270
if (value == null) {
271271
return null;
272272
}
273-
return get(key, clazz, null);
273+
return get(key, type, null);
274274
}
275275

276276
/**
277-
* Typesafe getter for the value represented by the provided key, with cast to given class.
278-
*
277+
* Typesafe getter for the value represented by the provided key, with cast to given
278+
* class.
279279
* @param key The key to get a value for
280280
* @param type The class of return type
281281
* @param defaultValue Default value in case element is not present
282282
* @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
283+
* @return The value of given type represented by the given key or the default value
284+
* if the key is not present
285285
*/
286286
@Nullable
287-
public <V> V get(String key, Class<V> clazz, @Nullable V defaultValue) {
287+
public <V> V get(String key, Class<V> type, @Nullable V defaultValue) {
288288
Object value = this.map.get(key);
289289
if (value == null) {
290290
return defaultValue;
291291
}
292-
return clazz.cast(value);
292+
if (!type.isInstance(value)) {
293+
throw new ClassCastException("Value for key=[" + key + "] is not of type: [" + type + "], it is [" + "("
294+
+ value.getClass() + ")" + value + "]");
295+
}
296+
return type.cast(value);
293297
}
294298

295299
/**

0 commit comments

Comments
 (0)