Skip to content

Commit

Permalink
Add type safe getters to execution context
Browse files Browse the repository at this point in the history
Issue #718
  • Loading branch information
IlyaNerd authored and fmbenhassine committed Sep 15, 2023
1 parent cc233e6 commit 306d737
Showing 1 changed file with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void putDouble(String key, double value) {
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);
Expand All @@ -148,7 +148,7 @@ public boolean isDirty() {
*/
public String getString(String key) {

return (String) readAndValidate(key, String.class);
return readAndValidate(key, String.class);
}

/**
Expand All @@ -174,7 +174,7 @@ public String getString(String key, String defaultString) {
*/
public long getLong(String key) {

return (Long) readAndValidate(key, Long.class);
return readAndValidate(key, Long.class);
}

/**
Expand All @@ -200,7 +200,7 @@ public long getLong(String key, long defaultLong) {
*/
public int getInt(String key) {

return (Integer) readAndValidate(key, Integer.class);
return readAndValidate(key, Integer.class);
}

/**
Expand All @@ -225,7 +225,7 @@ public int getInt(String key, int defaultInt) {
* @return The <code>Double</code> value
*/
public double getDouble(String key) {
return (Double) readAndValidate(key, Double.class);
return readAndValidate(key, Double.class);
}

/**
Expand Down Expand Up @@ -255,14 +255,51 @@ public Object get(String key) {
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 <V> 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> V get(String key, Class<V> 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 <V> 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> V get(String key, Class<V> 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.
* @param key The key to validate a value for
* @param type Class against which value should be validated
* @return Value typed to the specified <code>Class</code>
*/
private Object readAndValidate(String key, Class<?> type) {
private <V> V readAndValidate(String key, Class<V> type) {

Object value = get(key);

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

return value;
return type.cast(value);
}

/**
Expand Down

0 comments on commit 306d737

Please sign in to comment.