| Method from org.jboss.util.state.DefaultStateMachineModel Detail: |
public Set acceptableStates(State state) {
Entry entry = getEntry(state);
if (entry.acceptableStates != null)
return Collections.unmodifiableSet(entry.acceptableStates);
return null;
}
|
public Set addState(State state) {
return addState(state, (Set)null);
}
|
public Set addState(State state,
Set acceptable) {
Entry prevEntry = getEntry(state);
// If we will replace the state, do some clean up before
if (containsState(state)) {
// update mapping to reflect new state value
updateAcceptableMapping(state, false);
}
// Now replace it
putEntry(state, acceptable);
if (acceptable != null) {
// Sanity check acceptable states
Iterator iter = acceptable.iterator();
while (iter.hasNext()) {
State temp = (State)iter.next();
if (temp == null)
throw new NullArgumentException("acceptable", "Set Element");
if (temp.equals(state)) {
throw new IllegalArgumentException
("Acceptable states must not include the accepting state: " + temp);
}
// Add final states for all non-existant states
if (!containsState(temp)) {
addState(temp);
}
}
}
return prevEntry == null ? null : prevEntry.acceptableStates;
}
|
public Set addState(State state,
State[] acceptable) {
if (acceptable == null)
throw new NullArgumentException("acceptable");
if (acceptable.length == 0) {
return addState(state, (Set)null);
}
HashSet set = new HashSet(acceptable.length);
for (int i=0; i< acceptable.length; i++) {
if (acceptable[i] == null)
throw new NullArgumentException("acceptable", i);
set.add(acceptable[i]);
}
return addState(state, set);
}
|
public Set addState(State state,
State acceptable) {
HashSet set = new HashSet(1);
set.add(acceptable);
return addState(state, set);
}
|
public StringBuffer appendPrettyString(StringBuffer buff,
String prefix) {
buff.append(prefix).append(super.toString()).append(" {").append("\n");
buff.append(prefix).append(" Accepting state mappings:\n");
Iterator iter = acceptingMap.keySet().iterator();
while (iter.hasNext()) {
buff.append(prefix).append(" ").append(acceptingMap.get((State)iter.next()));
buff.append("\n");
}
buff.append(prefix).append(" Initial state: ")
.append(initial == null ? null : initial.state)
.append("\n");
buff.append(prefix).append(" Current state: ")
.append(current == null ? null : current.state)
.append("\n");
buff.append(prefix).append("}");
return buff;
}
|
public void clear() {
acceptingMap.clear();
initial = null;
current = null;
}
|
public Object clone() {
// clone one level deeper so that the model can be used as a prototype.
DefaultStateMachineModel model = (DefaultStateMachineModel)super.clone();
model.acceptingMap = new HashMap();
// Need to make sure that value entries are cloned too
Iterator iter = acceptingMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
model.acceptingMap.put(entry.getKey(),
((Entry)entry.getValue()).clone());
}
if (model.current != null)
model.current = (Entry)current.clone();
if (model.initial != null)
model.initial = (Entry)initial.clone();
return model;
}
|
public boolean containsState(State state) {
return acceptingMap.containsKey(state);
}
|
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj != null && obj.getClass() == getClass()) {
DefaultStateMachineModel model = (DefaultStateMachineModel)obj;
return
((current == model.current) ||
(current != null && current.equals(model.current))) &&
((initial == model.initial) ||
(initial != null && initial.equals(model.initial))) &&
((acceptingMap == model.acceptingMap) ||
(acceptingMap != null && acceptingMap.equals(model.acceptingMap)));
}
return false;
}
|
public State getCurrentState() {
return current != null ? current.state : null;
}
|
protected DefaultStateMachineModel.Entry getEntry(State state) {
if (state == null)
throw new NullArgumentException("state");
return (Entry)acceptingMap.get(state);
}
Get an entry from the map. |
public State getInitialState() {
return initial != null ? initial.state : null;
}
|
public State getMappedState(State state) {
Entry entry = getEntry(state);
if (entry != null)
return entry.state;
throw new IllegalArgumentException("State not mapped: " + state);
}
|
public boolean isMappedState(State state) {
if (state == null) return false;
return getEntry(state) != null;
}
|
protected DefaultStateMachineModel.Entry putEntry(State state,
Set acceptable) {
return (Entry)acceptingMap.put(state, new Entry(state, acceptable));
}
Put a new entry into the map. |
public Set removeState(State state) {
if (state == null)
throw new NullArgumentException("state");
if (current != null && state.equals(current.state))
throw new IllegalArgumentException
("Can not remove current state: " + state);
Entry prevEntry = getEntry(state);
// remove the mappings for this state
updateAcceptableMapping(state, true);
// Finally remove it
acceptingMap.remove(state);
return prevEntry.acceptableStates;
}
|
public void setCurrentState(State state) {
Entry entry = getEntry(state);
if (entry == null)
throw new IllegalArgumentException("State not mapped: " + state);
this.current = entry;
}
|
public void setInitialState(State state) {
Entry entry = getEntry(state);
if (entry == null)
throw new IllegalArgumentException("State not mapped: " + state);
this.initial = entry;
}
|
public Set states() {
return Collections.unmodifiableSet(acceptingMap.keySet());
}
|
public String toString() {
return appendPrettyString(new StringBuffer(), "").toString();
}
|
protected void updateAcceptableMapping(State state,
boolean remove) {
Iterator iter = acceptingMap.entrySet().iterator();
while (iter.hasNext()) {
Entry entry = (Entry)((Map.Entry)iter.next()).getValue();
// only attempt to update non-final states
if (entry.acceptableStates != null && entry.acceptableStates.contains(state)) {
entry.acceptableStates.remove(state);
if (!remove) {
entry.acceptableStates.add(state);
}
}
}
}
Update acceptable mappings. |