KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Stateful.java - 0.9.0 01/13/2001 - 17:13:37
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
30
31 /**
32  *
33  *
34  * @author Matt Albrecht
35  * @version 0.9.0 Alpha
36  */

37 public class Stateful
38 {
39     //---------------------------------------------------------------------
40
// Public Static Fields
41

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

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

50     
51     //---------------------------------------------------------------------
52
// Public Fields
53

54     
55     //---------------------------------------------------------------------
56
// Protected Fields
57

58     
59     //---------------------------------------------------------------------
60
// Private Fields
61

62     private IStatefulListener listener;
63     private StateCategory category;
64     private Object JavaDoc[] actions = null;
65     private int currentState = -1;
66     private Object JavaDoc currentAction = null;
67     
68     
69     //---------------------------------------------------------------------
70
// Constructors
71

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

76     public Stateful( IStatefulListener listener )
77     {
78         if (listener == null)
79         {
80             throw new IllegalArgumentException JavaDoc("no null args" );
81         }
82         this.listener = listener;
83     }
84     
85     
86     
87     
88     
89     //---------------------------------------------------------------------
90
// Public Methods
91

92     
93     /**
94      *
95      */

96     public void addStateAction( State s, Object JavaDoc action )
97     {
98         if (this.category == null)
99         {
100             throw new IllegalStateException JavaDoc(
101                 "Stateful never intialized - you must add it to a set "+
102                 "before adding State Actions" );
103         }
104         if (!this.category.isOfCategory( s ))
105         {
106             throw new IllegalStateException JavaDoc(
107                 "State object not of state category");
108         }
109         this.actions[ s.getIndex() ] = action;
110         
111         if (s.getIndex() == this.currentState &&
112             action != this.currentAction)
113         {
114             // The current state's action has changed
115
this.listener.setAction( action );
116             this.currentAction = action;
117         }
118     }
119     
120     /**
121      *
122      */

123     public void removeStateAction( State s )
124     {
125         addStateAction( s, null );
126     }
127     
128     /**
129      * Only calls the listener's setAction if the new state is different
130      * than the current state, and the new state's action object is
131      * different than the current action object.
132      */

133     public void setState( State s )
134     {
135         if (this.category == null)
136         {
137             throw new IllegalStateException JavaDoc(
138                 "Stateful never intialized - you must add it to a set "+
139                 "before adding State Actions" );
140         }
141         if (!this.category.isOfCategory( s ))
142         {
143             throw new IllegalStateException JavaDoc(
144                 "State object not of state category");
145         }
146         
147         //
148
// perform a series of checks to see if we need to
149
// set this state.
150
//
151

152         // check if the state is identical to the current state
153
int si = s.getIndex();
154         if (si == this.currentState)
155         {
156             // do nothing - we're on the same state as we were
157
return;
158         }
159         this.currentState = si;
160         
161         // now check if the new state's action object is identical
162
// to the current action object.
163
Object JavaDoc a = actions[ si ];
164         if (this.currentAction == a)
165         {
166             // do nothing - the state changed, but the action didn't
167
return;
168         }
169         this.currentAction = a;
170         
171         // we can set the state
172
this.listener.setAction( actions[ s.getIndex() ] );
173     }
174     
175     //---------------------------------------------------------------------
176
// Protected Methods
177

178     
179     protected void initialize( StateCategory s )
180     {
181         if (this.actions != null)
182         {
183             throw new IllegalStateException JavaDoc("Stateful already initialized");
184         }
185         this.actions = new Object JavaDoc[ s.getMaximumStateCount() ];
186         this.category = s;
187     }
188     
189     //---------------------------------------------------------------------
190
// Private Methods
191

192     
193 }
194  
195
Popular Tags