Package org.apache.pivot.collections
Class ArrayQueue<T>
- java.lang.Object
-
- org.apache.pivot.collections.ArrayQueue<T>
-
- Type Parameters:
T
- The type of object stored in this queue.
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<T>
,Collection<T>
,Queue<T>
public class ArrayQueue<T> extends java.lang.Object implements Queue<T>, java.io.Serializable
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ArrayQueue()
Construct an empty queue, with all defaults.ArrayQueue(int capacity)
Construct an empty queue with a given initial capacity.ArrayQueue(int capacity, int maxLen)
Construct an empty queue with a given initial capacity and maxmimum length.ArrayQueue(int capacity, int maxLen, java.util.Comparator<T> comparator)
Construct an empty queue with a given initial capacity, maximum length, and comparator for ordering the queue.ArrayQueue(java.util.Comparator<T> comparator)
Construct an empty queue with the given comparator used to order the elements in it.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.void
ensureCapacity(int capacity)
Ensure that the queue has sufficient internal capacity to satisfy the given number of elements.java.util.Comparator<T>
getComparator()
Returns the collection's sort order.int
getLength()
int
getMaxLength()
ListenerList<QueueListener<T>>
getQueueListeners()
boolean
isEmpty()
Tests the emptiness of the queue.java.util.Iterator<T>
iterator()
T
peek()
Returns the item at the head of the queue without removing it from the queue.void
setComparator(java.util.Comparator<T> comparator)
Set/remove the comparator for this queue.void
setMaxLength(int maxLen)
Set the maximum allowed queue length (0 means unlimited).
-
-
-
Constructor Detail
-
ArrayQueue
public ArrayQueue()
Construct an empty queue, with all defaults.
-
ArrayQueue
public ArrayQueue(java.util.Comparator<T> comparator)
Construct an empty queue with the given comparator used to order the elements in it.- Parameters:
comparator
- A comparator used to sort the elements in the queue as they are added.
-
ArrayQueue
public ArrayQueue(int capacity)
Construct an empty queue with a given initial capacity.- Parameters:
capacity
- The initial capacity for the queue.
-
ArrayQueue
public ArrayQueue(int capacity, int maxLen)
Construct an empty queue with a given initial capacity and maxmimum length.- Parameters:
capacity
- The initial capacity for the queue.maxLen
- The maximum permitted queue length.
-
ArrayQueue
public ArrayQueue(int capacity, int maxLen, java.util.Comparator<T> comparator)
Construct an empty queue with a given initial capacity, maximum length, and comparator for ordering the queue.- Parameters:
capacity
- The initial capacity for the queue.maxLen
- The maximum permitted queue length.comparator
- The comparator to use for ordering the elements.
-
-
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 (index0
). Otherwise, it is inserted at the appropriate index according to the priority/sort order.If there is a maximum queue length defined and the queue is already at the maximum length this new item will not be queued.
-
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);
-
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.
-
clear
public void clear()
Description copied from interface:Collection
Removes all elements from the collection.- Specified by:
clear
in interfaceCollection<T>
-
isEmpty
public boolean isEmpty()
Description copied from interface:Queue
Tests the emptiness of the queue.
-
getLength
public int getLength()
-
getMaxLength
public int getMaxLength()
- Specified by:
getMaxLength
in interfaceQueue<T>
- Returns:
- The maximum queue length allowed (0 means unlimited).
-
setMaxLength
public void setMaxLength(int maxLen)
Description copied from interface:Queue
Set the maximum allowed queue length (0 means unlimited).- Specified by:
setMaxLength
in interfaceQueue<T>
- Parameters:
maxLen
- The maximum allowed length.
-
ensureCapacity
public void ensureCapacity(int capacity)
Ensure that the queue has sufficient internal capacity to satisfy the given number of elements.- Parameters:
capacity
- The capacity to ensure (to make sure no further allocations are done to accommodate this number of elements).
-
getComparator
public java.util.Comparator<T> getComparator()
Description copied from interface:Collection
Returns the collection's sort order.- Specified by:
getComparator
in interfaceCollection<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(java.util.Comparator<T> comparator)
Set/remove the comparator for this queue.If a new comparator is set the queue will be reordered if it contains any elements. Removing the comparator will not reorder any elements.
Calls the
QueueListener.comparatorChanged(org.apache.pivot.collections.Queue<T>, java.util.Comparator<T>)
method for each registered listener after setting the new comparator.- Specified by:
setComparator
in interfaceCollection<T>
- Parameters:
comparator
- The new comparator used to order the elements in the queue. Can benull
to remove the existing comparator.
-
iterator
public java.util.Iterator<T> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<T>
-
getQueueListeners
public ListenerList<QueueListener<T>> getQueueListeners()
- Specified by:
getQueueListeners
in interfaceQueue<T>
- Returns:
- The queue listener list.
-
-