Class CircularFifoQueue<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- org.apache.commons.collections4.queue.CircularFifoQueue<E>
-
- Type Parameters:
E
- the type of elements in this collection
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<E>
,java.util.Collection<E>
,java.util.Queue<E>
,BoundedCollection<E>
public class CircularFifoQueue<E> extends java.util.AbstractCollection<E> implements java.util.Queue<E>, BoundedCollection<E>, java.io.Serializable
CircularFifoQueue is a first-in first-out queue with a fixed size that replaces its oldest element if full.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.
- Since:
- 4.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field 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 Summary
Constructors Constructor Description CircularFifoQueue()
Constructor that creates a queue with the default size of 32.CircularFifoQueue(int size)
Constructor that creates a queue with the specified size.CircularFifoQueue(java.util.Collection<? extends E> coll)
Constructor that creates a queue from the specified collection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.boolean
isAtFullCapacity()
Returnstrue
if the capacity limit of this queue has been reached, i.e.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.-
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
Serialization version.- See Also:
- Constant Field Values
-
elements
private transient E[] elements
Underlying storage array.
-
start
private transient int start
Array index of first (oldest) queue element.
-
end
private transient int end
Index mod maxElements of the array position following the last queue element. Queue elements start at elements[start] and "wrap around" elements[maxElements-1], ending at elements[decrement(end)]. For example, elements = {c,a,b}, start=1, end=1 corresponds to the queue [a,b,c].
-
full
private transient boolean full
Flag to indicate if the queue is currently full.
-
maxElements
private final int maxElements
Capacity of the queue.
-
-
Constructor Detail
-
CircularFifoQueue
public CircularFifoQueue()
Constructor that creates a queue with the default size of 32.
-
CircularFifoQueue
public CircularFifoQueue(int size)
Constructor that creates a queue with the specified size.- Parameters:
size
- the size of the queue (cannot be changed)- Throws:
java.lang.IllegalArgumentException
- if the size is < 1
-
CircularFifoQueue
public CircularFifoQueue(java.util.Collection<? extends E> coll)
Constructor that creates a queue from the specified collection. The collection size also sets the queue size.- Parameters:
coll
- the collection to copy into the queue, may not be null- Throws:
java.lang.NullPointerException
- if the collection is null
-
-
Method Detail
-
writeObject
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
Write the queue out using a custom routine.- Parameters:
out
- the output stream- Throws:
java.io.IOException
- if an I/O error occurs while writing to the output stream
-
readObject
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
Read the queue in using a custom routine.- Parameters:
in
- the input stream- Throws:
java.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 found
-
size
public int size()
Returns the number of elements stored in the queue.
-
isEmpty
public boolean isEmpty()
Returns true if this queue is empty; false otherwise.
-
isFull
public boolean isFull()
Returns true if this collection is full and no new elements can be added.A
CircularFifoQueue
can never be full, thus this returns alwaysfalse
.- Specified by:
isFull
in interfaceBoundedCollection<E>
- Returns:
- always returns
false
-
isAtFullCapacity
public boolean isAtFullCapacity()
Returnstrue
if the capacity limit of this queue has been reached, i.e. the number of elements stored in the queue equals its maximum size.- Returns:
true
if the capacity limit has been reached,false
otherwise- Since:
- 4.1
-
maxSize
public int maxSize()
Gets the maximum size of the collection (the bound).- Specified by:
maxSize
in interfaceBoundedCollection<E>
- Returns:
- the maximum number of elements the collection can hold
-
clear
public void clear()
Clears this queue.
-
add
public boolean add(E element)
Adds the given element to this queue. If the queue is full, the least recently added element is discarded so that a new element can be inserted.
-
get
public E get(int index)
Returns the element at the specified position in this queue.- Parameters:
index
- the position of the element in the queue- Returns:
- the element at position
index
- Throws:
java.util.NoSuchElementException
- if the requested position is outside the range [0, size)
-
offer
public boolean offer(E element)
Adds the given element to this queue. If the queue is full, the least recently added element is discarded so that a new element can be inserted.- Specified by:
offer
in interfacejava.util.Queue<E>
- Parameters:
element
- the element to add- Returns:
- true, always
- Throws:
java.lang.NullPointerException
- if the given element is null
-
increment
private int increment(int index)
Increments the internal index.- Parameters:
index
- the index to increment- Returns:
- the updated index
-
decrement
private int decrement(int index)
Decrements the internal index.- Parameters:
index
- the index to decrement- Returns:
- the updated index
-
iterator
public java.util.Iterator<E> iterator()
Returns an iterator over this queue's elements.
-
-