001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: BoolStack.java 468654 2006-10-28 07:09:23Z minchau $
020 */
021 package org.apache.xml.serializer.utils;
022
023
024 /**
025 * Simple stack for boolean values.
026 *
027 * This class is a copy of the one in org.apache.xml.utils.
028 * It exists to cut the serializers dependancy on that package.
029 * A minor changes from that package are:
030 * doesn't implement Clonable
031 *
032 * This class is not a public API, it is only public because it is
033 * used in org.apache.xml.serializer.
034 *
035 * @xsl.usage internal
036 */
037 public final class BoolStack
038 {
039
040 /** Array of boolean values */
041 private boolean m_values[];
042
043 /** Array size allocated */
044 private int m_allocatedSize;
045
046 /** Index into the array of booleans */
047 private int m_index;
048
049 /**
050 * Default constructor. Note that the default
051 * block size is very small, for small lists.
052 */
053 public BoolStack()
054 {
055 this(32);
056 }
057
058 /**
059 * Construct a IntVector, using the given block size.
060 *
061 * @param size array size to allocate
062 */
063 public BoolStack(int size)
064 {
065
066 m_allocatedSize = size;
067 m_values = new boolean[size];
068 m_index = -1;
069 }
070
071 /**
072 * Get the length of the list.
073 *
074 * @return Current length of the list
075 */
076 public final int size()
077 {
078 return m_index + 1;
079 }
080
081 /**
082 * Clears the stack.
083 *
084 */
085 public final void clear()
086 {
087 m_index = -1;
088 }
089
090 /**
091 * Pushes an item onto the top of this stack.
092 *
093 *
094 * @param val the boolean to be pushed onto this stack.
095 * @return the <code>item</code> argument.
096 */
097 public final boolean push(boolean val)
098 {
099
100 if (m_index == m_allocatedSize - 1)
101 grow();
102
103 return (m_values[++m_index] = val);
104 }
105
106 /**
107 * Removes the object at the top of this stack and returns that
108 * object as the value of this function.
109 *
110 * @return The object at the top of this stack.
111 * @throws EmptyStackException if this stack is empty.
112 */
113 public final boolean pop()
114 {
115 return m_values[m_index--];
116 }
117
118 /**
119 * Removes the object at the top of this stack and returns the
120 * next object at the top as the value of this function.
121 *
122 *
123 * @return Next object to the top or false if none there
124 */
125 public final boolean popAndTop()
126 {
127
128 m_index--;
129
130 return (m_index >= 0) ? m_values[m_index] : false;
131 }
132
133 /**
134 * Set the item at the top of this stack
135 *
136 *
137 * @param b Object to set at the top of this stack
138 */
139 public final void setTop(boolean b)
140 {
141 m_values[m_index] = b;
142 }
143
144 /**
145 * Looks at the object at the top of this stack without removing it
146 * from the stack.
147 *
148 * @return the object at the top of this stack.
149 * @throws EmptyStackException if this stack is empty.
150 */
151 public final boolean peek()
152 {
153 return m_values[m_index];
154 }
155
156 /**
157 * Looks at the object at the top of this stack without removing it
158 * from the stack. If the stack is empty, it returns false.
159 *
160 * @return the object at the top of this stack.
161 */
162 public final boolean peekOrFalse()
163 {
164 return (m_index > -1) ? m_values[m_index] : false;
165 }
166
167 /**
168 * Looks at the object at the top of this stack without removing it
169 * from the stack. If the stack is empty, it returns true.
170 *
171 * @return the object at the top of this stack.
172 */
173 public final boolean peekOrTrue()
174 {
175 return (m_index > -1) ? m_values[m_index] : true;
176 }
177
178 /**
179 * Tests if this stack is empty.
180 *
181 * @return <code>true</code> if this stack is empty;
182 * <code>false</code> otherwise.
183 */
184 public boolean isEmpty()
185 {
186 return (m_index == -1);
187 }
188
189 /**
190 * Grows the size of the stack
191 *
192 */
193 private void grow()
194 {
195
196 m_allocatedSize *= 2;
197
198 boolean newVector[] = new boolean[m_allocatedSize];
199
200 System.arraycopy(m_values, 0, newVector, 0, m_index + 1);
201
202 m_values = newVector;
203 }
204 }