simple.util
Class BlockingQueue<E>

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

public class BlockingQueue<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
BlockingQueue(int size)
          Creates a new fixed size First In First Out queue of size size.
BlockingQueue(int size, int increment)
          Creates a new First In First 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 queue is full.
 boolean addAll(java.util.Collection<? extends E> c)
          Adds the collection to the queue 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 first element added or throws an exception if the queue is empty.
 boolean isEmpty()
           
 boolean isFull()
          Whether the queue is full or not.
 java.util.Iterator<E> iterator()
           
 boolean offer(E e)
          Attempts to add e to the queue.
 boolean offer(E e, long timeout, java.util.concurrent.TimeUnit unit)
          Attempts to add e to the queue.
 E peek()
          Returns the first element added or null if the queue is empty.
 E poll()
          Removes and returns the first element added or null if the queue is empty.
 E poll(long timeout, java.util.concurrent.TimeUnit unit)
          Removes and returns the first element added.
 void put(E e)
          Adds the element to the list, blocking if the queue is full.
 int remainingCapacity()
          Returns the remaining capacity of the queue.
 E remove()
          Removes and returns the first 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 first element added, blocking if the queue 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

BlockingQueue

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

Parameters:
size - Capacity of the queue

BlockingQueue

public BlockingQueue(int size,
                     int increment)
Creates a new First In First 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 queue 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 queue.

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 queue 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 queue. Will wait the specified time if the queue 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 queue 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 first element added. Will wait timeout unit if the queue is empty.

Specified by:
poll in interface java.util.concurrent.BlockingQueue<E>
Parameters:
timeout - Amount to wait
unit - Units to wait
Returns:
The first 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 queue 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 first element added, blocking if the queue is empty.

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

element

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

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

peek

public E peek()
Returns the first element added or null if the queue is empty.

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

poll

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

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

remove

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

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

remainingCapacity

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

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

isFull

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

Returns:
true if the queue is full. Always returns false if the queue 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 queue 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>