01: /*
02: * $Id: StringStack.java,v 1.3 2004/07/08 08:01:45 yuvalo Exp $
03: *
04: * (C) Copyright 2002-2004 by Yuval Oren. All rights reserved.
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package com.bluecast.util;
20:
21: /**
22: * A simple implementation of a stack of Strings
23: */
24: public class StringStack {
25: private String[] stack;
26: private int pos; // position of the top of the stack
27:
28: public StringStack(int initialSize) {
29: stack = new String[initialSize];
30: pos = -1;
31: }
32:
33: public String pop() {
34: if (pos >= 0) {
35: return stack[pos--];
36: } else {
37: return null;
38: }
39: }
40:
41: public void push(String s) {
42: if (pos + 1 < stack.length) {
43: stack[++pos] = s;
44: } else {
45: setSize(stack.length * 2);
46: stack[++pos] = s;
47: }
48: }
49:
50: public void setSize(int newSize) {
51: if (newSize != stack.length) {
52: String[] newStack = new String[newSize];
53: System.arraycopy(stack, 0, newStack, 0, Math.min(
54: stack.length, newSize));
55: stack = newStack;
56: }
57: }
58:
59: public void clear() {
60: pos = -1;
61: }
62:
63: public int size() {
64: return pos + 1;
65: }
66: }
|