Class StateMachine<T>

java.lang.Object
io.trino.execution.StateMachine<T>

@ThreadSafe public class StateMachine<T> extends Object
Simple state machine which holds a single state. Callers can register for state change events.
  • Constructor Details

    • StateMachine

      public StateMachine(String name, Executor executor, T initialState)
      Creates a state machine with the specified initial state and no terminal states.
      Parameters:
      name - name of this state machine to use in debug statements
      executor - executor for firing state change events; must not be a same thread executor
      initialState - the initial state
    • StateMachine

      public StateMachine(String name, Executor executor, T initialState, Iterable<T> terminalStates)
      Creates a state machine with the specified initial state and terminal states.
      Parameters:
      name - name of this state machine to use in debug statements
      executor - executor for firing state change events; must not be a same thread executor
      initialState - the initial state
      terminalStates - the terminal states
  • Method Details

    • get

      public T get()
    • set

      public T set(T newState)
      Sets the state. If the new state does not .equals() the current state, listeners and waiters will be notified.
      Returns:
      the old state
      Throws:
      IllegalStateException - if state change would cause a transition from a terminal state
    • trySet

      public T trySet(T newState)
      Tries to change the state. State will not change if the new state .equals() the current state, of if the current state is a terminal state. If the state changed, listeners and waiters will be notified.
      Returns:
      the state before the possible state change
    • setIf

      public boolean setIf(T newState, Predicate<T> predicate)
      Sets the state if the current state satisfies the specified predicate. If the new state does not .equals() the current state, listeners and waiters will be notified.
      Returns:
      true if the state is set
    • compareAndSet

      public boolean compareAndSet(T expectedState, T newState)
      Sets the state if the current state .equals() the specified expected state. If the new state does not .equals() the current state, listeners and waiters will be notified.
      Returns:
      true if the state is set
    • getStateChange

      public com.google.common.util.concurrent.ListenableFuture<T> getStateChange(T currentState)
      Gets a future that completes when the state is no longer .equals() to currentState).
    • addStateChangeListener

      public void addStateChangeListener(StateMachine.StateChangeListener<T> stateChangeListener)
      Adds a listener to be notified when the state instance changes according to .equals(). Listener is always notified asynchronously using a dedicated notification thread pool so, care should be taken to avoid leaking this when adding a listener in a constructor. Additionally, it is possible notifications are observed out of order due to the asynchronous execution. The listener is immediately notified immediately of the current state.
    • toString

      public String toString()
      Overrides:
      toString in class Object