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:
Serializable, Iterable<E>, Collection<E>, Queue<E>, BoundedCollection<E>
public class CircularFifoQueue<E>
extends AbstractCollection<E>
implements Queue<E>, BoundedCollection<E>, 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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate E[]Underlying storage array.private intIndex mod maxElements of the array position following the last queue element.private booleanFlag to indicate if the queue is currently full.private final intCapacity of the queue.private static final longSerialization version.private intArray index of first (oldest) queue element. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor that creates a queue with the default size of 32.CircularFifoQueue(int size) Constructor that creates a queue with the specified size.CircularFifoQueue(Collection<? extends E> coll) Constructor that creates a queue from the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds the given element to this queue.voidclear()Clears this queue.private intdecrement(int index) Decrements the internal index.element()get(int index) Returns the element at the specified position in this queue.private intincrement(int index) Increments the internal index.booleanReturnstrueif the capacity limit of this queue has been reached, i.e.booleanisEmpty()Returns true if this queue is empty; false otherwise.booleanisFull()Returns true if this collection is full and no new elements can be added.iterator()Returns an iterator over this queue's elements.intmaxSize()Gets the maximum size of the collection (the bound).booleanAdds the given element to this queue.peek()poll()private voidRead the queue in using a custom routine.remove()intsize()Returns the number of elements stored in the queue.private voidWrite the queue out using a custom routine.Methods inherited from class AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
addAll, contains, containsAll, equals, hashCode, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerialization version.- See Also:
-
elements
Underlying storage array. -
start
private transient int startArray index of first (oldest) queue element. -
end
private transient int endIndex 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 fullFlag to indicate if the queue is currently full. -
maxElements
private final int maxElementsCapacity of the queue.
-
-
Constructor Details
-
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:
IllegalArgumentException- if the size is < 1
-
CircularFifoQueue
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:
NullPointerException- if the collection is null
-
-
Method Details
-
writeObject
Write the queue out using a custom routine.- Parameters:
out- the output stream- Throws:
IOException- if an I/O error occurs while writing to the output stream
-
readObject
Read the queue in using a custom routine.- Parameters:
in- the input stream- Throws:
IOException- if an I/O error occurs while writing to the output streamClassNotFoundException- if the class of a serialized object can not be found
-
size
public int size()Returns the number of elements stored in the queue.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- this queue's size
-
isEmpty
public boolean isEmpty()Returns true if this queue is empty; false otherwise.- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
- true if this queue is empty
-
isFull
public boolean isFull()Returns true if this collection is full and no new elements can be added.A
CircularFifoQueuecan never be full, thus this returns alwaysfalse.- Specified by:
isFullin interfaceBoundedCollection<E>- Returns:
- always returns
false
-
isAtFullCapacity
public boolean isAtFullCapacity()Returnstrueif the capacity limit of this queue has been reached, i.e. the number of elements stored in the queue equals its maximum size.- Returns:
trueif the capacity limit has been reached,falseotherwise- Since:
- 4.1
-
maxSize
public int maxSize()Gets the maximum size of the collection (the bound).- Specified by:
maxSizein interfaceBoundedCollection<E>- Returns:
- the maximum number of elements the collection can hold
-
clear
public void clear()Clears this queue.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>
-
add
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:
addin interfaceCollection<E>- Specified by:
addin interfaceQueue<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
element- the element to add- Returns:
- true, always
- Throws:
NullPointerException- if the given element is null
-
get
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:
NoSuchElementException- if the requested position is outside the range [0, size)
-
offer
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:
offerin interfaceQueue<E>- Parameters:
element- the element to add- Returns:
- true, always
- Throws:
NullPointerException- if the given element is null
-
poll
-
element
-
peek
-
remove
-
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
-