一、前言
关于jcommon包(1.0.9)定义org.jfree.util.FastStack堆栈类,通过对象数组实现,详情参见源码说明。
二、源码说明
package org.jfree.util;@b@@b@import java.io.Serializable;@b@import java.util.Arrays;@b@import java.util.EmptyStackException;@b@@b@public final class FastStack@b@ implements Serializable, Cloneable@b@{@b@ private Object[] contents;@b@ private int size;@b@ private int initialSize;@b@@b@ public FastStack()@b@ {@b@ this.initialSize = 10;@b@ }@b@@b@ public FastStack(int size)@b@ {@b@ this.initialSize = Math.max(1, size);@b@ }@b@@b@ public boolean isEmpty()@b@ {@b@ return (this.size == 0);@b@ }@b@@b@ public int size()@b@ {@b@ return this.size;@b@ }@b@@b@ public void push(Object o)@b@ {@b@ if (this.contents == null)@b@ {@b@ this.contents = new Object[this.initialSize];@b@ this.contents[0] = o;@b@ this.size = 1;@b@ return;@b@ }@b@@b@ int oldSize = this.size;@b@ this.size += 1;@b@ if (this.contents.length == this.size)@b@ {@b@ Object[] newContents = new Object[this.size + this.initialSize];@b@ System.arraycopy(this.contents, 0, newContents, 0, this.size);@b@ this.contents = newContents;@b@ }@b@ this.contents[oldSize] = o;@b@ }@b@@b@ public Object peek()@b@ {@b@ if (this.size == 0)@b@ {@b@ throw new EmptyStackException();@b@ }@b@ return this.contents[(this.size - 1)];@b@ }@b@@b@ public Object pop()@b@ {@b@ if (this.size == 0)@b@ {@b@ throw new EmptyStackException();@b@ }@b@ this.size -= 1;@b@ Object retval = this.contents[this.size];@b@ this.contents[this.size] = null;@b@ return retval;@b@ }@b@@b@ public Object clone()@b@ {@b@ FastStack stack;@b@ try {@b@ stack = (FastStack)super.clone();@b@ if (this.contents != null)@b@ {@b@ stack.contents = ((Object[])(Object[])this.contents.clone());@b@ }@b@ return stack;@b@ }@b@ catch (CloneNotSupportedException cne)@b@ {@b@ throw new IllegalStateException("Clone not supported? Why?");@b@ }@b@ }@b@@b@ public void clear()@b@ {@b@ this.size = 0;@b@ if (this.contents != null)@b@ {@b@ Arrays.fill(this.contents, null);@b@ }@b@ }@b@@b@ public Object get(int index)@b@ {@b@ if (index >= this.size)@b@ {@b@ throw new IndexOutOfBoundsException();@b@ }@b@ return this.contents[index];@b@ }@b@}