001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.util;
019:
020: /**
021: * A simple integer based stack.
022: *
023: * moved to org.apache.xerces.util by neilg to support the
024: * XPathMatcher.
025: * @author Andy Clark, IBM
026: *
027: * @version $Id: IntStack.java 447241 2006-09-18 05:12:57Z mrglavas $
028: */
029: public final class IntStack {
030:
031: //
032: // Data
033: //
034:
035: /** Stack depth. */
036: private int fDepth;
037:
038: /** Stack data. */
039: private int[] fData;
040:
041: //
042: // Public methods
043: //
044:
045: /** Returns the size of the stack. */
046: public int size() {
047: return fDepth;
048: }
049:
050: /** Pushes a value onto the stack. */
051: public void push(int value) {
052: ensureCapacity(fDepth + 1);
053: fData[fDepth++] = value;
054: }
055:
056: /** Peeks at the top of the stack. */
057: public int peek() {
058: return fData[fDepth - 1];
059: }
060:
061: /** Returns the element at the specified depth in the stack. */
062: public int elementAt(int depth) {
063: return fData[depth];
064: }
065:
066: /** Pops a value off of the stack. */
067: public int pop() {
068: return fData[--fDepth];
069: }
070:
071: /** Clears the stack. */
072: public void clear() {
073: fDepth = 0;
074: }
075:
076: // debugging
077:
078: /** Prints the stack. */
079: public void print() {
080: System.out.print('(');
081: System.out.print(fDepth);
082: System.out.print(") {");
083: for (int i = 0; i < fDepth; i++) {
084: if (i == 3) {
085: System.out.print(" ...");
086: break;
087: }
088: System.out.print(' ');
089: System.out.print(fData[i]);
090: if (i < fDepth - 1) {
091: System.out.print(',');
092: }
093: }
094: System.out.print(" }");
095: System.out.println();
096: }
097:
098: //
099: // Private methods
100: //
101:
102: /** Ensures capacity. */
103: private void ensureCapacity(int size) {
104: if (fData == null) {
105: fData = new int[32];
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
|