Type cast access for ext state variables.

This commit is contained in:
Janne Valkealahti
2015-04-10 09:14:58 +01:00
parent eae691f4a5
commit 60e8f0745a
3 changed files with 29 additions and 6 deletions

View File

@@ -34,4 +34,13 @@ public interface ExtendedState {
*/
Map<Object, Object> getVariables();
/**
* Gets a variable which is automatically casted into a type.
*
* @param key the variable key
* @param type the variable type
* @return the variable
*/
<T> T get(Object key, Class<T> type);
}

View File

@@ -42,4 +42,18 @@ public class DefaultExtendedState implements ExtendedState {
return variables;
}
@SuppressWarnings("unchecked")
@Override
public <T> T get(Object key, Class<T> type) {
Object value = this.variables.get(key);
if (value == null) {
return null;
}
if (!type.isAssignableFrom(value.getClass())) {
throw new IllegalArgumentException("Incorrect type specified for variable '" +
key + "'. Expected [" + type + "] but actual type is [" + value.getClass() + "]");
}
return (T) value;
}
}