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.10 2004/02/17 04:21:14 minchau Exp $
018: */
019: package org.apache.xml.utils;
020:
021: /**
022: * Simple stack for boolean values.
023: * @xsl.usage internal
024: */
025: public final class BoolStack implements Cloneable {
026:
027: /** Array of boolean values */
028: private boolean m_values[];
029:
030: /** Array size allocated */
031: private int m_allocatedSize;
032:
033: /** Index into the array of booleans */
034: private int m_index;
035:
036: /**
037: * Default constructor. Note that the default
038: * block size is very small, for small lists.
039: */
040: public BoolStack() {
041: this (32);
042: }
043:
044: /**
045: * Construct a IntVector, using the given block size.
046: *
047: * @param size array size to allocate
048: */
049: public BoolStack(int size) {
050:
051: m_allocatedSize = size;
052: m_values = new boolean[size];
053: m_index = -1;
054: }
055:
056: /**
057: * Get the length of the list.
058: *
059: * @return Current length of the list
060: */
061: public final int size() {
062: return m_index + 1;
063: }
064:
065: /**
066: * Clears the stack.
067: *
068: */
069: public final void clear() {
070: m_index = -1;
071: }
072:
073: /**
074: * Pushes an item onto the top of this stack.
075: *
076: *
077: * @param val the boolean to be pushed onto this stack.
078: * @return the <code>item</code> argument.
079: */
080: public final boolean push(boolean val) {
081:
082: if (m_index == m_allocatedSize - 1)
083: grow();
084:
085: return (m_values[++m_index] = val);
086: }
087:
088: /**
089: * Removes the object at the top of this stack and returns that
090: * object as the value of this function.
091: *
092: * @return The object at the top of this stack.
093: * @throws EmptyStackException if this stack is empty.
094: */
095: public final boolean pop() {
096: return m_values[m_index--];
097: }
098:
099: /**
100: * Removes the object at the top of this stack and returns the
101: * next object at the top as the value of this function.
102: *
103: *
104: * @return Next object to the top or false if none there
105: */
106: public final boolean popAndTop() {
107:
108: m_index--;
109:
110: return (m_index >= 0) ? m_values[m_index] : false;
111: }
112:
113: /**
114: * Set the item at the top of this stack
115: *
116: *
117: * @param b Object to set at the top of this stack
118: */
119: public final void setTop(boolean b) {
120: m_values[m_index] = b;
121: }
122:
123: /**
124: * Looks at the object at the top of this stack without removing it
125: * from the stack.
126: *
127: * @return the object at the top of this stack.
128: * @throws EmptyStackException if this stack is empty.
129: */
130: public final boolean peek() {
131: return m_values[m_index];
132: }
133:
134: /**
135: * Looks at the object at the top of this stack without removing it
136: * from the stack. If the stack is empty, it returns false.
137: *
138: * @return the object at the top of this stack.
139: */
140: public final boolean peekOrFalse() {
141: return (m_index > -1) ? m_values[m_index] : false;
142: }
143:
144: /**
145: * Looks at the object at the top of this stack without removing it
146: * from the stack. If the stack is empty, it returns true.
147: *
148: * @return the object at the top of this stack.
149: */
150: public final boolean peekOrTrue() {
151: return (m_index > -1) ? m_values[m_index] : true;
152: }
153:
154: /**
155: * Tests if this stack is empty.
156: *
157: * @return <code>true</code> if this stack is empty;
158: * <code>false</code> otherwise.
159: */
160: public boolean isEmpty() {
161: return (m_index == -1);
162: }
163:
164: /**
165: * Grows the size of the stack
166: *
167: */
168: private void grow() {
169:
170: m_allocatedSize *= 2;
171:
172: boolean newVector[] = new boolean[m_allocatedSize];
173:
174: System.arraycopy(m_values, 0, newVector, 0, m_index + 1);
175:
176: m_values = newVector;
177: }
178:
179: public Object clone() throws CloneNotSupportedException {
180: return super.clone();
181: }
182:
183: }
|