1 26 27 package net.groboclown.util.states.v1; 28 29 30 31 40 public class StateCategory 41 { 42 45 46 49 50 53 private final static Object sync = new Object (); 54 private static int categoryNum = 0; 55 56 57 60 61 64 65 68 private int category; 69 private int stateNum = 0; 70 private int maxStates; 71 private final Object m_sync = new Object (); 72 73 74 75 76 79 80 83 public StateCategory( int maxStateCount ) 84 { 85 this.maxStates = maxStateCount; 86 this.category = getNextCategoryInt(); 87 } 88 89 90 91 92 93 96 99 public State getNextState() 100 { 101 State s = new State( this.category, getNextStateInt() ); 102 103 return s; 104 } 105 106 107 110 public Transition getNextTransition() 111 { 112 Transition s = new Transition( this.category, getNextStateInt() ); 113 114 return s; 115 } 116 117 118 121 public boolean isOfCategory( State s ) 122 { 123 return (s.getCategory() == this.category); 124 } 125 126 127 130 public int getMaximumStateCount() 131 { 132 return this.maxStates; 133 } 134 135 136 139 140 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 ("created too many states"); 153 } 154 return val; 155 } 156 157 158 159 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 |