1 package com.thoughtworks.xstream.core.util; 2 3 public final class FastStack { 4 5 private Object [] stack; 6 private int pointer; 7 8 public FastStack(int initialCapacity) { 9 stack = new Object [initialCapacity]; 10 } 11 12 public Object push(Object value) { 13 if (pointer + 1 >= stack.length) { 14 resizeStack(stack.length * 2); 15 } 16 stack[pointer++] = value; 17 return value; 18 } 19 20 public void popSilently() { 21 pointer--; 22 } 23 24 public Object pop() { 25 return stack[--pointer]; 26 } 27 28 public Object peek() { 29 return pointer == 0 ? null : stack[pointer - 1]; 30 } 31 32 public int size() { 33 return pointer; 34 } 35 36 public boolean hasStuff() { 37 return pointer > 0; 38 } 39 40 public Object get(int i) { 41 return stack[i]; 42 } 43 44 private void resizeStack(int newCapacity) { 45 Object [] newStack = new Object [newCapacity]; 46 System.arraycopy(stack, 0, newStack, 0, Math.min(stack.length, newCapacity)); 47 stack = newStack; 48 } 49 50 public String toString() { 51 StringBuffer result = new StringBuffer ("["); 52 for (int i = 0; i < pointer; i++) { 53 if (i > 0) { 54 result.append(", "); 55 } 56 result.append(stack[i]); 57 } 58 result.append(']'); 59 return result.toString(); 60 } 61 } 62 | Popular Tags |