net.sourceforge.hiveutils.collections.impl
Class QueueImpl<T>

java.lang.Object
  extended by net.sourceforge.hiveutils.collections.impl.QueueImpl<T>
All Implemented Interfaces:
Queue<T>

public class QueueImpl<T>
extends java.lang.Object
implements Queue<T>

Implementation of Queue based on ArrayList.

Author:
Jean-Francois Poilpret

Constructor Summary
QueueImpl()
           
 
Method Summary
 void add(T o)
          Add an item at the top of this Queue.
 boolean isEmpty()
          Indicates if this Queue is empty.
 java.util.List<T> take()
          Take all the items currently in this Queue.
 java.util.List<T> take(int minCount)
          Take all the items currently in this Queue, with a minimum number of items.
 java.util.List<T> take(int minCount, long timeout)
          Take all the items currently in this Queue, with a minimum number of items.
 java.util.List<T> take(long timeout)
          Take all the items currently in this Queue.
 void unblock()
          Unblocks all threads currently waiting on the Take method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QueueImpl

public QueueImpl()
Method Detail

isEmpty

public boolean isEmpty()
Description copied from interface: Queue
Indicates if this Queue is empty.

Specified by:
isEmpty in interface Queue<T>
Returns:
true if the queue is empty

add

public void add(T o)
Description copied from interface: Queue
Add an item at the top of this Queue.

Specified by:
add in interface Queue<T>
Parameters:
o - the item to add to the queue

unblock

public void unblock()
Description copied from interface: Queue
Unblocks all threads currently waiting on the Take method. This can be useful at shutdown time to unblock a thread that would wait indefinately for a minimum number of items: you can unblock this thread so that it will receive all current items, even if this amount is less than the minimum required.

Specified by:
unblock in interface Queue<T>

take

public java.util.List<T> take()
Description copied from interface: Queue
Take all the items currently in this Queue. If the queue is empty, block until one item (at least) is added to it. The returned items are removed from the Queue.

It is possible to unblock this method by calling unblock.

Specified by:
take in interface Queue<T>
Returns:
the list of all items in the queue, in FIFO order.

take

public java.util.List<T> take(int minCount)
Description copied from interface: Queue
Take all the items currently in this Queue, with a minimum number of items. If the queue is empty or does not contain the required amount, block until this amount of items has been added to the Queue. The returned items are removed from the Queue.

Note that if several threads are blocked on this Queue, the first unblocked thread is unpredictable but will generally be the one with the least requirements.

It is possible to unblock this method by calling unblock.

Specified by:
take in interface Queue<T>
Parameters:
minCount - minimum number of items that must be returned
Returns:
the list of all items in the queue, in FIFO order.

take

public java.util.List<T> take(long timeout)
Description copied from interface: Queue
Take all the items currently in this Queue. If the queue is empty, block until one item (at least) is added to it or until the given timeout has ellapsed. The returned items are removed from the Queue.

Specified by:
take in interface Queue<T>
Parameters:
timeout - maximum number of milliseconds to wait until the queue has an item to be returned by this method
Returns:
the list of all items in the queue, in FIFO order.

take

public java.util.List<T> take(int minCount,
                              long timeout)
Description copied from interface: Queue
Take all the items currently in this Queue, with a minimum number of items. If the queue is empty or does not contain the required amount, block until this amount of items has been added to the Queue or until the given timeout has ellapsed. The returned items are removed from the Queue.

Note that if several threads are blocked on this Queue, the first unblocked thread is unpredictable but will generally be the one with the least requirements.

Specified by:
take in interface Queue<T>
Parameters:
minCount - minimum number of items that must be returned
timeout - maximum number of milliseconds to wait until the queue has an item to be returned by this method
Returns:
the list of all items in the queue, in FIFO order.