pivot.collections
Class ArrayQueue<T>

java.lang.Object
  extended by pivot.collections.ArrayList<T>
      extended by pivot.collections.ArrayQueue<T>
All Implemented Interfaces:
Serializable, Iterable<T>, Collection<T>, List<T>, Queue<T>, Sequence<T>

public class ArrayQueue<T>
extends ArrayList<T>
implements Queue<T>

Implementation of the Queue interface that is backed by an array.

TODO The current implementation is not optimal, since it requires shifting all elements on every call to enqueue(). Use an approach that maintains rotating headIndex and tailIndex values and override List methods to use these as offsets from the current capacity value. When the capacity needs to increase, we'll copy the elements in a contiguous block to the new array (we may want to make this operation a protected method so ArrayList can call it polymorphically).

Author:
gbrown
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface pivot.collections.List
List.ListListenerList<T>
 
Nested classes/interfaces inherited from interface pivot.collections.Sequence
Sequence.Search, Sequence.Sort, Sequence.Tree
 
Field Summary
 
Fields inherited from class pivot.collections.ArrayList
arrayList
 
Constructor Summary
ArrayQueue()
           
ArrayQueue(List<T> items)
           
 
Method Summary
 T dequeue()
          Removes the item from the head of the queue and returns it.
 void enqueue(T item)
          Enqueues an item.
 T peek()
          Returns the item at the head of the queue without removing it from the queue.
 
Methods inherited from class pivot.collections.ArrayList
add, clear, get, getComparator, getLength, getListListeners, indexOf, insert, iterator, remove, remove, setComparator, toArray, toString, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface pivot.collections.List
add, clear, getLength, getListListeners, insert, remove, setComparator, update
 
Methods inherited from interface pivot.collections.Sequence
get, indexOf, remove
 
Methods inherited from interface pivot.collections.Collection
getComparator
 
Methods inherited from interface java.lang.Iterable
iterator
 

Constructor Detail

ArrayQueue

public ArrayQueue()

ArrayQueue

public ArrayQueue(List<T> items)
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. getLength() can be used to distinguish between these two cases.

Specified by:
peek in interface Queue<T>