KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * ========================================
3  * JCommon : a free Java report library
4  * ========================================
5  *
6  * Project Info: http://www.jfree.org/jcommon/
7  *
8  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23  * in the United States and other countries.]
24  *
25  * ------------
26  * $Id: FastStack.java,v 1.2 2006/12/11 12:02:27 taqua Exp $
27  * ------------
28  * (C) Copyright 2002-2006, by Object Refinery Limited.
29  */

30
31 package org.jfree.util;
32
33 import java.io.Serializable JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.EmptyStackException JavaDoc;
36
37 /**
38  * A very simple unsynchronized stack. This one is faster than the
39  * java.util-Version.
40  *
41  * @author Thomas Morgner
42  */

43 public final class FastStack implements Serializable JavaDoc, Cloneable JavaDoc
44 {
45   private Object JavaDoc[] contents;
46   private int size;
47   private int initialSize;
48
49   public FastStack()
50   {
51     initialSize = 10;
52   }
53
54   public FastStack(int size)
55   {
56     initialSize = Math.max(1, size);
57   }
58
59   public boolean isEmpty()
60   {
61     return size == 0;
62   }
63
64   public int size()
65   {
66     return size;
67   }
68
69   public void push(Object JavaDoc o)
70   {
71     if (contents == null)
72     {
73       contents = new Object JavaDoc[initialSize];
74       contents[0] = o;
75       size = 1;
76       return;
77     }
78
79     final int oldSize = size;
80     size += 1;
81     if (contents.length == size)
82     {
83       // grow ..
84
final Object JavaDoc[] newContents = new Object JavaDoc[size + initialSize];
85       System.arraycopy(contents, 0, newContents, 0, size);
86       this.contents = newContents;
87     }
88     this.contents[oldSize] = o;
89   }
90
91   public Object JavaDoc peek()
92   {
93     if (size == 0)
94     {
95       throw new EmptyStackException JavaDoc();
96     }
97     return contents[size - 1];
98   }
99
100   public Object JavaDoc pop()
101   {
102     if (size == 0)
103     {
104       throw new EmptyStackException JavaDoc();
105     }
106     size -= 1;
107     final Object JavaDoc retval = contents[size];
108     contents[size] = null;
109     return retval;
110   }
111
112   public Object JavaDoc clone()
113   {
114     try
115     {
116       FastStack stack = (FastStack) super.clone();
117       if (contents != null)
118       {
119         stack.contents = (Object JavaDoc[]) contents.clone();
120       }
121       return stack;
122     }
123     catch (CloneNotSupportedException JavaDoc cne)
124     {
125       throw new IllegalStateException JavaDoc("Clone not supported? Why?");
126     }
127   }
128
129   public void clear()
130   {
131     size = 0;
132     if (contents != null)
133     {
134       Arrays.fill(contents, null);
135     }
136   }
137
138   public Object JavaDoc get(final int index)
139   {
140     if (index >= size)
141     {
142       throw new IndexOutOfBoundsException JavaDoc();
143     }
144     return contents[index];
145   }
146 }
147
Popular Tags