Class StateMachine


  • public class StateMachine
    extends java.lang.Object
    Represents a complete state machine. Contains a collection of State objects connected by Transitions. Normally you wouldn't create instances of this class directly but rather use the State annotation to define your states and then let StateMachineFactory create a StateMachine for you.
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • CALL_STACK

        private static final java.lang.String CALL_STACK
      • startState

        private final State startState
      • states

        private final java.util.Map<java.lang.String,​State> states
      • processingThreadLocal

        private final java.lang.ThreadLocal<java.lang.Boolean> processingThreadLocal
      • eventQueueThreadLocal

        private final java.lang.ThreadLocal<java.util.LinkedList<Event>> eventQueueThreadLocal
    • Constructor Detail

      • StateMachine

        public StateMachine​(State[] states,
                            java.lang.String startStateId)
        Creates a new instance using the specified States and start state.
        Parameters:
        states - the States.
        startStateId - the id of the start State.
      • StateMachine

        public StateMachine​(java.util.Collection<State> states,
                            java.lang.String startStateId)
        Creates a new instance using the specified States and start state.
        Parameters:
        states - the States.
        startStateId - the id of the start State.
    • Method Detail

      • getState

        public State getState​(java.lang.String id)
        Returns the State with the specified id.
        Parameters:
        id - the id of the State to return.
        Returns:
        the State
        Throws:
        NoSuchStateException - if no matching State could be found.
      • getStates

        public java.util.Collection<State> getStates()
        Returns:
        an unmodifiable Collection of all States used by this StateMachine.
      • handle

        public void handle​(Event event)
        Processes the specified Event through this StateMachine. Normally you wouldn't call this directly but rather use StateMachineProxyBuilder to create a proxy for an interface of your choice. Any method calls on the proxy will be translated into Event objects and then fed to the StateMachine by the proxy using this method.
        Parameters:
        event - the Event to be handled.
      • processEvents

        private void processEvents​(java.util.LinkedList<Event> eventQueue)
      • handle

        private void handle​(State state,
                            Event event)
      • getCallStack

        private java.util.Deque<State> getCallStack​(StateContext context)
      • setCurrentState

        private void setCurrentState​(StateContext context,
                                     State newState)