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 
22 package com.sun.org.apache.xerces.internal.util;
23 
24 /**
25  * A simple integer based stack.
26  *
27  * moved to com.sun.org.apache.xerces.internal.util by neilg to support the
28  * XPathMatcher.
29  * @author  Andy Clark, IBM
30  *
31  */
32 public final class IntStack {
33 
34     //
35     // Data
36     //
37 
38     /** Stack depth. */
39     private int fDepth;
40 
41     /** Stack data. */
42     private int[] fData;
43 
44     //
45     // Public methods
46     //
47 
48     /** Returns the size of the stack. */
size()49     public int size() {
50         return fDepth;
51     }
52 
53     /** Pushes a value onto the stack. */
push(int value)54     public void push(int value) {
55         ensureCapacity(fDepth + 1);
56         fData[fDepth++] = value;
57     }
58 
59     /** Peeks at the top of the stack. */
peek()60     public int peek() {
61         return fData[fDepth - 1];
62     }
63 
64     /** Returns the element at the specified depth in the stack. */
elementAt(int depth)65     public int elementAt(int depth) {
66         return fData[depth];
67     }
68 
69     /** Pops a value off of the stack. */
pop()70     public int pop() {
71         return fData[--fDepth];
72     }
73 
74     /** Clears the stack. */
clear()75     public void clear() {
76         fDepth = 0;
77     }
78 
79     // debugging
80 
81     /** Prints the stack. */
print()82     public void print() {
83         System.out.print('(');
84         System.out.print(fDepth);
85         System.out.print(") {");
86         for (int i = 0; i < fDepth; i++) {
87             if (i == 3) {
88                 System.out.print(" ...");
89                 break;
90             }
91             System.out.print(' ');
92             System.out.print(fData[i]);
93             if (i < fDepth - 1) {
94                 System.out.print(',');
95             }
96         }
97         System.out.print(" }");
98         System.out.println();
99     }
100 
101     //
102     // Private methods
103     //
104 
105     /** Ensures capacity. */
ensureCapacity(int size)106     private void ensureCapacity(int size) {
107         if (fData == null) {
108             fData = new int[32];
109         }
110         else if (fData.length <= size) {
111             int[] newdata = new int[fData.length * 2];
112             System.arraycopy(fData, 0, newdata, 0, fData.length);
113             fData = newdata;
114         }
115     }
116 
117 } // class IntStack
118