FileDocCategorySizeDatePackage
ArrayStack.javaAPI DocApache Tomcat 6.0.145870Fri Jul 20 04:20:32 BST 2007org.apache.tomcat.util.digester

ArrayStack

public class ArrayStack extends ArrayList

Imported copy of the ArrayStack class from Commons Collections, which was the only direct dependency from Digester.

WARNNG - This class is public solely to allow it to be used from subpackages of org.apache.commons.digester. It should not be considered part of the public API of Commons Digester. If you want to use such a class yourself, you should use the one from Commons Collections directly.

An implementation of the {@link java.util.Stack} API that is based on an ArrayList instead of a Vector, so it is not synchronized to protect against multi-threaded access. The implementation is therefore operates faster in environments where you do not need to worry about multiple thread contention.

Unlike Stack, ArrayStack accepts null entries.

see
java.util.Stack
since
Digester 1.6 (from Commons Collections 1.0)

Fields Summary
private static final long
serialVersionUID
Ensure serialization compatibility
Constructors Summary
public ArrayStack()
Constructs a new empty ArrayStack. The initial size is controlled by ArrayList and is currently 10.


                         
      
        super();
    
public ArrayStack(int initialSize)
Constructs a new empty ArrayStack with an initial size.

param
initialSize the initial size to use
throws
IllegalArgumentException if the specified initial size is negative

        super(initialSize);
    
Methods Summary
public booleanempty()
Return true if this stack is currently empty.

This method exists for compatibility with java.util.Stack. New users of this class should use isEmpty instead.

return
true if the stack is currently empty

        return isEmpty();
    
public java.lang.Objectpeek()
Returns the top item off of this stack without removing it.

return
the top item on the stack
throws
EmptyStackException if the stack is empty

        int n = size();
        if (n <= 0) {
            throw new EmptyStackException();
        } else {
            return get(n - 1);
        }
    
public java.lang.Objectpeek(int n)
Returns the n'th item down (zero-relative) from the top of this stack without removing it.

param
n the number of items down to go
return
the n'th item on the stack, zero relative
throws
EmptyStackException if there are not enough items on the stack to satisfy this request

        int m = (size() - n) - 1;
        if (m < 0) {
            throw new EmptyStackException();
        } else {
            return get(m);
        }
    
public java.lang.Objectpop()
Pops the top item off of this stack and return it.

return
the top item on the stack
throws
EmptyStackException if the stack is empty

        int n = size();
        if (n <= 0) {
            throw new EmptyStackException();
        } else {
            return remove(n - 1);
        }
    
public java.lang.Objectpush(java.lang.Object item)
Pushes a new item onto the top of this stack. The pushed item is also returned. This is equivalent to calling add.

param
item the item to be added
return
the item just pushed

        add(item);
        return item;
    
public intsearch(java.lang.Object object)
Returns the one-based position of the distance from the top that the specified object exists on this stack, where the top-most element is considered to be at distance 1. If the object is not present on the stack, return -1 instead. The equals() method is used to compare to the items in this stack.

param
object the object to be searched for
return
the 1-based depth into the stack of the object, or -1 if not found

        int i = size() - 1;        // Current index
        int n = 1;                 // Current distance
        while (i >= 0) {
            Object current = get(i);
            if ((object == null && current == null) ||
                (object != null && object.equals(current))) {
                return n;
            }
            i--;
            n++;
        }
        return -1;