Package io.trino.execution
Class StateMachine<T>
java.lang.Object
io.trino.execution.StateMachine<T>
Simple state machine which holds a single state. Callers can register for state change events.
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionStateMachine(String name, Executor executor, T initialState) Creates a state machine with the specified initial state and no terminal states.Creates a state machine with the specified initial state and terminal states. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddStateChangeListener(StateMachine.StateChangeListener<T> stateChangeListener) Adds a listener to be notified when the state instance changes according to.equals().booleancompareAndSet(T expectedState, T newState) Sets the state if the current state.equals()the specified expected state.get()com.google.common.util.concurrent.ListenableFuture<T> getStateChange(T currentState) Gets a future that completes when the state is no longer.equals()tocurrentState).Sets the state.booleanSets the state if the current state satisfies the specified predicate.toString()Tries to change the state.
-
Constructor Details
-
StateMachine
Creates a state machine with the specified initial state and no terminal states.- Parameters:
name- name of this state machine to use in debug statementsexecutor- executor for firing state change events; must not be a same thread executorinitialState- the initial state
-
StateMachine
Creates a state machine with the specified initial state and terminal states.- Parameters:
name- name of this state machine to use in debug statementsexecutor- executor for firing state change events; must not be a same thread executorinitialState- the initial stateterminalStates- the terminal states
-
-
Method Details
-
get
-
set
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
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
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
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
Gets a future that completes when the state is no longer.equals()tocurrentState). -
addStateChangeListener
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 leakingthiswhen 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
-