286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: BoolStack.java,v 1.1.4.1 2005/09/08 11:03:08 suresh_emailid Exp $
286N/A */
286N/Apackage com.sun.org.apache.xml.internal.serializer.utils;
286N/A
286N/A
286N/A/**
286N/A * Simple stack for boolean values.
286N/A *
286N/A * This class is a copy of the one in com.sun.org.apache.xml.internal.utils.
286N/A * It exists to cut the serializers dependancy on that package.
286N/A * A minor changes from that package are:
286N/A * doesn't implement Clonable
286N/A *
286N/A * This class is not a public API, it is only public because it is
286N/A * used in com.sun.org.apache.xml.internal.serializer.
286N/A *
286N/A * @xsl.usage internal
286N/A */
286N/Apublic final class BoolStack
286N/A{
286N/A
286N/A /** Array of boolean values */
286N/A private boolean m_values[];
286N/A
286N/A /** Array size allocated */
286N/A private int m_allocatedSize;
286N/A
286N/A /** Index into the array of booleans */
286N/A private int m_index;
286N/A
286N/A /**
286N/A * Default constructor. Note that the default
286N/A * block size is very small, for small lists.
286N/A */
286N/A public BoolStack()
286N/A {
286N/A this(32);
286N/A }
286N/A
286N/A /**
286N/A * Construct a IntVector, using the given block size.
286N/A *
286N/A * @param size array size to allocate
286N/A */
286N/A public BoolStack(int size)
286N/A {
286N/A
286N/A m_allocatedSize = size;
286N/A m_values = new boolean[size];
286N/A m_index = -1;
286N/A }
286N/A
286N/A /**
286N/A * Get the length of the list.
286N/A *
286N/A * @return Current length of the list
286N/A */
286N/A public final int size()
286N/A {
286N/A return m_index + 1;
286N/A }
286N/A
286N/A /**
286N/A * Clears the stack.
286N/A *
286N/A */
286N/A public final void clear()
286N/A {
286N/A m_index = -1;
286N/A }
286N/A
286N/A /**
286N/A * Pushes an item onto the top of this stack.
286N/A *
286N/A *
286N/A * @param val the boolean to be pushed onto this stack.
286N/A * @return the <code>item</code> argument.
286N/A */
286N/A public final boolean push(boolean val)
286N/A {
286N/A
286N/A if (m_index == m_allocatedSize - 1)
286N/A grow();
286N/A
286N/A return (m_values[++m_index] = val);
286N/A }
286N/A
286N/A /**
286N/A * Removes the object at the top of this stack and returns that
286N/A * object as the value of this function.
286N/A *
286N/A * @return The object at the top of this stack.
286N/A * @throws EmptyStackException if this stack is empty.
286N/A */
286N/A public final boolean pop()
286N/A {
286N/A return m_values[m_index--];
286N/A }
286N/A
286N/A /**
286N/A * Removes the object at the top of this stack and returns the
286N/A * next object at the top as the value of this function.
286N/A *
286N/A *
286N/A * @return Next object to the top or false if none there
286N/A */
286N/A public final boolean popAndTop()
286N/A {
286N/A
286N/A m_index--;
286N/A
286N/A return (m_index >= 0) ? m_values[m_index] : false;
286N/A }
286N/A
286N/A /**
286N/A * Set the item at the top of this stack
286N/A *
286N/A *
286N/A * @param b Object to set at the top of this stack
286N/A */
286N/A public final void setTop(boolean b)
286N/A {
286N/A m_values[m_index] = b;
286N/A }
286N/A
286N/A /**
286N/A * Looks at the object at the top of this stack without removing it
286N/A * from the stack.
286N/A *
286N/A * @return the object at the top of this stack.
286N/A * @throws EmptyStackException if this stack is empty.
286N/A */
286N/A public final boolean peek()
286N/A {
286N/A return m_values[m_index];
286N/A }
286N/A
286N/A /**
286N/A * Looks at the object at the top of this stack without removing it
286N/A * from the stack. If the stack is empty, it returns false.
286N/A *
286N/A * @return the object at the top of this stack.
286N/A */
286N/A public final boolean peekOrFalse()
286N/A {
286N/A return (m_index > -1) ? m_values[m_index] : false;
286N/A }
286N/A
286N/A /**
286N/A * Looks at the object at the top of this stack without removing it
286N/A * from the stack. If the stack is empty, it returns true.
286N/A *
286N/A * @return the object at the top of this stack.
286N/A */
286N/A public final boolean peekOrTrue()
286N/A {
286N/A return (m_index > -1) ? m_values[m_index] : true;
286N/A }
286N/A
286N/A /**
286N/A * Tests if this stack is empty.
286N/A *
286N/A * @return <code>true</code> if this stack is empty;
286N/A * <code>false</code> otherwise.
286N/A */
286N/A public boolean isEmpty()
286N/A {
286N/A return (m_index == -1);
286N/A }
286N/A
286N/A /**
286N/A * Grows the size of the stack
286N/A *
286N/A */
286N/A private void grow()
286N/A {
286N/A
286N/A m_allocatedSize *= 2;
286N/A
286N/A boolean newVector[] = new boolean[m_allocatedSize];
286N/A
286N/A System.arraycopy(m_values, 0, newVector, 0, m_index + 1);
286N/A
286N/A m_values = newVector;
286N/A }
286N/A}