001: /*
002: * @(#)StateImpl.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.mbtf.v1.engine;
030:
031: import net.sourceforge.groboutils.mbtf.v1.IState;
032: import net.sourceforge.groboutils.mbtf.v1.IValidate;
033: import net.sourceforge.groboutils.mbtf.v1.ITransition;
034:
035: /**
036: * Immutable IState implementation.
037: *
038: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
039: * @version $Date: 2003/02/10 22:52:26 $
040: * @since June 12, 2002
041: */
042: public class StateImpl implements IState {
043: private String name;
044: private ITransition[] trans;
045: private IValidate[] validate;
046:
047: public StateImpl(String name, ITransition[] t, IValidate[] v) {
048: if (name == null) {
049: throw new IllegalArgumentException("no null name");
050: }
051:
052: if (t == null) {
053: t = new ITransition[0];
054: } else {
055: int len = t.length;
056: ITransition[] tt = new ITransition[len];
057: for (int i = len; --i >= 0;) {
058: if (t[i] == null) {
059: throw new IllegalArgumentException(
060: "no nulls allowed in ITransition array (index "
061: + i + ")");
062: }
063: // else
064: tt[i] = t[i];
065: }
066: t = tt;
067: }
068:
069: if (v == null) {
070: v = new IValidate[0];
071: } else {
072: int len = v.length;
073: IValidate[] vv = new IValidate[len];
074: for (int i = len; --i >= 0;) {
075: if (v[i] == null) {
076: throw new IllegalArgumentException(
077: "no nulls allowed in IValidate array (index "
078: + i + ")");
079: }
080: // else
081: vv[i] = v[i];
082: }
083: v = vv;
084: }
085:
086: this .name = name;
087: this .trans = t;
088: this .validate = v;
089: }
090:
091: /**
092: * Retrieves the name for the state. This should be unique for
093: * state-machine assembly purposes, but it does not have to be.
094: *
095: * @return a non-null name for this state.
096: */
097: public String getName() {
098: return this .name;
099: }
100:
101: /**
102: * Retrieves the list of transitions leading from this state.
103: *
104: * @return a non-null list of all transitions from this state. If the
105: * length of the list is 0, then this is a terminal state.
106: */
107: public ITransition[] getTransitions() {
108: int len = this .trans.length;
109: ITransition t[] = new ITransition[len];
110: System.arraycopy(this .trans, 0, t, 0, len);
111: return t;
112: }
113:
114: /**
115: * Retrieves the list of all validation instances used to assert that
116: * the current state is valid.
117: *
118: * @return a non-null list of all validation instances for this state.
119: * An empty list will cause a warning on all but the first
120: * (initial) state. This list should not include the included
121: * Transitions' validates.
122: */
123: public IValidate[] getValidates() {
124: int len = this .validate.length;
125: IValidate v[] = new IValidate[len];
126: System.arraycopy(this .validate, 0, v, 0, len);
127: return v;
128: }
129:
130: public String toString() {
131: return "[State " + getName() + "]";
132: }
133: }
|