E - the type of the entity owning this state machineS - the type of the states of this state machinepublic class DefaultStateMachine<E,S extends State<E>> extends Object implements StateMachine<E,S>
StateMachine interface.| Modifier and Type | Field and Description |
|---|---|
protected S |
currentState
The current state the owner is in.
|
protected S |
globalState
The global state of the owner.
|
protected E |
owner
The entity that owns this state machine.
|
protected S |
previousState
The last state the owner was in.
|
| Constructor and Description |
|---|
DefaultStateMachine()
Creates a
DefaultStateMachine with no owner, initial state and global state. |
DefaultStateMachine(E owner)
Creates a
DefaultStateMachine for the specified owner. |
DefaultStateMachine(E owner,
S initialState)
Creates a
DefaultStateMachine for the specified owner and initial state. |
DefaultStateMachine(E owner,
S initialState,
S globalState)
Creates a
DefaultStateMachine for the specified owner, initial state and global state. |
| Modifier and Type | Method and Description |
|---|---|
void |
changeState(S newState)
Performs a transition to the specified state.
|
S |
getCurrentState()
Returns the current state of this state machine.
|
S |
getGlobalState()
Returns the global state of this state machine.
|
E |
getOwner()
Returns the owner of this state machine.
|
S |
getPreviousState()
Returns the last state of this state machine.
|
boolean |
handleMessage(Telegram telegram)
Handles received telegrams.
|
boolean |
isInState(S state)
Indicates whether the state machine is in the given state.
|
boolean |
revertToPreviousState()
Changes the state back to the previous state.
|
void |
setGlobalState(S state)
Sets the global state of this state machine.
|
void |
setInitialState(S state)
Sets the initial state of this state machine.
|
void |
setOwner(E owner)
Sets the owner of this state machine.
|
void |
update()
Updates the state machine by invoking first the
execute method of the global state (if any) then the execute
method of the current state. |
protected E owner
public DefaultStateMachine()
DefaultStateMachine with no owner, initial state and global state.public DefaultStateMachine(E owner)
DefaultStateMachine for the specified owner.owner - the owner of the state machinepublic DefaultStateMachine(E owner, S initialState)
DefaultStateMachine for the specified owner and initial state.owner - the owner of the state machineinitialState - the initial statepublic E getOwner()
public void setOwner(E owner)
owner - the owner.public void setInitialState(S state)
StateMachinesetInitialState in interface StateMachine<E,S extends State<E>>state - the initial state.public void setGlobalState(S state)
StateMachinesetGlobalState in interface StateMachine<E,S extends State<E>>state - the global state.public S getCurrentState()
StateMachinegetCurrentState in interface StateMachine<E,S extends State<E>>public S getGlobalState()
StateMachine
Implementation classes should invoke the update method of the global state every time the FSM is updated. Also, they
should never invoke its enter and exit method.
getGlobalState in interface StateMachine<E,S extends State<E>>public S getPreviousState()
StateMachinegetPreviousState in interface StateMachine<E,S extends State<E>>public void update()
execute method of the global state (if any) then the execute
method of the current state.public void changeState(S newState)
StateMachinechangeState in interface StateMachine<E,S extends State<E>>newState - the state to transition topublic boolean revertToPreviousState()
StateMachinerevertToPreviousState in interface StateMachine<E,S extends State<E>>true in case there was a previous state that we were able to revert to.
In case there is no previous state,
no state change occurs and false will be returned.public boolean isInState(S state)
This implementation assumes states are singletons (typically an enum) so they are compared with the == operator
instead of the equals method.
public boolean handleMessage(Telegram telegram)
handleMessage in interface StateMachine<E,S extends State<E>>handleMessage in interface Telegraphtelegram - the received telegramCopyright © 2018. All rights reserved.