Class StateMachine<T extends java.lang.Enum<T>>

  • Type Parameters:
    T - states.

    public final class StateMachine<T extends java.lang.Enum<T>>
    extends java.lang.Object
    A simple state machine.

    This class is not thread safe!

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  StateMachine.Builder<S extends java.lang.Enum<S>>  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean assertState​(T... expected)
      Verify expected state.
      boolean setState​(T nextState)
      Set the new state.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • toString

        @NonNull
        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • assertState

        @SafeVarargs
        public final boolean assertState​(@NonNull
                                         T... expected)
        Verify expected state.
        Parameters:
        expected - expected states.
        Returns:
        true if the current state at the time of the call, is one of the expected states.
      • setState

        public boolean setState​(@NonNull
                                T nextState)
        Set the new state. If it is legal to transition to the new state, from the current state, do so, returning the now-previous state. If the transition is illegal do nothing and return null.
        Parameters:
        nextState - the requested new state
        Returns:
        the previous state, if the transition succeeds; null otherwise.