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