001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2000,2001 The Apache Software Foundation.
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.utils;
059:
060: /**
061: * A simple integer based stack.
062: *
063: * @author Andy Clark, IBM
064: *
065: * @version $Id: IntStack.java,v 1.1 2001/01/25 07:18:39 andyc Exp $
066: */
067: public final class IntStack {
068:
069: //
070: // Data
071: //
072:
073: /** Stack depth. */
074: private int fDepth;
075:
076: /** Stack data. */
077: private int[] fData;
078:
079: //
080: // Public methods
081: //
082:
083: /** Returns the size of the stack. */
084: public int size() {
085: return fDepth;
086: }
087:
088: /** Pushes a value onto the stack. */
089: public void push(int value) {
090: ensureCapacity(fDepth + 1);
091: fData[fDepth++] = value;
092: }
093:
094: /** Peeks at the top of the stack. */
095: public int peek() {
096: return fData[fDepth - 1];
097: }
098:
099: /** Pops a value off of the stack. */
100: public int pop() {
101: return fData[--fDepth];
102: }
103:
104: /** Clears the stack. */
105: public void clear() {
106: fDepth = 0;
107: }
108:
109: // debugging
110:
111: /** Prints the stack. */
112: public void print() {
113: System.out.print('(');
114: System.out.print(fDepth);
115: System.out.print(") {");
116: for (int i = 0; i < fDepth; i++) {
117: if (i == 3) {
118: System.out.print(" ...");
119: break;
120: }
121: System.out.print(' ');
122: System.out.print(fData[i]);
123: if (i < fDepth - 1) {
124: System.out.print(',');
125: }
126: }
127: System.out.print(" }");
128: System.out.println();
129: }
130:
131: //
132: // Private methods
133: //
134:
135: /** Ensures capacity. */
136: private boolean ensureCapacity(int size) {
137: try {
138: return fData[size] != 0;
139: } catch (NullPointerException e) {
140: fData = new int[32];
141: } catch (ArrayIndexOutOfBoundsException e) {
142: int[] newdata = new int[fData.length * 2];
143: System.arraycopy(fData, 0, newdata, 0, fData.length);
144: fData = newdata;
145: }
146: return true;
147: }
148:
149: } // class IntStack
|