KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > utils > ObjectStack


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id$
18  */

19 package com.sun.org.apache.xml.internal.utils;
20
21 import java.util.EmptyStackException JavaDoc;
22
23 /**
24  * Implement a stack of simple integers.
25  *
26  * %OPT%
27  * This is currently based on ObjectVector, which permits fast acess but pays a
28  * heavy recopying penalty if/when its size is increased. If we expect deep
29  * stacks, we should consider a version based on ChunkedObjectVector.
30  * @xsl.usage internal
31  */

32 public class ObjectStack extends ObjectVector
33 {
34
35   /**
36    * Default constructor. Note that the default
37    * block size is very small, for small lists.
38    */

39   public ObjectStack()
40   {
41     super();
42   }
43
44   /**
45    * Construct a ObjectVector, using the given block size.
46    *
47    * @param blocksize Size of block to allocate
48    */

49   public ObjectStack(int blocksize)
50   {
51     super(blocksize);
52   }
53
54   /**
55    * Copy constructor for ObjectStack
56    *
57    * @param v ObjectStack to copy
58    */

59   public ObjectStack (ObjectStack v)
60   {
61     super(v);
62   }
63   
64   /**
65    * Pushes an item onto the top of this stack.
66    *
67    * @param i the int to be pushed onto this stack.
68    * @return the <code>item</code> argument.
69    */

70   public Object JavaDoc push(Object JavaDoc i)
71   {
72
73     if ((m_firstFree + 1) >= m_mapSize)
74     {
75       m_mapSize += m_blocksize;
76
77       Object JavaDoc newMap[] = new Object JavaDoc[m_mapSize];
78
79       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
80
81       m_map = newMap;
82     }
83
84     m_map[m_firstFree] = i;
85
86     m_firstFree++;
87
88     return i;
89   }
90
91   /**
92    * Removes the object at the top of this stack and returns that
93    * object as the value of this function.
94    *
95    * @return The object at the top of this stack.
96    */

97   public Object JavaDoc pop()
98   {
99     Object JavaDoc val = m_map[--m_firstFree];
100     m_map[m_firstFree] = null;
101     
102     return val;
103   }
104
105   /**
106    * Quickly pops a number of items from the stack.
107    */

108
109   public void quickPop(int n)
110   {
111     m_firstFree -= n;
112   }
113
114   /**
115    * Looks at the object at the top of this stack without removing it
116    * from the stack.
117    *
118    * @return the object at the top of this stack.
119    * @throws EmptyStackException if this stack is empty.
120    */

121   public Object JavaDoc peek()
122   {
123     try {
124       return m_map[m_firstFree - 1];
125     }
126     catch (ArrayIndexOutOfBoundsException JavaDoc e)
127     {
128       throw new EmptyStackException JavaDoc();
129     }
130   }
131
132   /**
133    * Looks at the object at the position the stack counting down n items.
134    *
135    * @param n The number of items down, indexed from zero.
136    * @return the object at n items down.
137    * @throws EmptyStackException if this stack is empty.
138    */

139   public Object JavaDoc peek(int n)
140   {
141     try {
142       return m_map[m_firstFree-(1+n)];
143     }
144     catch (ArrayIndexOutOfBoundsException JavaDoc e)
145     {
146       throw new EmptyStackException JavaDoc();
147     }
148   }
149
150   /**
151    * Sets an object at a the top of the statck
152    *
153    *
154    * @param val object to set at the top
155    * @throws EmptyStackException if this stack is empty.
156    */

157   public void setTop(Object JavaDoc val)
158   {
159     try {
160       m_map[m_firstFree - 1] = val;
161     }
162     catch (ArrayIndexOutOfBoundsException JavaDoc e)
163     {
164       throw new EmptyStackException JavaDoc();
165     }
166   }
167
168   /**
169    * Tests if this stack is empty.
170    *
171    * @return <code>true</code> if this stack is empty;
172    * <code>false</code> otherwise.
173    * @since JDK1.0
174    */

175   public boolean empty()
176   {
177     return m_firstFree == 0;
178   }
179
180   /**
181    * Returns where an object is on this stack.
182    *
183    * @param o the desired object.
184    * @return the distance from the top of the stack where the object is]
185    * located; the return value <code>-1</code> indicates that the
186    * object is not on the stack.
187    * @since JDK1.0
188    */

189   public int search(Object JavaDoc o)
190   {
191
192     int i = lastIndexOf(o);
193
194     if (i >= 0)
195     {
196       return size() - i;
197     }
198
199     return -1;
200   }
201
202   /**
203    * Returns clone of current ObjectStack
204    *
205    * @return clone of current ObjectStack
206    */

207   public Object JavaDoc clone()
208     throws CloneNotSupportedException JavaDoc
209   {
210     return (ObjectStack) super.clone();
211   }
212   
213 }
214
Popular Tags