1 package com.thoughtworks.xstream.core.util; 2 3 public final class StringStack { 4 5 private String [] stack; 6 private int pointer; 7 8 public StringStack(int initialCapacity) { 9 stack = new String [initialCapacity]; 10 } 11 12 public void push(String value) { 13 if (pointer + 1 >= stack.length) { 14 resizeStack(stack.length * 2); 15 } 16 stack[pointer++] = value; 17 } 18 19 public void popSilently() { 20 pointer--; 21 } 22 23 public String pop() { 24 return stack[--pointer]; 25 } 26 27 public String peek() { 28 return pointer == 0 ? null : stack[pointer - 1]; 29 } 30 31 public int size() { 32 return pointer; 33 } 34 35 public String get(int i) { 36 return stack[i]; 37 } 38 39 private void resizeStack(int newCapacity) { 40 String [] newStack = new String [newCapacity]; 41 System.arraycopy(stack, 0, newStack, 0, Math.min(stack.length, newCapacity)); 42 stack = newStack; 43 } 44 45 } 46 | Popular Tags |