View Javadoc

1   /*   Copyright 2004 BEA Systems, Inc.
2    *
3    *   Licensed under the Apache License, Version 2.0 (the "License");
4    *   you may not use this file except in compliance with the License.
5    *   You may obtain a copy of the License at
6    *
7    *       http://www.apache.org/licenses/LICENSE-2.0
8    *
9    *   Unless required by applicable law or agreed to in writing, software
10   *   distributed under the License is distributed on an "AS IS" BASIS,
11   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   *   See the License for the specific language governing permissions and
13   *   limitations under the License.
14   */
15  
16  package com.bea.xml.stream.util;
17  
18  import java.util.AbstractCollection;
19  import java.util.EmptyStackException;
20  import java.util.Iterator;
21  
22  public final class Stack extends AbstractCollection {
23    private Object[] values;
24    private int pointer;
25  
26    public Stack() {
27      this(15);
28    }
29  
30    public Stack(int size) {
31      if (size < 0) throw new IllegalArgumentException();
32      values = new Object[size];
33      pointer = 0;
34    }
35  
36    private Stack(Object[] values, int pointer) {
37      this.values = values;
38      this.pointer = pointer;
39    }
40  
41    private void resize() {
42      if (pointer == 0) {
43        values = new Object[1];
44        return;
45      }
46      Object[] o = new Object[pointer * 2];
47      System.arraycopy(values, 0, o, 0, pointer);
48      values = o;
49    }
50  
51    public boolean add(Object o) {
52      push(o);
53      return true;
54    }
55  
56    public void clear() {
57      Object[] v = values;
58      while (pointer > 0 ) {
59        v[--pointer] = null;
60      }
61    }
62  
63    public boolean isEmpty() { return pointer == 0; }
64  
65    public Iterator iterator() {
66      Object[] o = new Object[pointer];
67      System.arraycopy(values, 0, o, 0, pointer);
68      return new ArrayIterator(o);
69    }
70  
71    public Object clone() {
72      Object[] newValues = new Object[pointer];
73      System.arraycopy(values, 0, newValues, 0, pointer);
74      return new Stack(newValues, pointer); 
75    }
76  
77    public int size() { return pointer; }
78  
79    public void push(Object o) {
80      if (pointer == values.length) resize();
81      values[pointer++] = o;
82    }
83  
84    public Object pop() {
85      try {
86        Object o = values[--pointer];
87        values[pointer] = null;
88        return o;
89      } catch (ArrayIndexOutOfBoundsException aioobe) {
90        // Make sure the stack continues to be useable even if we popped
91        // too many.
92        if (pointer < 0) pointer = 0;
93        throw new EmptyStackException();
94      }
95    }
96  
97    public Object peek() {
98      try {
99        return values[pointer - 1];
100     } catch (ArrayIndexOutOfBoundsException aioobe) {
101       throw new EmptyStackException();
102     }
103   }
104 }