|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.AbstractCollection<E> org.eigenbase.util.ArrayQueue<E>
public class ArrayQueue<E>
ArrayQueue is a queue implementation backed by an array. Grows by doubling
the existing size, but never shrinks. Queue entries are allowed to wrap
around array boundaries. ArrayQueue does not allow null
entries.
Contains the necessary methods to implement JDK 1.5's Queue interface. Also, some methods can be removed by extending JDK 1.5's AbstractQueue class.
Offering
(adding) items to the queue, polling
(removing) items from the queue, and peeking
at the head of the queue are normally constant time operations. The exception
is that growing the queue is an O(N) operation and can occur when offering an
item to the queue.
The Iterator
returned by iterator()
behaves somewhat
inconsistently with the general contract of Iterator. Read the documentation
for that method carefully.
Field Summary | |
---|---|
private int |
capacity
The current capacity (not size) of this queue. |
private static int |
DEFAULT_CAPACITY
|
private int |
end
The current position for the next element added to the queue. |
private E[] |
queue
The queue contents. |
private int |
start
The current position of the head element of the queue. |
Constructor Summary | |
---|---|
ArrayQueue()
Constructs an empty ArrayQueue with the default initial capacity of DEFAULT_CAPACITY. |
|
ArrayQueue(Collection<? extends E> c)
Constructs an ArrayQueue with the given contents. |
|
ArrayQueue(int capacity)
Constructs an empty ArrayQueue with the specified initial capacity. |
|
ArrayQueue(int capacity,
Collection<? extends E> c)
Constructs an ArrayQueue with the given contents and initial capacity. |
Method Summary | |
---|---|
boolean |
add(E o)
Adds the specified element to this queue. |
boolean |
addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this queue. |
void |
clear()
Removes all elements from the queue. |
private void |
copyQueueToArray(E[] otherQueue)
Copies the contents of the queue into an array. |
Object |
element()
Retrieves, but does not remove, the head of the queue. |
boolean |
equals(Object o)
Compares two queues for equality. |
private void |
grow()
Grows the queue to twice the current capacity. |
private int |
increment(int index)
Increments the given index by one modulo the queue's capacity. |
Iterator<E> |
iterator()
Returns an iterator over the elements in the queue in proper sequence. |
boolean |
offer(E o)
Inserts the specified element into this queue. |
E |
peek()
Retrieves, but does not remove the head of this queue, returning
null if this queue is empty. |
E |
poll()
Retrieves and removes the head of this queue, returning null
if this queue is empty. |
E |
remove()
Retrieves and removes the head of the queue. |
boolean |
remove(Object o)
Unsupported operation. |
boolean |
removeAll(Collection c)
Unsupported operation. |
boolean |
retainAll(Collection<?> c)
Unsupported operation. |
int |
size()
Returns the number of elements currently in the queue. |
Methods inherited from class java.util.AbstractCollection |
---|
contains, containsAll, isEmpty, toArray, toArray, toString |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.Collection |
---|
contains, containsAll, hashCode, isEmpty, toArray, toArray |
Field Detail |
---|
private static final int DEFAULT_CAPACITY
private int capacity
{link
#queue}.length
.
private E[] queue
private int start
private int end
Constructor Detail |
---|
public ArrayQueue(int capacity)
capacity
- the initial capacity of this queuepublic ArrayQueue()
public ArrayQueue(Collection<? extends E> c)
10
or c.size()
whichever is larger. The queue is populated with the elements
of c
in the order in which c
's iterator returns
them.
c
- a collection to use as the default contents of the queue
NullPointerException
- if c or any of its elements are nullpublic ArrayQueue(int capacity, Collection<? extends E> c)
capacity
is smaller than c.size()
,
the initial capacity will be c.size()
. The queue is
populated with the elements of c
in the order in which
c
's iterator returns them.
capacity
- the initial capacity of this queuec
- a collection to use as the default contents of the queue
NullPointerException
- if c or any of its elements are nullMethod Detail |
---|
public boolean offer(E o)
o
- the element to insert
false
if o is null
, otherwise
true
since it's always possible to add an element to this queue.public E peek()
null
if this queue is empty.
null
if the queue is emptypublic E poll()
null
if this queue is empty.
null
if the queue is emptypublic int size()
size
in interface Collection<E>
size
in class AbstractCollection<E>
public Iterator<E> iterator()
Iterator
is a "weakly consistent" iterator. It
will never throw ConcurrentModificationException
and
guarantees to traverse elements as they existed upon construction of the
iterator, but will never reflect any modifications subsequent to
construction.
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in class AbstractCollection<E>
public boolean remove(Object o)
remove
in interface Collection<E>
remove
in class AbstractCollection<E>
public boolean removeAll(Collection c)
removeAll
in interface Collection<E>
removeAll
in class AbstractCollection<E>
public boolean retainAll(Collection<?> c)
retainAll
in interface Collection<E>
retainAll
in class AbstractCollection<E>
private void grow()
public boolean equals(Object o)
equals
method.
equals
in interface Collection<E>
equals
in class Object
o
- the queue to compare this queue to
ClassCastException
- if o
is not an ArrayQueue.private void copyQueueToArray(E[] otherQueue)
otherQueue[0]
. Elements are copied in order.
otherQueue
- the array to copy data into,
otherQueue.length
must be greater than or equal to size()
.private int increment(int index)
index
- the index value to increment
capacity
public boolean add(E o)
add
in interface Collection<E>
add
in class AbstractCollection<E>
o
- the element
Collection.add(Object)
).
NullPointerException
- if o is null
IllegalStateException
- if the call to offer(Object)
failspublic boolean addAll(Collection<? extends E> c)
This implementation iterates over the specified collection, and adds
each element returned by the iterator to this collection, in turn. A
runtime exception encountered while trying to add an element (including,
in particular, a null
element) may result in only some of
the elements having been successfully added when the associated exception
is thrown.
addAll
in interface Collection<E>
addAll
in class AbstractCollection<E>
c
- collection to add to the queue
IllegalArgumentException
- if this == c
NullPointerException
- if c
or any of its elements are
null
.
IllegalStateException
- if the call to add(Object)
doespublic void clear()
poll()
repeatedly until it returns
null
.
clear
in interface Collection<E>
clear
in class AbstractCollection<E>
public Object element()
peek()
unless the queue is empty.
NoSuchElementException
- if the queue is emptypublic E remove()
poll()
unless the queue is empty.
NoSuchElementException
- if the queue is empty
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |