Package io.prestosql.execution
Class StateMachine<T>
- java.lang.Object
-
- io.prestosql.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
StateMachine.StateChangeListener<T>
-
Constructor Summary
Constructors Constructor Description StateMachine(String name, Executor executor, T initialState)
Creates a state machine with the specified initial state and no terminal states.StateMachine(String name, Executor executor, T initialState, Iterable<T> terminalStates)
Creates a state machine with the specified initial state and terminal states.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addStateChangeListener(StateMachine.StateChangeListener<T> stateChangeListener)
Adds a listener to be notified when the state instance changes according to.equals()
.boolean
compareAndSet(T expectedState, T newState)
Sets the state if the current state.equals()
the specified expected state.T
get()
com.google.common.util.concurrent.ListenableFuture<T>
getStateChange(T currentState)
Gets a future that completes when the state is no longer.equals()
tocurrentState)
.T
set(T newState)
Sets the state.boolean
setIf(T newState, Predicate<T> predicate)
Sets the state if the current state satisfies the specified predicate.String
toString()
T
trySet(T newState)
Tries to change the state.
-
-
-
Constructor Detail
-
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 statementsexecutor
- executor for firing state change events; must not be a same thread executorinitialState
- 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 statementsexecutor
- executor for firing state change events; must not be a same thread executorinitialState
- the initial stateterminalStates
- the terminal states
-
-
Method Detail
-
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()
tocurrentState)
.
-
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 leakingthis
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.
-
-