001: /*
002: * StateCategory.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: /**
030: * Creates new categories for State collections. Does not save the
031: * created state objects. To save time, this creates both
032: * States and Transitions, though it should only be used for one or
033: * the other.
034: *
035: * @author Matt Albrecht
036: * @version 0.9.0 Alpha
037: */
038: public class StateCategory {
039: //---------------------------------------------------------------------
040: // Public Static Fields
041:
042: //---------------------------------------------------------------------
043: // Protected Static Fields
044:
045: //---------------------------------------------------------------------
046: // Private Static Fields
047:
048: private final static Object sync = new Object();
049: private static int categoryNum = 0;
050:
051: //---------------------------------------------------------------------
052: // Public Fields
053:
054: //---------------------------------------------------------------------
055: // Protected Fields
056:
057: //---------------------------------------------------------------------
058: // Private Fields
059:
060: private int category;
061: private int stateNum = 0;
062: private int maxStates;
063: private final Object m_sync = new Object();
064:
065: //---------------------------------------------------------------------
066: // Constructors
067:
068: /**
069: * Default Constructor
070: */
071: public StateCategory(int maxStateCount) {
072: this .maxStates = maxStateCount;
073: this .category = getNextCategoryInt();
074: }
075:
076: //---------------------------------------------------------------------
077: // Public Methods
078:
079: /**
080: * Creates a new unique State instance for this category.
081: */
082: public State getNextState() {
083: State s = new State(this .category, getNextStateInt());
084:
085: return s;
086: }
087:
088: /**
089: * Creates a new unique Transition instance for this category.
090: */
091: public Transition getNextTransition() {
092: Transition s = new Transition(this .category, getNextStateInt());
093:
094: return s;
095: }
096:
097: /**
098: * Checks if the given state is of this category.
099: */
100: public boolean isOfCategory(State s) {
101: return (s.getCategory() == this .category);
102: }
103:
104: /**
105: *
106: */
107: public int getMaximumStateCount() {
108: return this .maxStates;
109: }
110:
111: //---------------------------------------------------------------------
112: // Protected Methods
113:
114: //---------------------------------------------------------------------
115: // Private Methods
116:
117: private final int getNextStateInt() {
118: int val;
119: synchronized (this .m_sync) {
120: val = this .stateNum++;
121: }
122: if (val >= this .maxStates) {
123: throw new IllegalStateException("created too many states");
124: }
125: return val;
126: }
127:
128: //---------------------------------------------------------------------
129: // Private Static Methods
130:
131: private static final int getNextCategoryInt() {
132: int val;
133: synchronized (sync) {
134: val = categoryNum++;
135: }
136: return val;
137: }
138: }
|