KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > groboclown > util > states > v1 > StateSet


1 /*
2  * StateSet.java - 0.9.0 01/13/2001 - 16:51:36
3  *
4  * Copyright (C) 2000,2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.groboclown.util.states.v1;
28
29 import java.util.Vector JavaDoc;
30
31
32 /**
33  * A simple StateMachine where the transition to state mapping is 1-to-1.
34  *
35  * @author Matt Albrecht
36  * @version 0.9.0 Alpha
37  */

38 public class StateSet
39 {
40     //---------------------------------------------------------------------
41
// Public Static Fields
42

43      
44     //---------------------------------------------------------------------
45
// Protected Static Fields
46

47      
48     //---------------------------------------------------------------------
49
// Private Static Fields
50

51      
52     //---------------------------------------------------------------------
53
// Public Fields
54

55      
56     //---------------------------------------------------------------------
57
// Protected Fields
58

59      
60     //---------------------------------------------------------------------
61
// Private Fields
62

63     
64     private StateCategory states = new StateCategory();
65     private Vector JavaDoc statefuls = new Vector JavaDoc();
66     private State currentState = null;
67      
68      
69     //---------------------------------------------------------------------
70
// Constructors
71

72      
73     /**
74      * Default Constructor
75      */

76     public StateSet()
77     {
78         // do nothing
79
}
80      
81      
82      
83      
84      
85     //---------------------------------------------------------------------
86
// Public Methods
87

88     /**
89      *
90      */

91     public State createNextState()
92     {
93         return this.states.getNextState();
94     }
95     
96     
97     /**
98      *
99      */

100     public void addStateful( Stateful s )
101     {
102         if (s == null)
103         {
104             throw new IllegalArgumentException JavaDoc( "no null args" );
105         }
106         s.initialize( this.states );
107         this.statefuls.addElement( s );
108     }
109     
110     
111     /**
112      *
113      */

114     public void removeStateful( Stateful s )
115     {
116         if (s == null)
117         {
118             throw new IllegalArgumentException JavaDoc( "no null args" );
119         }
120         this.statefuls.removeElement( s );
121     }
122     
123     
124     /**
125      *
126      */

127     public void setState( State s )
128     {
129         if (!this.states.isOfCategory( s ))
130         {
131             throw new IllegalStateException JavaDoc("wrong state category");
132         }
133         this.currentState = s;
134         for (int i = this.statefuls.size(); --i >= 0;)
135         {
136             ((Stateful)this.statefuls.elementAt(i)).setState( s );
137         }
138     }
139     
140      
141     //---------------------------------------------------------------------
142
// Protected Methods
143

144      
145     //---------------------------------------------------------------------
146
// Private Methods
147

148      
149 }
150  
151
Popular Tags