david_williams | 9621348 | 2004-11-11 09:07:12 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 2004 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.wst.xml.core.internal.contenttype; |
| 14 | |
| 15 | /* |
| 16 | * |
| 17 | * A non-resizable class implementing the behavior of java.util.Stack, but |
| 18 | * directly for the <code> integer </code> primitive. |
| 19 | */ |
| 20 | import java.util.EmptyStackException; |
| 21 | |
| 22 | public class IntStack { |
| 23 | private int[] list = null; |
| 24 | |
| 25 | private int size = 0; |
| 26 | |
| 27 | public IntStack() { |
| 28 | this(100); |
| 29 | } |
| 30 | |
| 31 | public IntStack(int maxdepth) { |
| 32 | super(); |
| 33 | list = new int[maxdepth]; |
| 34 | initialize(); |
| 35 | } |
| 36 | |
| 37 | public void clear() { |
| 38 | initialize(); |
| 39 | } |
| 40 | |
| 41 | public boolean empty() { |
| 42 | return size == 0; |
| 43 | } |
| 44 | |
| 45 | public int get(int slot) { |
| 46 | return list[slot]; |
| 47 | } |
| 48 | |
| 49 | private void initialize() { |
| 50 | for (int i = 0; i < list.length; i++) |
| 51 | list[i] = -1; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the int at the top of the stack without removing it |
| 56 | * |
| 57 | * @return int at the top of this stack. |
| 58 | * @exception EmptyStackException |
| 59 | * when empty. |
| 60 | */ |
| 61 | public int peek() { |
| 62 | if (size == 0) |
| 63 | throw new EmptyStackException(); |
| 64 | return list[size - 1]; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Removes and returns the int at the top of the stack |
| 69 | * |
| 70 | * @return int at the top of this stack. |
| 71 | * @exception EmptyStackException |
| 72 | * when empty. |
| 73 | */ |
| 74 | public int pop() { |
| 75 | int value = peek(); |
| 76 | list[size - 1] = -1; |
| 77 | size--; |
| 78 | return value; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Pushes an item onto the top of this stack. |
| 83 | * |
| 84 | * @param newValue - |
| 85 | * the int to be pushed onto this stack. |
| 86 | * @return the <code>newValue</code> argument. |
| 87 | */ |
| 88 | public int push(int newValue) { |
| 89 | if (size == list.length) { |
| 90 | throw new StackOverflowError(); |
| 91 | } |
| 92 | list[size++] = newValue; |
| 93 | return newValue; |
| 94 | } |
| 95 | |
| 96 | public int size() { |
| 97 | return list.length; |
| 98 | } |
| 99 | } |