com.twitter.common.util
Class StateMachine.Builder<T>

java.lang.Object
  extended by com.twitter.common.util.StateMachine.Builder<T>
Type Parameters:
T -
Enclosing class:
StateMachine<T>

public static class StateMachine.Builder<T>
extends Object

Builder to create a state machine.


Constructor Summary
StateMachine.Builder(String name)
           
 
Method Summary
 StateMachine.Builder<T> addState(Closure<StateMachine.Transition<T>> callback, T state, Set<T> transitionStates)
          Adds a state and its allowed transitions.
 StateMachine.Builder<T> addState(Closure<StateMachine.Transition<T>> callback, T state, T... transitionStates)
          Varargs version of addState(com.twitter.common.base.Closure, Object, java.util.Set).
 StateMachine.Builder<T> addState(StateMachine.Rule<T> rule)
          Adds a state and its allowed transitions.
 StateMachine.Builder<T> addState(T state, T... transitionStates)
          Adds a state and its allowed transitions.
 StateMachine<T> build()
          Builds the state machine.
 StateMachine.Builder<T> initialState(T state)
          Sets the initial state for the state machine.
 StateMachine.Builder<T> logTransitions()
          Adds a log message for every state transition that is attempted.
 StateMachine.Builder<T> onAnyTransition(Closure<StateMachine.Transition<T>> handler)
          Adds a callback to be executed for every state transition, including invalid transitions that are attempted.
 StateMachine.Builder<T> throwOnBadTransition(boolean throwOnBadTransition)
          Allows the caller to specify whether StateMachine.IllegalStateTransitionException should be thrown when a bad state transition is attempted (the default behavior).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StateMachine.Builder

public StateMachine.Builder(String name)
Method Detail

initialState

public StateMachine.Builder<T> initialState(T state)
Sets the initial state for the state machine.

Parameters:
state - Initial state.
Returns:
A reference to the builder.

addState

public StateMachine.Builder<T> addState(StateMachine.Rule<T> rule)
Adds a state and its allowed transitions.

Parameters:
rule - The state and transition rule to add.
Returns:
A reference to the builder.

addState

public StateMachine.Builder<T> addState(Closure<StateMachine.Transition<T>> callback,
                                        T state,
                                        Set<T> transitionStates)
Adds a state and its allowed transitions. At least one transition state must be added, it is not necessary to explicitly add states that have no allowed transitions (terminal states).

Parameters:
callback - Callback to notify of any transition attempted from the state.
state - State to add.
transitionStates - Allowed transitions from state.
Returns:
A reference to the builder.

addState

public StateMachine.Builder<T> addState(Closure<StateMachine.Transition<T>> callback,
                                        T state,
                                        T... transitionStates)
Varargs version of addState(com.twitter.common.base.Closure, Object, java.util.Set).

Parameters:
callback - Callback to notify of any transition attempted from the state.
state - State to add.
transitionStates - Allowed transitions from state.
Returns:
A reference to the builder.

addState

public StateMachine.Builder<T> addState(T state,
                                        T... transitionStates)
Adds a state and its allowed transitions. At least one transition state must be added, it is not necessary to explicitly add states that have no allowed transitions (terminal states).

Parameters:
state - State to add.
transitionStates - Allowed transitions from state.
Returns:
A reference to the builder.

onAnyTransition

public StateMachine.Builder<T> onAnyTransition(Closure<StateMachine.Transition<T>> handler)
Adds a callback to be executed for every state transition, including invalid transitions that are attempted.

Parameters:
handler - Callback to notify of transition attempts.
Returns:
A reference to the builder.

logTransitions

public StateMachine.Builder<T> logTransitions()
Adds a log message for every state transition that is attempted.

Returns:
A reference to the builder.

throwOnBadTransition

public StateMachine.Builder<T> throwOnBadTransition(boolean throwOnBadTransition)
Allows the caller to specify whether StateMachine.IllegalStateTransitionException should be thrown when a bad state transition is attempted (the default behavior).

Parameters:
throwOnBadTransition - Whether an exception should be thrown when a bad state transition is attempted.
Returns:
A reference to the builder.

build

public StateMachine<T> build()
Builds the state machine.

Returns:
A reference to the prepared state machine.