org.apache.pivot.collections.concurrent
Class SynchronizedQueue<T>

java.lang.Object
  extended by org.apache.pivot.collections.concurrent.SynchronizedQueue<T>
All Implemented Interfaces:
Iterable<T>, Collection<T>, Queue<T>

public class SynchronizedQueue<T>
extends Object
implements Queue<T>

Synchronized implementation of the Queue interface.


Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.pivot.collections.Queue
Queue.QueueListenerList<T>
 
Constructor Summary
SynchronizedQueue(Queue<T> queue)
           
 
Method Summary
 void clear()
          Removes all elements from the collection.
 T dequeue()
          Removes the item from the head of the queue and returns it.
 void enqueue(T item)
          Enqueues an item.
 Comparator<T> getComparator()
          Returns the collection's sort order.
 int getLength()
          Returns the length of the queue.
 ListenerList<QueueListener<T>> getQueueListeners()
          Returns the queue listener list.
 boolean isEmpty()
          Tests the emptiness of the queue.
 Iterator<T> iterator()
          NOTE Callers must manually synchronize on the SynchronizedQueue instance to ensure thread safety during iteration.
 T peek()
          Returns the item at the head of the queue without removing it from the queue.
 void setComparator(Comparator<T> comparator)
          Sets the collection's sort order, re-ordering the collection's contents and ensuring that new entries preserve the sort order.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SynchronizedQueue

public SynchronizedQueue(Queue<T> queue)
Method Detail

enqueue

public void enqueue(T item)
Description copied from interface: Queue
Enqueues an item. If the queue is unsorted, the item is added at the tail of the queue (index 0). Otherwise, it is inserted at the appropriate index.

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

dequeue

public T dequeue()
Description copied from interface: Queue
Removes the item from the head of the queue and returns it. Calling this method should have the same effect as: remove(getLength() - 1, 1);

Specified by:
dequeue in interface Queue<T>

peek

public T peek()
Description copied from interface: Queue
Returns the item at the head of the queue without removing it from the queue. Returns null if the queue contains no items. Will also return null if the head item in the queue is null. isEmpty() can be used to distinguish between these two cases.

Specified by:
peek in interface Queue<T>

clear

public void clear()
Description copied from interface: Collection
Removes all elements from the collection.

Specified by:
clear in interface Collection<T>

isEmpty

public boolean isEmpty()
Description copied from interface: Queue
Tests the emptiness of the queue.

Specified by:
isEmpty in interface Collection<T>
Specified by:
isEmpty in interface Queue<T>
Returns:
true if the queue contains no items; false, otherwise.

getLength

public int getLength()
Description copied from interface: Queue
Returns the length of the queue.

Specified by:
getLength in interface Queue<T>

getComparator

public Comparator<T> getComparator()
Description copied from interface: Collection
Returns the collection's sort order.

Specified by:
getComparator in interface Collection<T>
Returns:
The comparator used to order elements in the collection, or null if the sort order is undefined.
See Also:
Collection.setComparator(Comparator)

setComparator

public void setComparator(Comparator<T> comparator)
Description copied from interface: Collection
Sets the collection's sort order, re-ordering the collection's contents and ensuring that new entries preserve the sort order.

Calling this method more than once with the same comparator will re-sort the collection.

Specified by:
setComparator in interface Collection<T>
Parameters:
comparator - The comparator used to order elements in the collection, or null if the collection is unsorted.

iterator

public Iterator<T> iterator()
NOTE Callers must manually synchronize on the SynchronizedQueue instance to ensure thread safety during iteration.

Specified by:
iterator in interface Iterable<T>

getQueueListeners

public ListenerList<QueueListener<T>> getQueueListeners()
Description copied from interface: Queue
Returns the queue listener list.

Specified by:
getQueueListeners in interface Queue<T>