一、前言
关于tomcat源码包基于java.util.ArrayList序列,定义org.apache.tomcat.util.digester.ArrayStack序列堆栈,详情源码说明部分。
二、源码说明
package org.apache.tomcat.util.digester;@b@@b@import java.util.ArrayList;@b@import java.util.EmptyStackException;@b@ @b@public class ArrayStack extends ArrayList {@b@@b@ /** Ensure serialization compatibility */ @b@ private static final long serialVersionUID = 2130079159931574599L;@b@@b@ /**@b@ * Constructs a new empty <code>ArrayStack</code>. The initial size@b@ * is controlled by <code>ArrayList</code> and is currently 10.@b@ */@b@ public ArrayStack() {@b@ super();@b@ }@b@@b@ /**@b@ * Constructs a new empty <code>ArrayStack</code> with an initial size.@b@ * @b@ * @param initialSize the initial size to use@b@ * @throws IllegalArgumentException if the specified initial size@b@ * is negative@b@ */@b@ public ArrayStack(int initialSize) {@b@ super(initialSize);@b@ }@b@@b@ /**@b@ * Return <code>true</code> if this stack is currently empty.@b@ * <p>@b@ * This method exists for compatibility with <code>java.util.Stack</code>.@b@ * New users of this class should use <code>isEmpty</code> instead.@b@ * @b@ * @return true if the stack is currently empty@b@ */@b@ public boolean empty() {@b@ return isEmpty();@b@ }@b@@b@ /**@b@ * Returns the top item off of this stack without removing it.@b@ *@b@ * @return the top item on the stack@b@ * @throws EmptyStackException if the stack is empty@b@ */@b@ public Object peek() throws EmptyStackException {@b@ int n = size();@b@ if (n <= 0) {@b@ throw new EmptyStackException();@b@ } else {@b@ return get(n - 1);@b@ }@b@ }@b@@b@ /**@b@ * Returns the n'th item down (zero-relative) from the top of this@b@ * stack without removing it.@b@ *@b@ * @param n the number of items down to go@b@ * @return the n'th item on the stack, zero relative@b@ * @throws EmptyStackException if there are not enough items on the@b@ * stack to satisfy this request@b@ */@b@ public Object peek(int n) throws EmptyStackException {@b@ int m = (size() - n) - 1;@b@ if (m < 0) {@b@ throw new EmptyStackException();@b@ } else {@b@ return get(m);@b@ }@b@ }@b@@b@ /**@b@ * Pops the top item off of this stack and return it.@b@ *@b@ * @return the top item on the stack@b@ * @throws EmptyStackException if the stack is empty@b@ */@b@ public Object pop() throws EmptyStackException {@b@ int n = size();@b@ if (n <= 0) {@b@ throw new EmptyStackException();@b@ } else {@b@ return remove(n - 1);@b@ }@b@ }@b@@b@ /**@b@ * Pushes a new item onto the top of this stack. The pushed item is also@b@ * returned. This is equivalent to calling <code>add</code>.@b@ *@b@ * @param item the item to be added@b@ * @return the item just pushed@b@ */@b@ public Object push(Object item) {@b@ add(item);@b@ return item;@b@ }@b@@b@@b@ /**@b@ * Returns the one-based position of the distance from the top that the@b@ * specified object exists on this stack, where the top-most element is@b@ * considered to be at distance <code>1</code>. If the object is not@b@ * present on the stack, return <code>-1</code> instead. The@b@ * <code>equals()</code> method is used to compare to the items@b@ * in this stack.@b@ *@b@ * @param object the object to be searched for@b@ * @return the 1-based depth into the stack of the object, or -1 if not found@b@ */@b@ public int search(Object object) {@b@ int i = size() - 1; // Current index@b@ int n = 1; // Current distance@b@ while (i >= 0) {@b@ Object current = get(i);@b@ if ((object == null && current == null) ||@b@ (object != null && object.equals(current))) {@b@ return n;@b@ }@b@ i--;@b@ n++;@b@ }@b@ return -1;@b@ }@b@@b@@b@}