|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbb.util.StateMachine<E>
public class StateMachine<E extends Enum<E>>
Simple implementation of a software state machine.
Currently, this class merely stores state transition rules, and offers the isTransitionAllowed
method
for users to verify transitions at runtime.
This class is multithread safe: it is immutable (both its immediate state, as well as the deep state of its fields).
Nested Class Summary | |
---|---|
static class |
StateMachine.UnitTest
See the Overview page of the project's javadocs for a general description of this unit test class. |
Field Summary | |
---|---|
private EnumMap<E,EnumSet<E>> |
transitions
Every key is an initial state, and its corresponding value is the transition rule for that state (i.e. the set of states that are allowed to be transitioned to). |
Constructor Summary | |
---|---|
StateMachine(Class<E> clazz,
String... rules)
Convenience constructor which parses the transition rules from a String. |
|
StateMachine(EnumMap<E,EnumSet<E>> transitions)
Fundamental constructor. |
Method Summary | |
---|---|
private EnumMap<E,EnumSet<E>> |
checkTransitions(EnumMap<E,EnumSet<E>> transitions)
|
private EnumMap<E,EnumSet<E>> |
cloneDeep(EnumMap<E,EnumSet<E>> transitions)
|
boolean |
isTransitionAllowed(E stateInitial,
E stateFinal)
|
private E |
parseStateInitial(Class<E> clazz,
String rule)
|
private EnumSet<E> |
parseStatesFinal(Class<E> clazz,
String rule)
|
private EnumMap<E,EnumSet<E>> |
parseTransitions(Class<E> clazz,
String... rules)
Parses rules into an EnumMap. |
String |
toString()
Returns a String representation of transitions . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
private final EnumMap<E extends Enum<E>,EnumSet<E extends Enum<E>>> transitions
Contract: is never null, defines a non-empty transition rule for every value of E, and must never be modified.
Constructor Detail |
---|
public StateMachine(EnumMap<E,EnumSet<E>> transitions) throws IllegalArgumentException
IllegalArgumentException
- if the transitions arg violates the contract of the transitions
field.public StateMachine(Class<E> clazz, String... rules) throws IllegalArgumentException
IllegalArgumentException
- if the parsing fails, or its result violates the contract of the transitions
field.Method Detail |
---|
private EnumMap<E,EnumSet<E>> checkTransitions(EnumMap<E,EnumSet<E>> transitions) throws IllegalArgumentException
IllegalArgumentException
private EnumMap<E,EnumSet<E>> cloneDeep(EnumMap<E,EnumSet<E>> transitions)
private EnumMap<E,EnumSet<E>> parseTransitions(Class<E> clazz, String... rules) throws IllegalArgumentException
Each element of rules is a state transition rule. Its format is stateInitial followed by a "-->" followed by statesFinal, which must be a comma (',') delimited list of states. Optional whitespace may appear around any token in the above format. Every state in the enum referred to by clazz must have one and only one transition rule in rules.
IllegalArgumentException
- if rules is blank
or some format error occursprivate E parseStateInitial(Class<E> clazz, String rule) throws IllegalArgumentException
IllegalArgumentException
private EnumSet<E> parseStatesFinal(Class<E> clazz, String rule) throws IllegalArgumentException
IllegalArgumentException
public String toString()
transitions
.
Contract: the result must be compatible with parseTransitions(java.lang.Class
, that is,
parseTransitions( stateMachine.toString() )
must always succeed.
toString
in class Object
public boolean isTransitionAllowed(E stateInitial, E stateFinal) throws IllegalArgumentException
IllegalArgumentException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |