E - An enumeration of events managed by this class
O - The object type to be passed to the state machine handler
public class StateMachine<S extends Enum,E extends Enum,O>
This class manages state transitions in an organized way. It effectively makes a directed
graph such that each node is a state and each edge an event. It implements a traditional
The class is used by first constructing all of the state and event transitions by calling
addTransition(). In this way we define what state transitions are legal and both what
events cause which state transitions and the logic required to successfully move from one
state to the next.
When events occur in a system eventOccurred() is called signalling the state machine that a new
event occurred. The state machine then looks at its current state to see if it supports
that event. If it does the StateTransition object is looked up and the Runnable associated
with it is called. If that returns without an exception the state machine moves to the
This method is used to build the state machine transitions. Each call creates a directed edge
between the theStartState and the theNextState. A Runnable is associated with the transition. The only
way to move from one state to the next is by executing the Runnable associated with that state
When an event occurs the current state is checked. If a transition was added such that the current
state matches theStartState and the occurring event matches theEvent then the state machine will
execute theMethod Runnable object. If it returns successfully the state machine will move to the
theStartState - The starting state for this transition mapping.
theEvent - The event that this transition will handle.
theNextState - The state to move to if the transition is successful.
theTransitionConsumer - The method to run that will handle the logic for this transition.
If this is null the state machine will move directly to theNextState.
theUserObject - A object to be passed to the state transition handlers.
This method signals the state machine that a given event occurred. If there is a transition from
the current state to a new state given this event then a transition handler method will be executed.
If the handler returns without an exception the state machine will transition to the new state.