KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.xstream.core.util;
2
3 public final class FastStack {
4
5     private Object JavaDoc[] stack;
6     private int pointer;
7
8     public FastStack(int initialCapacity) {
9         stack = new Object JavaDoc[initialCapacity];
10     }
11
12     public Object JavaDoc push(Object JavaDoc 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 JavaDoc pop() {
25         return stack[--pointer];
26     }
27
28     public Object JavaDoc 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 JavaDoc get(int i) {
41         return stack[i];
42     }
43
44     private void resizeStack(int newCapacity) {
45         Object JavaDoc[] newStack = new Object JavaDoc[newCapacity];
46         System.arraycopy(stack, 0, newStack, 0, Math.min(stack.length, newCapacity));
47         stack = newStack;
48     }
49
50     public String JavaDoc toString() {
51         StringBuffer JavaDoc result = new StringBuffer JavaDoc("[");
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