public class CircularFifoQueue<E> extends java.util.AbstractCollection<E> implements java.util.Queue<E>, BoundedCollection<E>, java.io.Serializable
The removal order of a CircularFifoQueue
is based on the
insertion order; elements are removed in the same order in which they
were added. The iteration order is the same as the removal order.
The add(Object)
, remove()
, peek()
, poll()
,
offer(Object)
operations all perform in constant time.
All other operations perform in linear time or worse.
This queue prevents null objects from being added.
Modifier and Type | Field and Description |
---|---|
private E[] |
elements
Underlying storage array.
|
private int |
end
Index mod maxElements of the array position following the last queue
element.
|
private boolean |
full
Flag to indicate if the queue is currently full.
|
private int |
maxElements
Capacity of the queue.
|
private static long |
serialVersionUID
Serialization version.
|
private int |
start
Array index of first (oldest) queue element.
|
Constructor and Description |
---|
CircularFifoQueue()
Constructor that creates a queue with the default size of 32.
|
CircularFifoQueue(java.util.Collection<? extends E> coll)
Constructor that creates a queue from the specified collection.
|
CircularFifoQueue(int size)
Constructor that creates a queue with the specified size.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E element)
Adds the given element to this queue.
|
void |
clear()
Clears this queue.
|
private int |
decrement(int index)
Decrements the internal index.
|
E |
element() |
E |
get(int index)
Returns the element at the specified position in this queue.
|
private int |
increment(int index)
Increments the internal index.
|
private boolean |
isAtFullCapacity() |
boolean |
isEmpty()
Returns true if this queue is empty; false otherwise.
|
boolean |
isFull()
Returns true if this collection is full and no new elements can be added.
|
java.util.Iterator<E> |
iterator()
Returns an iterator over this queue's elements.
|
int |
maxSize()
Gets the maximum size of the collection (the bound).
|
boolean |
offer(E element)
Adds the given element to this queue.
|
E |
peek() |
E |
poll() |
private void |
readObject(java.io.ObjectInputStream in)
Read the queue in using a custom routine.
|
E |
remove() |
int |
size()
Returns the number of elements stored in the queue.
|
private void |
writeObject(java.io.ObjectOutputStream out)
Write the queue out using a custom routine.
|
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
private static final long serialVersionUID
private transient E[] elements
private transient int start
private transient int end
private transient boolean full
private final int maxElements
public CircularFifoQueue()
public CircularFifoQueue(int size)
size
- the size of the queue (cannot be changed)java.lang.IllegalArgumentException
- if the size is < 1public CircularFifoQueue(java.util.Collection<? extends E> coll)
coll
- the collection to copy into the queue, may not be nulljava.lang.NullPointerException
- if the collection is nullprivate void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
out
- the output streamjava.io.IOException
- if an I/O error occurs while writing to the output streamprivate void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
in
- the input streamjava.io.IOException
- if an I/O error occurs while writing to the output streamjava.lang.ClassNotFoundException
- if the class of a serialized object can not be foundpublic int size()
public boolean isEmpty()
public boolean isFull()
A CircularFifoQueue
can never be full, thus this returns always
false
.
isFull
in interface BoundedCollection<E>
false
private boolean isAtFullCapacity()
public int maxSize()
maxSize
in interface BoundedCollection<E>
public void clear()
public boolean add(E element)
public E get(int index)
index
- the position of the element in the queueindex
java.util.NoSuchElementException
- if the requested position is outside the range [0, size)public boolean offer(E element)
offer
in interface java.util.Queue<E>
element
- the element to addjava.lang.NullPointerException
- if the given element is nullprivate int increment(int index)
index
- the index to incrementprivate int decrement(int index)
index
- the index to decrementpublic java.util.Iterator<E> iterator()