public class StateMachine extends Object implements Transitioner<State,Message,StateTransition>
| Constructor and Description |
|---|
StateMachine()
Creates the state machine.
|
StateMachine(@NonNull TransitionChecker<State,Message> transitionChecker)
Creates the state machine with the given
transition checker.
|
StateMachine(@NonNull TransitionIndex<State,Message,StateTransition> transitionIndex)
Creates the state machine with the given
transition map.
|
StateMachine(@NonNull TransitionIndex<State,Message,StateTransition> transitionIndex,
@NonNull TransitionChecker<State,Message> transitionChecker)
Creates the state machine with the given
transition map and checker
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(StateTransition transition)
Adds a new Transition to the state
machine.
|
void |
addAll(@NonNull Collection<StateTransition> transitions)
Adds a collection of Transition to the state
machine.
|
void |
clear()
Removes all transitions from the state machine
|
Optional<State> |
find(String stateName)
Finds the state referenced by the given name
in the state machine
machine.
|
Collection<StateTransition> |
getAllTransitions()
Retrieves all state transitions currently
defined.
|
State |
getCurrent()
Retrieves the current state
|
Optional<State> |
getNext(Message m)
Retrieves the next state upon
message reception.
|
Collection<State> |
getSiblings(String stateName)
Retrieves the adjacent states i.e.
|
Collection<StateTransition> |
getTransitions(String stateName)
Retrieves the state transitions for the given
state.
|
TransitionIndex<State,Message,StateTransition> |
getTransitionsIndex()
Retrieves the transition map
|
void |
init()
Initializes the current state
to the first one added to the state machine (if any).
|
static StateMachineBuilder |
newBuilder()
Retrieves a new
StateMachineBuilder instance
to ease the State Machine creation process. |
StateMachine |
next()
Sends a void message to the state machine triggering
a potential transition.
|
List<State> |
prune()
Remove orphan states
|
void |
remove(State state)
Removes an existing state from the state machine.
|
void |
remove(StateTransition transition)
Removes an existing Transition from the state machine.
|
void |
remove(String stateName)
Removes an existing state from the state machine.
|
void |
removeAllTransitions()
Removes all existing Transitions from the state machine.
|
StateMachine |
send(Message message)
Sends a message to the state machine triggering
a potential transition.
|
StateMachine |
send(String message)
Sends a message to the state machine triggering
a potential transition.
|
void |
setCurrent(@NonNull State state)
Sets the current state if present
|
void |
setCurrent(@NonNull String stateName)
Sets the current state to the
given one.
|
int |
size()
Retrieves the number of states currently
present in the state machine.
|
public StateMachine()
public StateMachine(@NonNull
@NonNull TransitionIndex<State,Message,StateTransition> transitionIndex)
transitionIndex - the transition mappublic StateMachine(@NonNull
@NonNull TransitionChecker<State,Message> transitionChecker)
transitionChecker - the transition checkerpublic StateMachine(@NonNull
@NonNull TransitionIndex<State,Message,StateTransition> transitionIndex,
@NonNull
@NonNull TransitionChecker<State,Message> transitionChecker)
transitionIndex - the transition maptransitionChecker - the transition checkerpublic static StateMachineBuilder newBuilder()
StateMachineBuilder instance
to ease the State Machine creation process.public void add(StateTransition transition)
add in interface Transitioner<State,Message,StateTransition>transition - the transition to addpublic void addAll(@NonNull
@NonNull Collection<StateTransition> transitions)
addAll in interface Transitioner<State,Message,StateTransition>transitions - the transition to addpublic void remove(StateTransition transition)
remove in interface Transitioner<State,Message,StateTransition>transition - the transition to removepublic void remove(String stateName)
NullStateException
is thrown.remove in interface Transitioner<State,Message,StateTransition>stateName - the state to removepublic void remove(State state)
NullStateException
is thrown.remove in interface Transitioner<State,Message,StateTransition>state - the state to removepublic void removeAllTransitions()
removeAllTransitions in interface Transitioner<State,Message,StateTransition>public void clear()
clear in interface Transitioner<State,Message,StateTransition>public Optional<State> find(String stateName)
find in interface Transitioner<State,Message,StateTransition>stateName - the name of the nodepublic void init()
init in interface Transitioner<State,Message,StateTransition>public void setCurrent(@NonNull
@NonNull String stateName)
NullStateException
in case the state is not found.setCurrent in interface Transitioner<State,Message,StateTransition>stateName - the node's namepublic void setCurrent(@NonNull
@NonNull State state)
state - the state to setpublic State getCurrent()
getCurrent in interface Transitioner<State,Message,StateTransition>public Optional<State> getNext(Message m)
getNext in interface Transitioner<State,Message,StateTransition>m - the messagepublic StateMachine next()
next in interface Transitioner<State,Message,StateTransition>Transitioner instance for chaining purposespublic StateMachine send(String message)
message - a string messagepublic StateMachine send(Message message)
send in interface Transitioner<State,Message,StateTransition>message - the messagepublic int size()
size in interface Transitioner<State,Message,StateTransition>public Collection<StateTransition> getTransitions(String stateName)
getTransitions in interface Transitioner<State,Message,StateTransition>stateName - the state namepublic Collection<State> getSiblings(String stateName)
getSiblings in interface Transitioner<State,Message,StateTransition>stateName - the state's namepublic Collection<StateTransition> getAllTransitions()
getAllTransitions in interface Transitioner<State,Message,StateTransition>public TransitionIndex<State,Message,StateTransition> getTransitionsIndex()
getTransitionsIndex in interface Transitioner<State,Message,StateTransition>public List<State> prune()
prune in interface Transitioner<State,Message,StateTransition>Copyright © 2019 state-machine. All rights reserved.