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