Package org.apache.pivot.collections
Interface Stack<T>
-
- Type Parameters:
T
- The type of element kept in the stack.
- All Superinterfaces:
Collection<T>
,java.lang.Iterable<T>
- All Known Implementing Classes:
ArrayStack
,LinkedStack
,SynchronizedStack
public interface Stack<T> extends Collection<T>
Interface representing a last-in, first-out (LIFO) stack when unsorted, and a priority stack when sorted.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description int
getDepth()
int
getMaxDepth()
ListenerList<StackListener<T>>
getStackListeners()
boolean
isEmpty()
Tests the emptiness of the stack.T
peek()
Returns the item on top of the stack without removing it from the stack.T
pop()
Removes the top item from the stack and returns it.void
push(T item)
"Pushes" an item onto the stack.void
setMaxDepth(int maxDepth)
Set the maximum permitted stack/queue depth (0 = unlimited).-
Methods inherited from interface org.apache.pivot.collections.Collection
clear, getComparator, setComparator
-
-
-
-
Method Detail
-
push
void push(T item)
"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.
- Parameters:
item
- The item to push onto the stack.
-
pop
T pop()
Removes the top item from the stack and returns it.- Returns:
- The top item from the stack (removed from it).
- Throws:
java.lang.IllegalStateException
- If the stack contains no items.
-
peek
T peek()
Returns 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.- Returns:
- The top item from the stack (which remains there).
-
isEmpty
boolean isEmpty()
Tests the emptiness of the stack.- Specified by:
isEmpty
in interfaceCollection<T>
- Returns:
true
if the stack contains no items;false
, otherwise.
-
getDepth
int getDepth()
- Returns:
- The stack depth.
-
getMaxDepth
int getMaxDepth()
- Returns:
- The maximum permitted stack/queue length (0 = unlimited).
-
setMaxDepth
void setMaxDepth(int maxDepth)
Set the maximum permitted stack/queue depth (0 = unlimited).- Parameters:
maxDepth
- The new maximum depth.
-
getStackListeners
ListenerList<StackListener<T>> getStackListeners()
- Returns:
- The stack listener list.
-
-