Package com.complexible.stardog.api.admin.pack

# Class StateMachine<S extends Enum,E extends Enum,O>

java.lang.Object
com.complexible.stardog.api.admin.pack.StateMachine<S,E,O>

Type Parameters:
  `S` - An enumeration of possible states
  `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> extends Object
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 state machine. 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 new state.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `class`
  `StateMachine.StateTransition`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected S`
  `mCurrentState`
  `protected StateMachine<S,E,O>.StateTransition`
  `mCurrentTransition`
  `protected final Map<S,Map<E,StateMachine<S,E,O>.StateTransition>>`
  `mTransitions`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `StateMachine(S theFirstState)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addTransition(S theStartState, E theEvent, S theNextState, Consumer<StateMachine<S,E,O>.StateTransition> theTransitionConsumer, O theUserObject)`
  This method is used to build the state machine transitions.
  `void`
  `eventOccurred(E theEvent)`
  This method signals the state machine that a given event occurred.
  `S`
  `getCurrentState()`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### mTransitions

    protected final Map<S extends Enum,Map<E extends Enum,StateMachine<S extends Enum,E extends Enum,O>.StateTransition>> mTransitions

  - ### mCurrentState

    protected S extends Enum mCurrentState

  - ### mCurrentTransition

    protected StateMachine<S extends Enum,E extends Enum,O>.StateTransition mCurrentTransition

- ## Constructor Details

  - ### StateMachine

    public StateMachine(S theFirstState)
    Parameters:
      `theFirstState` - The beginning state

- ## Method Details

  - ### addTransition

    public void addTransition(S theStartState, E theEvent, S theNextState, Consumer<StateMachine<S,E,O>.StateTransition> theTransitionConsumer, O theUserObject)
    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 transition. 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 theNextState.
    Parameters:
      `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.

  - ### eventOccurred

    public void eventOccurred(E theEvent)
    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.
    Parameters:
      `theEvent` -

  - ### getCurrentState

    public S getCurrentState()
    Returns:
      the current state which the state machine is in.