1:
30:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36:
37:
43: public final class FastStack implements Serializable, Cloneable
44: {
45: private Object[] contents;
46: private int size;
47: private int initialSize;
48:
49: public FastStack()
50: {
51: initialSize = 10;
52: }
53:
54: public FastStack(int size)
55: {
56: initialSize = Math.max(1, size);
57: }
58:
59: public boolean isEmpty()
60: {
61: return size == 0;
62: }
63:
64: public int size()
65: {
66: return size;
67: }
68:
69: public void push(Object o)
70: {
71: if (contents == null)
72: {
73: contents = new Object[initialSize];
74: contents[0] = o;
75: size = 1;
76: return;
77: }
78:
79: final int oldSize = size;
80: size += 1;
81: if (contents.length == size)
82: {
83:
84: final Object[] newContents = new Object[size + initialSize];
85: System.arraycopy(contents, 0, newContents, 0, size);
86: this.contents = newContents;
87: }
88: this.contents[oldSize] = o;
89: }
90:
91: public Object peek()
92: {
93: if (size == 0)
94: {
95: throw new EmptyStackException();
96: }
97: return contents[size - 1];
98: }
99:
100: public Object pop()
101: {
102: if (size == 0)
103: {
104: throw new EmptyStackException();
105: }
106: size -= 1;
107: final Object retval = contents[size];
108: contents[size] = null;
109: return retval;
110: }
111:
112: public Object clone()
113: {
114: try
115: {
116: FastStack stack = (FastStack) super.clone();
117: if (contents != null)
118: {
119: stack.contents = (Object[]) contents.clone();
120: }
121: return stack;
122: }
123: catch (CloneNotSupportedException cne)
124: {
125: throw new IllegalStateException("Clone not supported? Why?");
126: }
127: }
128:
129: public void clear()
130: {
131: size = 0;
132: if (contents != null)
133: {
134: Arrays.fill(contents, null);
135: }
136: }
137:
138: public Object get(final int index)
139: {
140: if (index >= size)
141: {
142: throw new IndexOutOfBoundsException();
143: }
144: return contents[index];
145: }
146: }