001: /*
002: * StateSet.java - 0.9.0 01/13/2001 - 16:51:36
003: *
004: * Copyright (C) 2000,2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.groboclown.util.states.v1;
028:
029: import java.util.Vector;
030:
031: /**
032: * A simple StateMachine where the transition to state mapping is 1-to-1.
033: *
034: * @author Matt Albrecht
035: * @version 0.9.0 Alpha
036: */
037: public class StateSet {
038: //---------------------------------------------------------------------
039: // Public Static Fields
040:
041: //---------------------------------------------------------------------
042: // Protected Static Fields
043:
044: //---------------------------------------------------------------------
045: // Private Static Fields
046:
047: //---------------------------------------------------------------------
048: // Public Fields
049:
050: //---------------------------------------------------------------------
051: // Protected Fields
052:
053: //---------------------------------------------------------------------
054: // Private Fields
055:
056: private StateCategory states = new StateCategory();
057: private Vector statefuls = new Vector();
058: private State currentState = null;
059:
060: //---------------------------------------------------------------------
061: // Constructors
062:
063: /**
064: * Default Constructor
065: */
066: public StateSet() {
067: // do nothing
068: }
069:
070: //---------------------------------------------------------------------
071: // Public Methods
072:
073: /**
074: *
075: */
076: public State createNextState() {
077: return this .states.getNextState();
078: }
079:
080: /**
081: *
082: */
083: public void addStateful(Stateful s) {
084: if (s == null) {
085: throw new IllegalArgumentException("no null args");
086: }
087: s.initialize(this .states);
088: this .statefuls.addElement(s);
089: }
090:
091: /**
092: *
093: */
094: public void removeStateful(Stateful s) {
095: if (s == null) {
096: throw new IllegalArgumentException("no null args");
097: }
098: this .statefuls.removeElement(s);
099: }
100:
101: /**
102: *
103: */
104: public void setState(State s) {
105: if (!this .states.isOfCategory(s)) {
106: throw new IllegalStateException("wrong state category");
107: }
108: this .currentState = s;
109: for (int i = this .statefuls.size(); --i >= 0;) {
110: ((Stateful) this .statefuls.elementAt(i)).setState(s);
111: }
112: }
113:
114: //---------------------------------------------------------------------
115: // Protected Methods
116:
117: //---------------------------------------------------------------------
118: // Private Methods
119:
120: }
|