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