@ThreadSafe public class StateMachine<T> extends Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
StateMachine.StateChangeListener<T> |
| Constructor and 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.
|
| Modifier and Type | Method and 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() to currentState). |
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() |
public StateMachine(String name, Executor executor, T initialState)
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 statepublic StateMachine(String name, Executor executor, T initialState, Iterable<T> terminalStates)
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 statespublic T get()
public T set(T newState)
.equals() the current state, listeners and waiters will be notified.public boolean setIf(T newState, Predicate<T> predicate)
.equals() the current state, listeners and waiters will be notified.public boolean compareAndSet(T expectedState, T newState)
.equals() the specified expected state.
If the new state does not .equals() the current state, listeners and waiters will be notified.public com.google.common.util.concurrent.ListenableFuture<T> getStateChange(T currentState)
.equals() to currentState).public void addStateChangeListener(StateMachine.StateChangeListener<T> stateChangeListener)
.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.Copyright © 2012–2019. All rights reserved.