Skip to content

Commit

Permalink
Refine contribution #718
Browse files Browse the repository at this point in the history
- Rename variables
- Update javadoc
- Add type check before cast
- Update code formatting
  • Loading branch information
fmbenhassine committed Sep 15, 2023
1 parent 306d737 commit 54239fe
Showing 1 changed file with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,40 +256,44 @@ public Object get(String key) {
}

/**
* Typesafe getter for the value represented by the provided key, with cast to given class.
*
* 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 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
* @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) {
public <V> V get(String key, Class<V> type) {
Object value = this.map.get(key);
if (value == null) {
return null;
}
return get(key, clazz, null);
return get(key, type, null);
}

/**
* Typesafe getter for the value represented by the provided key, with cast to given class.
*
* 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
* @return The value of given type represented by the given key or the default value
* if the key is not present
*/
@Nullable
public <V> V get(String key, Class<V> clazz, @Nullable V defaultValue) {
public <V> V get(String key, Class<V> type, @Nullable V defaultValue) {
Object value = this.map.get(key);
if (value == null) {
return defaultValue;
}
return clazz.cast(value);
if (!type.isInstance(value)) {
throw new ClassCastException("Value for key=[" + key + "] is not of type: [" + type + "], it is [" + "("
+ value.getClass() + ")" + value + "]");
}
return type.cast(value);
}

/**
Expand Down

0 comments on commit 54239fe

Please sign in to comment.