Source for 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;
  34: import java.util.Arrays;
  35: import java.util.EmptyStackException;
  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, Cloneable
  44: {
  45:   private Object[] 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 o)
  70:   {
  71:     if (contents == null)
  72:     {
  73:       contents = new Object[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[] newContents = new Object[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 peek()
  92:   {
  93:     if (size == 0)
  94:     {
  95:       throw new EmptyStackException();
  96:     }
  97:     return contents[size - 1];
  98:   }
  99: 
 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: }