001    /**
002     * ========================================
003     * JCommon : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://www.jfree.org/jcommon/
007     *
008     * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: FastStack.java,v 1.2 2006/12/11 12:02:27 taqua Exp $
027     * ------------
028     * (C) Copyright 2002-2006, by Object Refinery Limited.
029     */
030    
031    package org.jfree.util;
032    
033    import java.io.Serializable;
034    import java.util.Arrays;
035    import java.util.EmptyStackException;
036    
037    /**
038     * A very simple unsynchronized stack. This one is faster than the
039     * java.util-Version.
040     *
041     * @author Thomas Morgner
042     */
043    public final class FastStack implements Serializable, Cloneable
044    {
045      private Object[] contents;
046      private int size;
047      private int initialSize;
048    
049      public FastStack()
050      {
051        initialSize = 10;
052      }
053    
054      public FastStack(int size)
055      {
056        initialSize = Math.max(1, size);
057      }
058    
059      public boolean isEmpty()
060      {
061        return size == 0;
062      }
063    
064      public int size()
065      {
066        return size;
067      }
068    
069      public void push(Object o)
070      {
071        if (contents == null)
072        {
073          contents = new Object[initialSize];
074          contents[0] = o;
075          size = 1;
076          return;
077        }
078    
079        final int oldSize = size;
080        size += 1;
081        if (contents.length == size)
082        {
083          // grow ..
084          final Object[] newContents = new Object[size + initialSize];
085          System.arraycopy(contents, 0, newContents, 0, size);
086          this.contents = newContents;
087        }
088        this.contents[oldSize] = o;
089      }
090    
091      public Object peek()
092      {
093        if (size == 0)
094        {
095          throw new EmptyStackException();
096        }
097        return contents[size - 1];
098      }
099    
100      public Object pop()
101      {
102        if (size == 0)
103        {
104          throw new EmptyStackException();
105        }
106        size -= 1;
107        final Object retval = contents[size];
108        contents[size] = null;
109        return retval;
110      }
111    
112      public Object clone()
113      {
114        try
115        {
116          FastStack stack = (FastStack) super.clone();
117          if (contents != null)
118          {
119            stack.contents = (Object[]) contents.clone();
120          }
121          return stack;
122        }
123        catch (CloneNotSupportedException cne)
124        {
125          throw new IllegalStateException("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 get(final int index)
139      {
140        if (index >= size)
141        {
142          throw new IndexOutOfBoundsException();
143        }
144        return contents[index];
145      }
146    }