KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > IntStack


1 /*
2  * Copyright 2000-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.util;
18
19 /**
20  * A simple integer based stack.
21  *
22  * moved to org.apache.xerces.util by neilg to support the
23  * XPathMatcher.
24  * @author Andy Clark, IBM
25  *
26  * @version $Id: IntStack.java,v 1.5 2004/02/24 23:15:53 mrglavas Exp $
27  */

28 public final class IntStack {
29
30     //
31
// Data
32
//
33

34     /** Stack depth. */
35     private int fDepth;
36
37     /** Stack data. */
38     private int[] fData;
39
40     //
41
// Public methods
42
//
43

44     /** Returns the size of the stack. */
45     public int size() {
46         return fDepth;
47     }
48
49     /** Pushes a value onto the stack. */
50     public void push(int value) {
51         ensureCapacity(fDepth + 1);
52         fData[fDepth++] = value;
53     }
54
55     /** Peeks at the top of the stack. */
56     public int peek() {
57         return fData[fDepth - 1];
58     }
59
60     /** Returns the element at the specified depth in the stack. */
61     public int elementAt(int depth) {
62         return fData[depth];
63     }
64
65     /** Pops a value off of the stack. */
66     public int pop() {
67         return fData[--fDepth];
68     }
69
70     /** Clears the stack. */
71     public void clear() {
72         fDepth = 0;
73     }
74
75     // debugging
76

77     /** Prints the stack. */
78     public void print() {
79         System.out.print('(');
80         System.out.print(fDepth);
81         System.out.print(") {");
82         for (int i = 0; i < fDepth; i++) {
83             if (i == 3) {
84                 System.out.print(" ...");
85                 break;
86             }
87             System.out.print(' ');
88             System.out.print(fData[i]);
89             if (i < fDepth - 1) {
90                 System.out.print(',');
91             }
92         }
93         System.out.print(" }");
94         System.out.println();
95     }
96
97     //
98
// Private methods
99
//
100

101     /** Ensures capacity. */
102     private void ensureCapacity(int size) {
103         if (fData == null) {
104             fData = new int[32];
105         }
106         else if (fData.length <= size) {
107             int[] newdata = new int[fData.length * 2];
108             System.arraycopy(fData, 0, newdata, 0, fData.length);
109             fData = newdata;
110         }
111     }
112
113 } // class IntStack
114
Popular Tags