KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * StateCategory.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
30
31 /**
32  * Creates new categories for State collections. Does not save the
33  * created state objects. To save time, this creates both
34  * States and Transitions, though it should only be used for one or
35  * the other.
36  *
37  * @author Matt Albrecht
38  * @version 0.9.0 Alpha
39  */

40 public class StateCategory
41 {
42     //---------------------------------------------------------------------
43
// Public Static Fields
44

45      
46     //---------------------------------------------------------------------
47
// Protected Static Fields
48

49      
50     //---------------------------------------------------------------------
51
// Private Static Fields
52

53     private final static Object JavaDoc sync = new Object JavaDoc();
54     private static int categoryNum = 0;
55      
56      
57     //---------------------------------------------------------------------
58
// Public Fields
59

60      
61     //---------------------------------------------------------------------
62
// Protected Fields
63

64      
65     //---------------------------------------------------------------------
66
// Private Fields
67

68     private int category;
69     private int stateNum = 0;
70     private int maxStates;
71     private final Object JavaDoc m_sync = new Object JavaDoc();
72      
73      
74      
75      
76     //---------------------------------------------------------------------
77
// Constructors
78

79      
80     /**
81      * Default Constructor
82      */

83     public StateCategory( int maxStateCount )
84     {
85         this.maxStates = maxStateCount;
86         this.category = getNextCategoryInt();
87     }
88     
89     
90     
91     
92     
93     //---------------------------------------------------------------------
94
// Public Methods
95

96     /**
97      * Creates a new unique State instance for this category.
98      */

99     public State getNextState()
100     {
101         State s = new State( this.category, getNextStateInt() );
102         
103         return s;
104     }
105     
106      
107     /**
108      * Creates a new unique Transition instance for this category.
109      */

110     public Transition getNextTransition()
111     {
112         Transition s = new Transition( this.category, getNextStateInt() );
113         
114         return s;
115     }
116     
117     
118     /**
119      * Checks if the given state is of this category.
120      */

121     public boolean isOfCategory( State s )
122     {
123         return (s.getCategory() == this.category);
124     }
125     
126     
127     /**
128      *
129      */

130     public int getMaximumStateCount()
131     {
132         return this.maxStates;
133     }
134     
135      
136     //---------------------------------------------------------------------
137
// Protected Methods
138

139      
140     //---------------------------------------------------------------------
141
// Private Methods
142

143     private final int getNextStateInt()
144     {
145         int val;
146         synchronized( this.m_sync )
147         {
148             val = this.stateNum++;
149         }
150         if (val >= this.maxStates)
151         {
152             throw new IllegalStateException JavaDoc("created too many states");
153         }
154         return val;
155     }
156      
157      
158      
159     //---------------------------------------------------------------------
160
// Private Static Methods
161

162     private static final int getNextCategoryInt()
163     {
164         int val;
165         synchronized (sync)
166         {
167             val = categoryNum++;
168         }
169         return val;
170     }
171 }
172  
173
Popular Tags