01: /*
02: * Copyright 2000,2005 wingS development team.
03: *
04: * This file is part of wingS (http://wingsframework.org).
05: *
06: * wingS is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * Please see COPYING for the complete licence.
12: */
13: package org.wings.util;
14:
15: /**
16: * Non synchronized, fast stack. This stack never shrinks its internal
17: * data structure.
18: *
19: * @author <a href="mailto:H.Zeller@acm.org">Henner Zeller</a>
20: */
21: public final class FastStack {
22: private Object[] elements;
23: private int pos;
24:
25: public FastStack(int initialElements) {
26: pos = -1;
27: elements = new Object[initialElements];
28: }
29:
30: /**
31: * clear the stack.
32: */
33: public void clear() {
34: pos = -1;
35: }
36:
37: public boolean isEmpty() {
38: return pos == -1;
39: }
40:
41: /**
42: * push object to Stack.
43: */
44: public void push(Object o) {
45: ++pos;
46: if (pos == elements.length)
47: resize();
48: /*
49: * debugging hint: if you get an IndexOutOfBoundException here,
50: * maybe the last pop() operation was bogus in the first place ?
51: * It is not checked there for speed reasons.
52: */
53: elements[pos] = o;
54: }
55:
56: public Object peek() {
57: return elements[pos];
58: }
59:
60: /**
61: * pop element from stack. Does not return the actual element. If you
62: * need this, call peek().
63: */
64: public void pop() {
65: --pos;
66: }
67:
68: /**
69: * we only increase the size.
70: */
71: private void resize() {
72: Object[] newArray = new Object[elements.length * 2];
73: System.arraycopy(elements, 0, newArray, 0, elements.length);
74: elements = newArray;
75: }
76: }
|