Class ArrayStack<T>
- java.lang.Object
- 
- org.apache.pivot.collections.ArrayStack<T>
 
- 
- Type Parameters:
- T- The type of elements stored in this stack.
 - All Implemented Interfaces:
- java.io.Serializable,- java.lang.Iterable<T>,- Collection<T>,- Stack<T>
 
 public class ArrayStack<T> extends java.lang.Object implements Stack<T>, java.io.Serializable Implementation of theStackinterface that is backed by anArrayList.Note: a stack with a comparator set is more like a priority queue than a stack because it no longer maintains the LIFO (last-in, first-out) ordering of a normal stack. - See Also:
- Serialized Form
 
- 
- 
Constructor SummaryConstructors Constructor Description ArrayStack()Construct an empty stack, with all defaults.ArrayStack(int capacity)Construct an empty stack with the given initial capacity.ArrayStack(int capacity, int depth)Construct an empty stack with the given initial capacity and maximum depth.ArrayStack(int capacity, int depth, java.util.Comparator<T> comparator)Construct an empty stack with the given initial capacity, maximum stack depth, and comparator used to order the stack elements.ArrayStack(java.util.Comparator<T> comparator)Construct an empty stack with the given comparator used to order the elemnts in it.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all elements from the collection.voidensureCapacity(int capacity)Ensure that the stack has sufficient internal capacity to satisfy the given number of elements.java.util.Comparator<T>getComparator()Returns the collection's sort order.intgetDepth()intgetMaxDepth()ListenerList<StackListener<T>>getStackListeners()booleanisEmpty()Tests the emptiness of the stack.java.util.Iterator<T>iterator()Tpeek()Returns the item on top of the stack without removing it from the stack.Tpop()Removes the top item from the stack and returns it.voidpush(T item)"Pushes" an item onto the stack.voidsetComparator(java.util.Comparator<T> comparator)Set/remove the comparator for this stack.voidsetMaxDepth(int depth)Set the maximum depth permitted for this stack, 0 means unlimited.
 
- 
- 
- 
Constructor Detail- 
ArrayStackpublic ArrayStack() Construct an empty stack, with all defaults.
 - 
ArrayStackpublic ArrayStack(java.util.Comparator<T> comparator) Construct an empty stack with the given comparator used to order the elemnts in it.- Parameters:
- comparator- A comparator used to sort the elements in the stack as they are pushed.
 
 - 
ArrayStackpublic ArrayStack(int capacity) Construct an empty stack with the given initial capacity.- Parameters:
- capacity- The initial capacity for the stack.
 
 - 
ArrayStackpublic ArrayStack(int capacity, int depth)Construct an empty stack with the given initial capacity and maximum depth.- Parameters:
- capacity- The initial capacity for the stack.
- depth- The maximum depth to enforce for this stack (0 = unlimited).
 
 - 
ArrayStackpublic ArrayStack(int capacity, int depth, java.util.Comparator<T> comparator)Construct an empty stack with the given initial capacity, maximum stack depth, and comparator used to order the stack elements.- Parameters:
- capacity- The initial stack capacity.
- depth- The maximum stack depth to enforce (0 = unlimited).
- comparator- The comparator to use to order the stack elements.
 
 
- 
 - 
Method Detail- 
getMaxDepthpublic int getMaxDepth() - Specified by:
- getMaxDepthin interface- Stack<T>
- Returns:
- The maximum depth this stack is permitted to reach, where 0 means unlimited.
 
 - 
setMaxDepthpublic void setMaxDepth(int depth) Set the maximum depth permitted for this stack, 0 means unlimited.- Specified by:
- setMaxDepthin interface- Stack<T>
- Parameters:
- depth- The new maximum depth for this stack.
 
 - 
pushpublic void push(T item) Description copied from interface:Stack"Pushes" an item onto the stack. If the stack is unsorted, the item is added at the top of the stack (getLength()). Otherwise, it is inserted at the appropriate index.If there is a maximum stack depth defined and the stack goes past this maximum depth, the deepest item (which could be this new item, depending on the comparator) will be removed. 
 - 
poppublic T pop() Description copied from interface:StackRemoves the top item from the stack and returns it.
 - 
peekpublic T peek() Description copied from interface:StackReturns the item on top of the stack without removing it from the stack. Returns null if the stack contains no items. Will also return null if the top item in the stack is null.getLength()can be used to distinguish between these two cases.
 - 
clearpublic void clear() Description copied from interface:CollectionRemoves all elements from the collection.- Specified by:
- clearin interface- Collection<T>
 
 - 
isEmptypublic boolean isEmpty() Description copied from interface:StackTests the emptiness of the stack.
 - 
getDepthpublic int getDepth() 
 - 
ensureCapacitypublic void ensureCapacity(int capacity) Ensure that the stack has sufficient internal capacity to satisfy the given number of elements.- Parameters:
- capacity- The capacity to ensure (to make sure no further allocations are done to accommodate this number of elements).
 
 - 
getComparatorpublic java.util.Comparator<T> getComparator() Description copied from interface:CollectionReturns the collection's sort order.- Specified by:
- getComparatorin interface- Collection<T>
- Returns:
- The comparator used to order elements in the collection, or
 nullif the sort order is undefined.
- See Also:
- Collection.setComparator(Comparator)
 
 - 
setComparatorpublic void setComparator(java.util.Comparator<T> comparator) Set/remove the comparator for this stack.Setting a non-null comparator changes this stack to a priority queue, because LIFO ordering is no longer maintained. If a new comparator is set the stack will be reordered if it contains any elements. Removing the comparator will not reorder any elements. Calls the StackListener.comparatorChanged(org.apache.pivot.collections.Stack<T>, java.util.Comparator<T>)method for each registered listener after setting the new comparator.- Specified by:
- setComparatorin interface- Collection<T>
- Parameters:
- comparator- The new comparator used to order the elements in the stack. Can be- nullto remove the existing comparator.
 
 - 
iteratorpublic java.util.Iterator<T> iterator() - Specified by:
- iteratorin interface- java.lang.Iterable<T>
 
 - 
getStackListenerspublic ListenerList<StackListener<T>> getStackListeners() - Specified by:
- getStackListenersin interface- Stack<T>
- Returns:
- The stack listener list.
 
 
- 
 
-