Class StateMachine


  • public final class StateMachine
    extends java.lang.Object
    A finite state machine that can be created with user defined states and transitions.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  StateMachine.Builder
      Builder that builds a StateMachine.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void checkState​(java.lang.Enum expectedCurrentState)
      Checks whether the current state is same as the expectedCurrentState.
      boolean compareAndSetState​(java.lang.Enum expectedCurrentState, java.lang.Enum state)
      Atomically sets the state to the given updated state if the current state equals to the expected state.
      java.lang.Enum getCurrentState()  
      static StateMachine.Builder newBuilder()  
      void setState​(java.lang.Enum state)
      Sets the current state as a certain state.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • checkState

        public void checkState​(java.lang.Enum expectedCurrentState)
        Checks whether the current state is same as the expectedCurrentState.
        Parameters:
        expectedCurrentState - the expected current state
        Throws:
        java.lang.RuntimeException - if the expectedCurrentState is not same as the actual current state
      • setState

        public void setState​(java.lang.Enum state)
                      throws IllegalStateTransitionException
        Sets the current state as a certain state.
        Parameters:
        state - a state
        Throws:
        IllegalStateTransitionException - the state is unknown state, or the transition from the current state to the specified state is illegal
      • compareAndSetState

        public boolean compareAndSetState​(java.lang.Enum expectedCurrentState,
                                          java.lang.Enum state)
                                   throws IllegalStateTransitionException
        Atomically sets the state to the given updated state if the current state equals to the expected state.
        Parameters:
        expectedCurrentState - an expected state
        state - a state
        Returns:
        true if successful. false indicates that the actual value was not equal to the expected value.
        Throws:
        IllegalStateTransitionException - if the state is unknown state, or the transition from the current state to the specified state is illegal
      • getCurrentState

        public java.lang.Enum getCurrentState()
        Returns:
        the name of the current state.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object