simple.util
Class BlockingStack<E>

java.lang.Object
  extended by simple.util.BlockingStack<E>
All Implemented Interfaces:
java.lang.Iterable<E>, java.util.Collection<E>, java.util.concurrent.BlockingQueue<E>, java.util.Queue<E>

public class BlockingStack<E>
extends java.lang.Object
implements java.util.concurrent.BlockingQueue<E>

Blocks until the list has an item.
Created: Feb 8, 2010

Author:
Kenneth Pierce

Field Summary
private  boolean fixedSize
           
private  java.util.Vector<E> list
           
 
Constructor Summary
BlockingStack(int size)
          Creates a new fixed size First In Last Out queue of size size.
BlockingStack(int size, int increment)
          Creates a new First In Last Out queue with the initial size size.
 
Method Summary
 boolean add(E e)
          Attempts to add e to the stack, throwing an exception if the stack is full.
 boolean addAll(java.util.Collection<? extends E> c)
          Adds the collection to the stack if there is enough room.
 void clear()
           
 boolean contains(java.lang.Object e)
           
 boolean containsAll(java.util.Collection<?> c)
           
 int drainTo(java.util.Collection<? super E> c)
           
 int drainTo(java.util.Collection<? super E> c, int max)
           
 E element()
          Returns the last element added or throws an exception if the stack is empty.
 boolean isEmpty()
           
 boolean isFull()
          Whether the stack is full or not.
 java.util.Iterator<E> iterator()
           
 boolean offer(E e)
          Attempts to add e to the stack.
 boolean offer(E e, long timeout, java.util.concurrent.TimeUnit unit)
          Attempts to add e to the stack.
 E peek()
          Returns the last element added or null if the stack is empty.
 E poll()
          Removes and returns the last element added or null if the stack is empty.
 E poll(long timeout, java.util.concurrent.TimeUnit unit)
          Removes and returns the last element added.
 void put(E e)
          Adds the element to the list, blocking if the stack is full.
 int remainingCapacity()
          Returns the remaining capacity of the stack.
 E remove()
          Removes and returns the last element added.
 boolean remove(java.lang.Object e)
           
 boolean removeAll(java.util.Collection<?> c)
           
 boolean retainAll(java.util.Collection<?> c)
           
 int size()
           
 E take()
          Removes and returns the last element added, blocking if the stack is empty.
 java.lang.Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Field Detail

list

private final java.util.Vector<E> list

fixedSize

private final boolean fixedSize
Constructor Detail

BlockingStack

public BlockingStack(int size)
Creates a new fixed size First In Last Out queue of size size.

Parameters:
size - Size of the queue.

BlockingStack

public BlockingStack(int size,
                     int increment)
Creates a new First In Last Out queue with the initial size size.

Parameters:
size - Initial size of the queue
increment - Number of slots to add when full. If negative or zero the number of slots double.
Method Detail

add

public boolean add(E e)
Attempts to add e to the stack, throwing an exception if the stack is full.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.concurrent.BlockingQueue<E>
Specified by:
add in interface java.util.Queue<E>
Parameters:
e - The element to add.
Returns:
false if there was an underlying error.
Throws:
{@link - java.lang.IllegalStateException} if the stack is full.
{@link - java.lang.NullPointerException} if e is null.
See Also:
BlockingQueue.add(java.lang.Object), offer(Object)

offer

public boolean offer(E e)
Attempts to add e to the stack.

Specified by:
offer in interface java.util.concurrent.BlockingQueue<E>
Specified by:
offer in interface java.util.Queue<E>
Parameters:
e - Element to add.
Returns:
false if stack is full or there was an underlying error.
Throws:
{@link - java.lang.NullPointerException} if e is null
See Also:
BlockingQueue.offer(java.lang.Object), add(Object), offer(Object, long, TimeUnit)

offer

public boolean offer(E e,
                     long timeout,
                     java.util.concurrent.TimeUnit unit)
              throws java.lang.InterruptedException
Attempts to add e to the stack. Will wait the specified time if the stack is full.

Specified by:
offer in interface java.util.concurrent.BlockingQueue<E>
Parameters:
e - Element to add.
timeout - Length of time to wait.
unit - Units of time to wait.
Returns:
false if the waited time elapsed and the stack is still full or there was an underlying error.
Throws:
{@link - java.lang.InterruptedException} if interrupted
{@link - java.lang.NullPointerException} if e is null
java.lang.InterruptedException
See Also:
BlockingQueue.offer(java.lang.Object, long, java.util.concurrent.TimeUnit)

