KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > util > FastStack


1 /*
2  * $Id: FastStack.java,v 1.3 2004/12/01 07:54:30 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.util;
15
16 /**
17  * Non synchronized, fast stack. This stack never shrinks its internal
18  * data structure.
19  *
20  * @author <a HREF="mailto:H.Zeller@acm.org">Henner Zeller</a>
21  * @version $Revision: 1.3 $
22  */

23 public final class FastStack {
24     private Object JavaDoc[] elements;
25     private int pos;
26
27     public FastStack(int initialElements) {
28         pos = -1;
29         elements = new Object JavaDoc[initialElements];
30     }
31
32     /**
33      * clear the stack.
34      */

35     public void clear() {
36         pos = -1;
37     }
38
39     public boolean isEmpty() {
40         return pos == -1;
41     }
42
43     /**
44      * push object to Stack.
45      */

46     public void push(Object JavaDoc o) {
47         ++pos;
48         if (pos == elements.length) resize();
49         /*
50          * debugging hint: if you get an IndexOutOfBoundException here,
51          * maybe the last pop() operation was bogus in the first place ?
52          * It is not checked there for speed reasons.
53          */

54         elements[pos] = o;
55     }
56
57     public Object JavaDoc peek() {
58         return elements[pos];
59     }
60
61     /**
62      * pop element from stack. Does not return the actual element. If you
63      * need this, call peek().
64      */

65     public void pop() {
66         --pos;
67     }
68
69     /**
70      * we only increase the size.
71      */

72     private void resize() {
73         Object JavaDoc[] newArray = new Object JavaDoc[elements.length * 2];
74         System.arraycopy(elements, 0, newArray, 0, elements.length);
75         elements = newArray;
76     }
77 }
78
79
80
Popular Tags