Frames | No Frames |
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.3 2008/09/10 09:22:05 mungady 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: private Object[] contents; 45: private int size; 46: private int initialSize; 47: 48: /** 49: * Creates a new empty stack. 50: */ 51: public FastStack() { 52: this.initialSize = 10; 53: } 54: 55: /** 56: * Creates a new empty stack with the specified initial storage size. 57: * 58: * @param size the initial storage elements. 59: */ 60: public FastStack(int size) { 61: this.initialSize = Math.max(1, size); 62: } 63: 64: /** 65: * Returns <code>true</code> if the stack is empty, and <code>false</code> 66: * otherwise. 67: * 68: * @return A boolean. 69: */ 70: public boolean isEmpty() { 71: return this.size == 0; 72: } 73: 74: /** 75: * Returns the number of elements in the stack. 76: * 77: * @return The element count. 78: */ 79: public int size() { 80: return this.size; 81: } 82: 83: /** 84: * Pushes an object onto the stack. 85: * 86: * @param o the object. 87: */ 88: public void push(Object o) { 89: if (this.contents == null) { 90: this.contents = new Object[this.initialSize]; 91: this.contents[0] = o; 92: this.size = 1; 93: return; 94: } 95: 96: final int oldSize = this.size; 97: this.size += 1; 98: if (this.contents.length == this.size) { 99: // grow .. 100: final Object[] newContents = new Object[this.size 101: + this.initialSize]; 102: System.arraycopy(this.contents, 0, newContents, 0, this.size); 103: this.contents = newContents; 104: } 105: this.contents[oldSize] = o; 106: } 107: 108: /** 109: * Returns the object at the top of the stack without removing it. 110: * 111: * @return The object at the top of the stack. 112: */ 113: public Object peek() { 114: if (this.size == 0) { 115: throw new EmptyStackException(); 116: } 117: return this.contents[this.size - 1]; 118: } 119: 120: /** 121: * Removes and returns the object from the top of the stack. 122: * 123: * @return The object. 124: */ 125: public Object pop() { 126: if (this.size == 0) { 127: throw new EmptyStackException(); 128: } 129: this.size -= 1; 130: final Object retval = this.contents[this.size]; 131: this.contents[this.size] = null; 132: return retval; 133: } 134: 135: /** 136: * Returns a clone of the stack. 137: * 138: * @return A clone. 139: */ 140: public Object clone() { 141: try { 142: FastStack stack = (FastStack) super.clone(); 143: if (this.contents != null) { 144: stack.contents = (Object[]) this.contents.clone(); 145: } 146: return stack; 147: } 148: catch (CloneNotSupportedException cne) { 149: throw new IllegalStateException("Clone not supported? Why?"); 150: } 151: } 152: 153: /** 154: * Clears the stack. 155: */ 156: public void clear() { 157: this.size = 0; 158: if (this.contents != null) { 159: Arrays.fill(this.contents, null); 160: } 161: } 162: 163: /** 164: * Returns the item at the specified slot in the stack. 165: * 166: * @param index the index. 167: * 168: * @return The item. 169: */ 170: public Object get(final int index) { 171: if (index >= this.size) { 172: throw new IndexOutOfBoundsException(); 173: } 174: return this.contents[index]; 175: } 176: }