Allow states to be automatically added to the state map.

They have to have a Constructor that accept the StateMap as argument, otherwise a RuntimeException is thrown.
This commit is contained in:
snowleo 2011-10-26 17:42:39 +02:00
parent 38b6d79f49
commit 102570958e

View File

@ -16,6 +16,23 @@ public abstract class AbstractState
public AbstractState getState(final Class<? extends AbstractState> stateClass)
{
if (!stateMap.containsKey(stateClass))
{
try
{
final AbstractState state = stateClass.getConstructor(StateMap.class).newInstance(stateMap);
stateMap.put(stateClass, state);
}
catch (Exception ex)
{
/*
* This should never happen.
* All states that are added to the map automatically,
* have to have a Constructor that accepts the StateMap.
*/
throw new RuntimeException(ex);
}
}
return stateMap.get(stateClass);
}