001: /*
002: * @(#)AsmblTransition.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.assembler;
030:
031: import net.sourceforge.groboutils.mbtf.v1.IState;
032: import net.sourceforge.groboutils.mbtf.v1.ITransition;
033: import net.sourceforge.groboutils.mbtf.v1.IValidate;
034: import net.sourceforge.groboutils.mbtf.v1.IAction;
035:
036: import net.sourceforge.groboutils.mbtf.v1.engine.TransitionImpl;
037:
038: import java.util.Vector;
039:
040: /**
041: *
042: *
043: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
044: * @version $Date: 2003/02/10 22:52:25 $
045: * @since June 12, 2002
046: */
047: public class AsmblTransition {
048: private String name;
049: private Vector validates = new Vector();
050: private IAction action;
051: private String nextState;
052:
053: private TransitionImpl iTrans;
054:
055: /**
056: *
057: */
058: public AsmblTransition() {
059: // do nothing
060: }
061:
062: public void setName(String name) {
063: if (name == null) {
064: throw new IllegalArgumentException("no null args");
065: }
066:
067: // invalidates our transition
068: resetTransition(true);
069:
070: this .name = name;
071: }
072:
073: public String getName() {
074: return this .name;
075: }
076:
077: public void setNextStateName(String name) {
078: if (name == null) {
079: throw new IllegalArgumentException("no null args");
080: }
081:
082: // invalidates our transition, and its state (if any)
083: resetTransition(false);
084:
085: this .nextState = name;
086: }
087:
088: public String getNextStateName() {
089: return this .nextState;
090: }
091:
092: public void addValidate(IValidate v) {
093: if (v != null) {
094: // invalidates our transition
095: resetTransition(true);
096:
097: this .validates.addElement(v);
098: }
099: }
100:
101: public void setAction(IAction a) {
102: if (a == null) {
103: throw new IllegalArgumentException("no null args");
104: }
105:
106: // invalidates our transition
107: resetTransition(true);
108:
109: this .action = a;
110: }
111:
112: public IValidate[] getValidates() {
113: IValidate v[] = new IValidate[this .validates.size()];
114: this .validates.copyInto(v);
115: return v;
116: }
117:
118: public IAction getAction() {
119: return this .action;
120: }
121:
122: public ITransition getTransition() {
123: if (this .iTrans == null) {
124: this .iTrans = new TransitionImpl(getName(), null,
125: getAction(), getValidates());
126: }
127: return this .iTrans;
128: }
129:
130: public void setDestinationState(IState state) {
131: // ensure the transition is created...
132: getTransition();
133:
134: // make sure the state is correct
135: if (state == null) {
136: throw new IllegalArgumentException("no null args");
137: }
138: if (!state.getName().equals(getNextStateName())) {
139: throw new IllegalArgumentException("Invalid state name");
140: }
141:
142: // only set the destination if we can.
143: if (!this .iTrans.isDestinationStateSet()) {
144: this .iTrans.setDestinationState(state);
145: }
146: }
147:
148: protected void resetTransition(boolean saveState) {
149: if (saveState && this .iTrans != null
150: && this .iTrans.isDestinationStateSet()) {
151: throw new IllegalStateException("Cannot reset transition");
152: } else {
153: this.iTrans = null;
154: }
155: }
156: }
|