Home | History | Annotate | Download | only in utils
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: BoolStack.java 468655 2006-10-28 07:12:06Z minchau $
     20  */
     21 package org.apache.xml.utils;
     22 
     23 
     24 /**
     25  * Simple stack for boolean values.
     26  * @xsl.usage internal
     27  */
     28 public final class BoolStack implements Cloneable
     29 {
     30 
     31   /** Array of boolean values          */
     32   private boolean m_values[];
     33 
     34   /** Array size allocated           */
     35   private int m_allocatedSize;
     36 
     37   /** Index into the array of booleans          */
     38   private int m_index;
     39 
     40   /**
     41    * Default constructor.  Note that the default
     42    * block size is very small, for small lists.
     43    */
     44   public BoolStack()
     45   {
     46     this(32);
     47   }
     48 
     49   /**
     50    * Construct a IntVector, using the given block size.
     51    *
     52    * @param size array size to allocate
     53    */
     54   public BoolStack(int size)
     55   {
     56 
     57     m_allocatedSize = size;
     58     m_values = new boolean[size];
     59     m_index = -1;
     60   }
     61 
     62   /**
     63    * Get the length of the list.
     64    *
     65    * @return Current length of the list
     66    */
     67   public final int size()
     68   {
     69     return m_index + 1;
     70   }
     71 
     72   /**
     73    * Clears the stack.
     74    *
     75    */
     76   public final void clear()
     77   {
     78   	m_index = -1;
     79   }
     80 
     81   /**
     82    * Pushes an item onto the top of this stack.
     83    *
     84    *
     85    * @param val the boolean to be pushed onto this stack.
     86    * @return  the <code>item</code> argument.
     87    */
     88   public final boolean push(boolean val)
     89   {
     90 
     91     if (m_index == m_allocatedSize - 1)
     92       grow();
     93 
     94     return (m_values[++m_index] = val);
     95   }
     96 
     97   /**
     98    * Removes the object at the top of this stack and returns that
     99    * 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   {
    106     return m_values[m_index--];
    107   }
    108 
    109   /**
    110    * Removes the object at the top of this stack and returns the
    111    * next object at the top as the value of this function.
    112    *
    113    *
    114    * @return Next object to the top or false if none there
    115    */
    116   public final boolean popAndTop()
    117   {
    118 
    119     m_index--;
    120 
    121     return (m_index >= 0) ? m_values[m_index] : false;
    122   }
    123 
    124   /**
    125    * Set the item at the top of this stack
    126    *
    127    *
    128    * @param b Object to set at the top of this stack
    129    */
    130   public final void setTop(boolean b)
    131   {
    132     m_values[m_index] = b;
    133   }
    134 
    135   /**
    136    * Looks at the object at the top of this stack without removing it
    137    * from the stack.
    138    *
    139    * @return     the object at the top of this stack.
    140    * @throws  EmptyStackException  if this stack is empty.
    141    */
    142   public final boolean peek()
    143   {
    144     return m_values[m_index];
    145   }
    146 
    147   /**
    148    * Looks at the object at the top of this stack without removing it
    149    * from the stack.  If the stack is empty, it returns false.
    150    *
    151    * @return     the object at the top of this stack.
    152    */
    153   public final boolean peekOrFalse()
    154   {
    155     return (m_index > -1) ? m_values[m_index] : false;
    156   }
    157 
    158   /**
    159    * Looks at the object at the top of this stack without removing it
    160    * from the stack.  If the stack is empty, it returns true.
    161    *
    162    * @return     the object at the top of this stack.
    163    */
    164   public final boolean peekOrTrue()
    165   {
    166     return (m_index > -1) ? m_values[m_index] : true;
    167   }
    168 
    169   /**
    170    * Tests if this stack is empty.
    171    *
    172    * @return  <code>true</code> if this stack is empty;
    173    *          <code>false</code> otherwise.
    174    */
    175   public boolean isEmpty()
    176   {
    177     return (m_index == -1);
    178   }
    179 
    180   /**
    181    * Grows the size of the stack
    182    *
    183    */
    184   private void grow()
    185   {
    186 
    187     m_allocatedSize *= 2;
    188 
    189     boolean newVector[] = new boolean[m_allocatedSize];
    190 
    191     System.arraycopy(m_values, 0, newVector, 0, m_index + 1);
    192 
    193     m_values = newVector;
    194   }
    195 
    196   public Object clone()
    197     throws CloneNotSupportedException
    198   {
    199     return super.clone();
    200   }
    201 
    202 }
    203