poll

public E poll(long timeout,
              java.util.concurrent.TimeUnit unit)
       throws java.lang.InterruptedException
Removes and returns the last element added. Will wait timeout unit if the stack is empty.

Specified by:
poll in interface java.util.concurrent.BlockingQueue<E>
Parameters:
timeout - Amount to wait
unit - Units to wait
Returns:
The last element added or null if timed out.
Throws:
{@link - java.lang.InterruptedException} if interrupted.
java.lang.InterruptedException
See Also:
BlockingQueue.poll(long, java.util.concurrent.TimeUnit)

put

public void put(E e)
         throws java.lang.InterruptedException
Adds the element to the list, blocking if the stack is full.

Specified by:
put in interface java.util.concurrent.BlockingQueue<E>
Throws:
{@link - java.lang.InterruptedException} if interrupted.
java.lang.InterruptedException
See Also:
BlockingQueue.put(java.lang.Object)

remove

public boolean remove(java.lang.Object e)
Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.concurrent.BlockingQueue<E>

take

public E take()
       throws java.lang.InterruptedException
Removes and returns the last element added, blocking if the stack is empty.

Specified by:
take in interface java.util.concurrent.BlockingQueue<E>
Returns:
The last element added.
Throws:
java.lang.InterruptedException
See Also:
BlockingQueue.take(), peek(), poll(), poll(long, TimeUnit)

element

public E element()
Returns the last element added or throws an exception if the stack is empty.

Specified by:
element in interface java.util.Queue<E>
Returns:
The last element added.
Throws:
{@link - java.util.NoSuchElementException} if the stack is empty.
See Also:
Queue.element(), peek(), take()

peek

public E peek()
Returns the last element added or null if the stack is empty.

Specified by:
peek in interface java.util.Queue<E>
Returns:
the last element added or null if the stack is empty.
See Also:
Queue.peek(), element(), take()

poll

public E poll()
Removes and returns the last element added or null if the stack is empty.

Specified by:
poll in interface java.util.Queue<E>
Returns:
The last element added or null if the stack is empty.
See Also:
Queue.poll(), remove(), poll(long, TimeUnit)

remove

public E remove()
Removes and returns the last element added. Throws an exception if the list is empty.

Specified by:
remove in interface java.util.Queue<E>
Returns:
The last element added.
Throws:
{@link - java.util.NoSuchElementException} if empty.
See Also:
Queue.remove(), poll()

remainingCapacity

public int remainingCapacity()
Returns the remaining capacity of the stack.

Specified by:
remainingCapacity in interface java.util.concurrent.BlockingQueue<E>
Returns:
The remaining capacity of the stack or Integer.MAX_VALUE if the size is not fixed.
See Also:
BlockingQueue.remainingCapacity()

isFull

public boolean isFull()
Whether the stack is full or not.

Returns:
true if the stack is full. Always returns false if the stack is not a fixed size.

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Collection<E>

size

public int size()
Specified by:
size in interface java.util.Collection<E>

clear

public void clear()
Specified by:
clear in interface java.util.Collection<E>

addAll

public boolean addAll(java.util.Collection<? extends E> c)
Adds the collection to the stack if there is enough room.

Specified by:
addAll in interface java.util.Collection<E>
Returns:
true if all were added, false otherwise.
See Also:
Collection.addAll(java.util.Collection)

containsAll

public boolean containsAll(java.util.Collection<?> c)
Specified by:
containsAll in interface java.util.Collection<E>

iterator

public java.util.Iterator<E> iterator()
Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>

removeAll

public boolean removeAll(java.util.Collection<?> c)
Specified by:
removeAll in interface java.util.Collection<E>

retainAll

public boolean retainAll(java.util.Collection<?> c)
Specified by:
retainAll in interface java.util.Collection<E>

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.Collection<E>

toArray

public <T> T[] toArray(T[] a)
Specified by:
toArray in interface java.util.Collection<E>

contains

public boolean contains(java.lang.Object e)
Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.concurrent.BlockingQueue<E>

drainTo

public int drainTo(java.util.Collection<? super E> c)
Specified by:
drainTo in interface java.util.concurrent.BlockingQueue<E>

drainTo

public int drainTo(java.util.Collection<? super E> c,
                   int max)
Specified by:
drainTo in interface java.util.concurrent.BlockingQueue<E>