KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > core > util > StringStack


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