|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectbb.util.State
public class State
This class is meant to represent named states. An instance is constructed with an array of all possible states that it can be in, and it can be assigned any state in this array by various method calls. It is useful for writing simple software state machines.
This class is multithread safe: all public methods are synchronized.
Field Summary | |
---|---|
protected int |
index
|
protected String[] |
states
Contract: is never null, zero length, contains a blank element, or contains duplicate elements. |
Constructor Summary | |
---|---|
State(String[] states)
|
|
State(String[] states,
int index)
|
Method Summary | |
---|---|
void |
decrement()
Moves to the previous state (wrapping around to the end of the states array if are currently at beginning). |
protected int |
findIndex(String state)
Returns the index of the element within states which equals state, else returns -1 if no such element exists. |
String |
get()
Returns the current state. |
String[] |
getStates()
Returns a clone of all the potential states of this State instance. |
void |
increment()
Moves to the next state (wrapping around to the beginning of the states array if are currently at end). |
boolean |
is(String state)
Returns whether or not state equals this instance's current state. |
boolean |
knows(String state)
Returns whether or not this instance knows of state as a valid state that it can reach. |
void |
set(int indexNew)
Adjusts the internal index to the specified index, which changes the state to the one at that index. |
void |
set(String state)
Moves to the specified state. |
String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected final String[] states
protected int index
Constructor Detail |
---|
public State(String[] states) throws IllegalArgumentException
IllegalArgumentException
public State(String[] states, int index) throws IllegalArgumentException
IllegalArgumentException
Method Detail |
---|
public String toString()
toString
in class Object
public boolean knows(String state)
public boolean is(String state)
public String get()
public String[] getStates()
public void set(int indexNew) throws IllegalArgumentException
This is the fundamental state changing method. Subclasses may override in order to enforce custom state transition rules.
IllegalArgumentException
- if indexNew is not a valid valuepublic void set(String state) throws IllegalArgumentException
IllegalArgumentException
- if state is not a valid state for this State instancepublic void increment()
public void decrement()
protected int findIndex(String state)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |