Package com.complexible.common.base
Class AtomicState<S extends Enum<S>>
java.lang.Object
com.complexible.common.base.AtomicState<S>
Represents a mutable state that can be updated exclusively through
beginTransitionFrom(Enum[])
and endTransition() calls. Actual states are represented as enumerations. This class guarantees that
there will be a single current state at any time and at most one state transition action will be running at
any time.- Since:
- 2.0.3
- Version:
- 3.0
- Author:
- Evren Sirin
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbeginTransitionFrom(S... theStates) Starts a transition provided that the current state is one of the specified states.voidRequest the cancellation of current transition.voidcheckState(Collection<S> theStates) ThrowsIllegalStateExceptionif the current state is one of the given states.voidcheckState(S theState) ThrowsIllegalStateExceptionif the current state is not equal to the given state.voidcheckStateNot(S theState) ThrowsIllegalStateExceptionif the current state is equal to the given state.voidEnds the current transition.voidendTransition(S theFinalState) Ends the transition with the given final state.get()Returns the current state.booleanReturnstrueif a transition is being performed.booleanReturnstrueif the current transition is requested to be canceled.static <S extends Enum<S>>
AtomicState<S> newState(S theInitState) voidSets the current state only if the current threadbegana transition.voidtransitionTo(S theFinalState, S... theInitialStates) Performs the transition to a final state.tryBeginTransition(S... thePossibleInitialStates) Attempts to start a transition "tentatively": that is, it will attempt the transition only if the current state is contained inthePossibleInitialStates; If it is not, this will unroll any action taken, as if the transition was never attempted.
-
Constructor Details
-
AtomicState
-
-
Method Details
-
newState
-
beginTransitionFrom
Starts a transition provided that the current state is one of the specified states. If no initial states are provided then the transition will begin regardless of the current state. This call will block if a current transition is in progress.- Throws:
IllegalStateException- if the current state is not a valid initial state for the action
-
tryBeginTransition
Attempts to start a transition "tentatively": that is, it will attempt the transition only if the current state is contained inthePossibleInitialStates; If it is not, this will unroll any action taken, as if the transition was never attempted. If the transition is successful, then the old state will be returned. If it is not successful, thennullis returned. This is an inexpensive way of doing idempotent transitions (i.e. only transition to a single state once).- Parameters:
thePossibleInitialStates- the possible states that the transaction can be in- Returns:
- the old state, if the transition has begun, or
nullif the transition fails
-
endTransition
Ends the transition with the given final state. This is equivalent to:try { set(theFinalState); } finally { endTransition(); }- Parameters:
theFinalState- the final state
-
endTransition
public void endTransition()Ends the current transition. -
transitionTo
Performs the transition to a final state. This is equivalent to:beginTransitionFrom(theInitialStates); try { set(theFinalState); } finally { endTransition(); }- Parameters:
theFinalState-theInitialStates-
-
cancelTransition
public void cancelTransition()Request the cancellation of current transition. -
isTransitionCanceled
public boolean isTransitionCanceled()Returnstrueif the current transition is requested to be canceled. -
get
Returns the current state. -
set
Sets the current state only if the current threadbegana transition. -
checkState
ThrowsIllegalStateExceptionif the current state is not equal to the given state. -
checkState
ThrowsIllegalStateExceptionif the current state is one of the given states. -
checkStateNot
ThrowsIllegalStateExceptionif the current state is equal to the given state. -
isInTransition
public boolean isInTransition()Returnstrueif a transition is being performed.
-