View Javadoc

1   /*
2    * Copyright (c) 2001, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   */
18  
19  package net.sf.zel.vm;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.Iterator;
24  
25  
26  public class SimpleStack<T>
27          implements Collection<T>
28  {
29  
30      /**
31       * the stack storage
32       */
33      protected T[] elems;
34      /**
35       * the top element position
36       */
37      protected int top;
38      /**
39       * stack default initial size
40       */
41      protected static final int defaultSize = 32;
42  
43  
44      /**
45       * construct a stack with specified max size
46       * @param max int
47       */
48      public SimpleStack(int max)
49      {
50  
51          elems = (T[]) new Object[max];
52          top = 0;
53      }
54  
55      /**
56       * Construct a stack, default size is 20
57       */
58      public SimpleStack()
59      {
60          this(defaultSize);
61      }
62  
63      /**
64       * check if stack is empty
65       * @return boolean
66       */
67      public boolean isEmpty()
68      {
69          return top == 0;
70      }
71  
72      /**
73       * push object into stack
74       * @param o Object
75       */
76      public void push(T o)
77      {
78          if (top >= elems.length)
79          {
80              final T[] nObj = (T[]) new Object[elems.length << 1];
81              System.arraycopy(elems, 0, nObj, 0, elems.length);
82              elems = nObj;
83          }
84          elems[top++] = o;
85      }
86  
87      /**
88       * Push more objects into the stack
89       * @param args
90       */
91  
92      public void pushAll(T[] args)
93      {
94          if (args.length == 0)
95              return;
96          int newTop = top + args.length;
97          if (newTop >= elems.length)
98          {
99              final T[] nObj = (T[]) new Object[newTop << 1];
100             System.arraycopy(elems, 0, nObj, 0, elems.length);
101             elems = nObj;
102         }
103         System.arraycopy(args, 0, elems, top, args.length);
104         top=newTop;
105     }
106 
107     /**
108      * pops object out of stack
109      * @return Object
110      */
111     public T pop()
112     {
113         final T o = elems[top - 1];
114         top--;
115         // this is for the garbage collector  to avoid memory leaks if storing big objects in stack
116         elems[top] = null;
117         return o;
118     }
119 
120     /**
121      * take a look at the top of stack
122      * @return Object
123      */
124     public T peek()
125     {
126         return elems[top - 1];
127     }
128 
129     /**
130      * Clear the stack - also makes sure the stack objects are not referenced anymore
131      */
132     public void clear()
133     {
134         for (int i = 0; i < top; i++)
135         {
136             elems[i] = null;
137         }
138         top = 0;
139     }
140 
141     /**
142      * get the curent stack pos relative to base
143      * @return
144      */
145     public int getPtr()
146     {
147         return top;
148     }
149 
150     /**
151      * get element from stack at index relative to base
152      * @param ptr
153      * @return
154      */    
155     public T getFromPtr(int ptr)
156     {
157         if (ptr < 0 || ptr >= top)
158             throw new IndexOutOfBoundsException("Trying to get from invalid index: " + ptr + " from: " + this.toString());
159         return elems[ptr];
160     }
161     
162     /**
163      * returns a character separated string with the stack elements
164      * @param separator 
165      * @return String
166      */
167     public String toString(char separator)
168     {
169         if (top == 0)
170         {
171             return "";
172         }
173         final StringBuilder result = new StringBuilder(32);
174         result.append(elems[0].toString());
175         for (int i = 1; i < top; i++)
176         {
177             result.append(separator);
178             result.append(elems[i].toString());
179         }
180         return result.toString();
181     }
182 
183     @Override
184     public String toString()
185     {
186         return toString('.');
187     }
188 
189     @Override
190     public int size()
191     {
192         return top;
193     }
194 
195     @Override
196     public boolean contains(Object o)
197     {
198         throw new UnsupportedOperationException("Not supported yet.");
199     }
200 
201     @Override
202     public Iterator<T> iterator()
203     {
204         throw new UnsupportedOperationException("Not supported yet.");
205     }
206 
207     @Override
208     public Object[] toArray()
209     {
210         throw new UnsupportedOperationException("Not supported yet.");
211     }
212 
213     @Override
214     public <T> T[] toArray(T[] a)
215     {
216         throw new UnsupportedOperationException("Not supported yet.");
217     }
218 
219     @Override
220     public boolean add(T e)
221     {
222         push(e);
223         return true;
224     }
225 
226     @Override
227     public boolean remove(Object o)
228     {
229         throw new UnsupportedOperationException("Not supported yet.");
230     }
231 
232     @Override
233     public boolean containsAll(Collection<?> c)
234     {
235         throw new UnsupportedOperationException("Not supported yet.");
236     }
237 
238     @Override
239     public boolean addAll(Collection<? extends T> c)
240     {
241         throw new UnsupportedOperationException("Not supported yet.");
242     }
243 
244     @Override
245     public boolean removeAll(Collection<?> c)
246     {
247         throw new UnsupportedOperationException("Not supported yet.");
248     }
249 
250     @Override
251     public boolean retainAll(Collection<?> c)
252     {
253         throw new UnsupportedOperationException("Not supported yet.");
254     }
255 
256     @Override
257     public boolean equals(Object obj)
258     {
259         if (obj == null)
260         {
261             return false;
262         }
263         if (getClass() != obj.getClass())
264         {
265             return false;
266         }
267         final SimpleStack<T> other = (SimpleStack<T>) obj;
268         if (this.top != other.top)
269         {
270             return false;
271         }
272         if (!Arrays.deepEquals(this.elems, other.elems))
273         {
274             return false;
275         }
276         return true;
277     }
278 
279     @Override
280     public int hashCode()
281     {
282         int hash = 7;
283         hash = 37 * hash + Arrays.deepHashCode(this.elems);
284         hash = 37 * hash + this.top;
285         return hash;
286     }
287 }