1 28 29 package org.jibx.binding.util; 30 31 import java.lang.reflect.Array ; 32 33 43 44 public class ObjectStack 45 { 46 47 public static final int DEFAULT_SIZE = 8; 48 49 50 protected int m_countLimit; 51 52 53 protected int m_countPresent; 54 55 56 protected int m_maximumGrowth; 57 58 59 protected Object [] m_baseArray; 60 61 68 69 public ObjectStack(int size, int growth) { 70 m_countLimit = size; 71 m_maximumGrowth = growth; 72 m_baseArray = new Object [size]; 73 } 74 75 81 82 public ObjectStack(int size) { 83 this(size, Integer.MAX_VALUE); 84 } 85 86 89 90 public ObjectStack() { 91 this(DEFAULT_SIZE); 92 } 93 94 99 100 public ObjectStack(ObjectStack base) { 101 this(base.m_countLimit, base.m_maximumGrowth); 102 System.arraycopy(base.m_baseArray, 0, m_baseArray, 0, 103 base.m_countPresent); 104 m_countPresent = base.m_countPresent; 105 } 106 107 112 113 public ObjectStack(Object [] strings) { 114 this(strings.length); 115 System.arraycopy(strings, 0, m_baseArray, 0, strings.length); 116 m_countPresent = strings.length; 117 } 118 119 127 128 private void resizeCopy(Object base, Object grown) { 129 System.arraycopy(base, 0, grown, 0, Array.getLength(base)); 130 } 131 132 140 141 private void discardValues(int from, int to) { 142 for (int i = from; i < to; i++) { 143 m_baseArray[i] = null; 144 } 145 } 146 147 158 159 private void growArray(int required) { 160 int size = Math.max(required, 161 m_countLimit + Math.min(m_countLimit, m_maximumGrowth)); 162 Object [] grown = new Object [size]; 163 resizeCopy(m_baseArray, grown); 164 m_countLimit = size; 165 m_baseArray = grown; 166 } 167 168 174 175 public final void ensureCapacity(int min) { 176 if (min > m_countLimit) { 177 growArray(min); 178 } 179 } 180 181 186 187 public void push(Object value) { 188 int index = getAddIndex(); 189 m_baseArray[index] = value; 190 } 191 192 198 199 public Object pop() { 200 if (m_countPresent > 0) { 201 Object value = m_baseArray[--m_countPresent]; 202 m_baseArray[m_countPresent] = null; 203 return value; 204 } else { 205 throw new ArrayIndexOutOfBoundsException 206 ("Attempt to pop empty stack"); 207 } 208 } 209 210 220 221 public Object pop(int count) { 222 if (count < 0) { 223 throw new IllegalArgumentException ("Count must be greater than 0"); 224 } else if (m_countPresent >= count) { 225 m_countPresent -= count; 226 Object value = m_baseArray[m_countPresent]; 227 discardValues(m_countPresent, m_countPresent + count); 228 return value; 229 } else { 230 throw new ArrayIndexOutOfBoundsException 231 ("Attempt to pop past end of stack"); 232 } 233 } 234 235 244 245 public Object peek(int depth) { 246 if (m_countPresent > depth) { 247 return m_baseArray[m_countPresent - depth - 1]; 248 } else { 249 throw new ArrayIndexOutOfBoundsException 250 ("Attempt to peek past end of stack"); 251 } 252 } 253 254 261 262 public Object peek() { 263 return peek(0); 264 } 265 266 274 275 public Object [] toArray() { 276 Object [] copy = new Object [m_countPresent]; 277 System.arraycopy(m_baseArray, 0, copy, 0, m_countPresent); 278 return copy; 279 } 280 281 286 287 public Object clone() { 288 return new ObjectStack(this); 289 } 290 291 299 300 private int getAddIndex() { 301 int index = m_countPresent++; 302 if (m_countPresent > m_countLimit) { 303 growArray(m_countPresent); 304 } 305 return index; 306 } 307 308 313 public int size() { 314 return m_countPresent; 315 } 316 317 322 323 public boolean isEmpty() { 324 return m_countPresent == 0; 325 } 326 327 330 331 public void clear() { 332 discardValues(0, m_countPresent); 333 m_countPresent = 0; 334 } 335 } | Popular Tags |