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: */package org.jfree.util;
030:
031: import java.io.Serializable;
032: import java.util.Arrays;
033: import java.util.EmptyStackException;
034:
035: /**
036: * A very simple unsynchronized stack. This one is faster than the
037: * java.util-Version.
038: *
039: * @author Thomas Morgner
040: */
041: public final class FastStack implements Serializable, Cloneable {
042: private Object[] contents;
043: private int size;
044: private int initialSize;
045:
046: public FastStack() {
047: initialSize = 10;
048: }
049:
050: public FastStack(int size) {
051: initialSize = Math.max(1, size);
052: }
053:
054: public boolean isEmpty() {
055: return size == 0;
056: }
057:
058: public int size() {
059: return size;
060: }
061:
062: public void push(Object o) {
063: if (contents == null) {
064: contents = new Object[initialSize];
065: contents[0] = o;
066: size = 1;
067: return;
068: }
069:
070: final int oldSize = size;
071: size += 1;
072: if (contents.length == size) {
073: // grow ..
074: final Object[] newContents = new Object[size + initialSize];
075: System.arraycopy(contents, 0, newContents, 0, size);
076: this .contents = newContents;
077: }
078: this .contents[oldSize] = o;
079: }
080:
081: public Object peek() {
082: if (size == 0) {
083: throw new EmptyStackException();
084: }
085: return contents[size - 1];
086: }
087:
088: public Object pop() {
089: if (size == 0) {
090: throw new EmptyStackException();
091: }
092: size -= 1;
093: final Object retval = contents[size];
094: contents[size] = null;
095: return retval;
096: }
097:
098: public Object clone() {
099: try {
100: FastStack stack = (FastStack) super .clone();
101: if (contents != null) {
102: stack.contents = (Object[]) contents.clone();
103: }
104: return stack;
105: } catch (CloneNotSupportedException cne) {
106: throw new IllegalStateException("Clone not supported? Why?");
107: }
108: }
109:
110: public void clear() {
111: size = 0;
112: if (contents != null) {
113: Arrays.fill(contents, null);
114: }
115: }
116:
117: public Object get(final int index) {
118: if (index >= size) {
119: throw new IndexOutOfBoundsException();
120: }
121: return contents[index];
122: }
123: }
|