1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xml.internal.utils;
23 
24 import java.util.EmptyStackException;
25 
26 /**
27  * Implement a stack of simple integers.
28  *
29  * %OPT%
30  * This is currently based on ObjectVector, which permits fast acess but pays a
31  * heavy recopying penalty if/when its size is increased. If we expect deep
32  * stacks, we should consider a version based on ChunkedObjectVector.
33  * @xsl.usage internal
34  */
35 public class ObjectStack extends ObjectVector
36 {
37 
38   /**
39    * Default constructor.  Note that the default
40    * block size is very small, for small lists.
41    */
ObjectStack()42   public ObjectStack()
43   {
44     super();
45   }
46 
47   /**
48    * Construct a ObjectVector, using the given block size.
49    *
50    * @param blocksize Size of block to allocate
51    */
ObjectStack(int blocksize)52   public ObjectStack(int blocksize)
53   {
54     super(blocksize);
55   }
56 
57   /**
58    * Copy constructor for ObjectStack
59    *
60    * @param v ObjectStack to copy
61    */
ObjectStack(ObjectStack v)62   public ObjectStack (ObjectStack v)
63   {
64         super(v);
65   }
66 
67   /**
68    * Pushes an item onto the top of this stack.
69    *
70    * @param   i   the int to be pushed onto this stack.
71    * @return  the <code>item</code> argument.
72    */
push(Object i)73   public Object push(Object i)
74   {
75 
76     if ((m_firstFree + 1) >= m_mapSize)
77     {
78       m_mapSize += m_blocksize;
79 
80       Object newMap[] = new Object[m_mapSize];
81 
82       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
83 
84       m_map = newMap;
85     }
86 
87     m_map[m_firstFree] = i;
88 
89     m_firstFree++;
90 
91     return i;
92   }
93 
94   /**
95    * Removes the object at the top of this stack and returns that
96    * object as the value of this function.
97    *
98    * @return     The object at the top of this stack.
99    */
pop()100   public Object pop()
101   {
102     Object val = m_map[--m_firstFree];
103     m_map[m_firstFree] = null;
104 
105     return val;
106   }
107 
108   /**
109    * Quickly pops a number of items from the stack.
110    */
111 
quickPop(int n)112   public void quickPop(int n)
113   {
114     m_firstFree -= n;
115   }
116 
117   /**
118    * Looks at the object at the top of this stack without removing it
119    * from the stack.
120    *
121    * @return     the object at the top of this stack.
122    * @throws  EmptyStackException  if this stack is empty.
123    */
peek()124   public Object peek()
125   {
126     try {
127       return m_map[m_firstFree - 1];
128     }
129     catch (ArrayIndexOutOfBoundsException e)
130     {
131       throw new EmptyStackException();
132     }
133   }
134 
135   /**
136    * Looks at the object at the position the stack counting down n items.
137    *
138    * @param n The number of items down, indexed from zero.
139    * @return     the object at n items down.
140    * @throws  EmptyStackException  if this stack is empty.
141    */
peek(int n)142   public Object peek(int n)
143   {
144     try {
145       return m_map[m_firstFree-(1+n)];
146     }
147     catch (ArrayIndexOutOfBoundsException e)
148     {
149       throw new EmptyStackException();
150     }
151   }
152 
153   /**
154    * Sets an object at a the top of the statck
155    *
156    *
157    * @param val object to set at the top
158    * @throws  EmptyStackException  if this stack is empty.
159    */
setTop(Object val)160   public void setTop(Object val)
161   {
162     try {
163       m_map[m_firstFree - 1] = val;
164     }
165     catch (ArrayIndexOutOfBoundsException e)
166     {
167       throw new EmptyStackException();
168     }
169   }
170 
171   /**
172    * Tests if this stack is empty.
173    *
174    * @return  <code>true</code> if this stack is empty;
175    *          <code>false</code> otherwise.
176    * @since   JDK1.0
177    */
empty()178   public boolean empty()
179   {
180     return m_firstFree == 0;
181   }
182 
183   /**
184    * Returns where an object is on this stack.
185    *
186    * @param   o   the desired object.
187    * @return  the distance from the top of the stack where the object is]
188    *          located; the return value <code>-1</code> indicates that the
189    *          object is not on the stack.
190    * @since   JDK1.0
191    */
search(Object o)192   public int search(Object o)
193   {
194 
195     int i = lastIndexOf(o);
196 
197     if (i >= 0)
198     {
199       return size() - i;
200     }
201 
202     return -1;
203   }
204 
205   /**
206    * Returns clone of current ObjectStack
207    *
208    * @return clone of current ObjectStack
209    */
clone()210   public Object clone()
211     throws CloneNotSupportedException
212   {
213         return (ObjectStack) super.clone();
214   }
215 
216 }
217