View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2000-2002,2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package com.sun.org.apache.xerces.internal.util;
22  
23  /**
24   * A simple integer based stack.
25   *
26   * moved to com.sun.org.apache.xerces.internal.util by neilg to support the
27   * XPathMatcher.
28   * @author  Andy Clark, IBM
29   *
30   */
31  public final class IntStack {
32  
33      //
34      // Data
35      //
36  
37      /** Stack depth. */
38      private int fDepth;
39  
40      /** Stack data. */
41      private int[] fData;
42  
43      //
44      // Public methods
45      //
46  
47      /** Returns the size of the stack. */
48      public int size() {
49          return fDepth;
50      }
51  
52      /** Pushes a value onto the stack. */
53      public void push(int value) {
54          ensureCapacity(fDepth + 1);
55          fData[fDepth++] = value;
56      }
57  
58      /** Peeks at the top of the stack. */
59      public int peek() {
60          return fData[fDepth - 1];
61      }
62  
63      /** Returns the element at the specified depth in the stack. */
64      public int elementAt(int depth) {
65          return fData[depth];
66      }
67  
68      /** Pops a value off of the stack. */
69      public int pop() {
70          return fData[--fDepth];
71      }
72  
73      /** Clears the stack. */
74      public void clear() {
75          fDepth = 0;
76      }
77  
78      // debugging
79  
80      /** Prints the stack. */
81      public void print() {
82          System.out.print('(');
83          System.out.print(fDepth);
84          System.out.print(") {");
85          for (int i = 0; i < fDepth; i++) {
86              if (i == 3) {
87                  System.out.print(" ...");
88                  break;
89              }
90              System.out.print(' ');
91              System.out.print(fData[i]);
92              if (i < fDepth - 1) {
93                  System.out.print(',');
94              }
95          }
96          System.out.print(" }");
97          System.out.println();
98      }
99  
100     //
101     // Private methods
102     //
103 
104     /** Ensures capacity. */
105     private void ensureCapacity(int size) {
106         if (fData == null) {
107             fData = new int[32];
108         }
109         else if (fData.length <= size) {
110             int[] newdata = new int[fData.length * 2];
111             System.arraycopy(fData, 0, newdata, 0, fData.length);
112             fData = newdata;
113         }
114     }
115 
116 } // class IntStack