Top Description Fields Constructors Methods
org.apache.commons.collections4.queue

public Class CircularFifoQueue<E>

extends AbstractCollection<E>
implements Queue<E>, BoundedCollection<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.apache.commons.collections4.BoundedCollection, java.util.Collection, java.lang.Iterable, java.util.Queue
Type Parameters
<E>
the type of elements in this collection
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.util.AbstractCollection, .Arrays, .Collection, .Iterator, .NoSuchElementException, .Queue, org.apache.commons.collections4.BoundedCollection

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

Field Summary

Modifier and TypeField and Description
private transient E[]
elements

Underlying storage array.

private transient int
end

Index mod maxElements of the array position following the last queue element.

private transient boolean
full

Flag to indicate if the queue is currently full.

private final int
maxElements

Capacity of the queue.

private static final long
serialVersionUID

Serialization version.

private transient int
start

Array index of first (oldest) queue element.

Constructor Summary

AccessConstructor and Description
public
CircularFifoQueue()

Constructor that creates a queue with the default size of 32.

public
CircularFifoQueue(final int
the size of the queue (cannot be changed)
size
)

Constructor that creates a queue with the specified size.

public
CircularFifoQueue(final Collection<? extends E>
the collection to copy into the queue, may not be null
coll
)

Constructor that creates a queue from the specified collection.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true, always
add
(final E
the element to add
element
)

Overrides java.util.AbstractCollection.add.

Implements java.util.Queue.add, java.util.Collection.add.

Adds the given element to this queue.
public void
clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Collection.clear.

Clears this queue.
private int

Returns:

the updated index
decrement
(int
the index to decrement
index
)

Decrements the internal index.

public E
element()

Implements java.util.Queue.element.

Retrieves, but does not remove, the head of this queue.
public E

Returns:

the element at position index
get
(final int
the position of the element in the queue
index
)

Returns the element at the specified position in this queue.

private int

Returns:

the updated index
increment
(int
the index to increment
index
)

Increments the internal index.

public boolean

Returns:

true if the capacity limit has been reached, false otherwise
isAtFullCapacity
()

Returns true if the capacity limit of this queue has been reached, i.e. the number of elements stored in the queue equals its maximum size.

public boolean

Returns:

true if this queue is empty
isEmpty
()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Returns true if this queue is empty; false otherwise.
public boolean

Returns:

always returns false
isFull
()

Implements org.apache.commons.collections4.BoundedCollection.isFull.

Returns true if this collection is full and no new elements can be added.
public Iterator<E>

Returns:

an iterator over this queue's elements
iterator
()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Collection.iterator.

Returns an iterator over this queue's elements.
public int

Returns:

the maximum number of elements the collection can hold
maxSize
()

Implements org.apache.commons.collections4.BoundedCollection.maxSize.

Gets the maximum size of the collection (the bound).
public boolean

Returns:

true, always
offer
(final E
the element to add
element
)

Implements java.util.Queue.offer.

Adds the given element to this queue.
public E
peek()

Implements java.util.Queue.peek.

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
public E
poll()

Implements java.util.Queue.poll.

Retrieves and removes the head of this queue, or returns null if this queue is empty.
private void
readObject(final ObjectInputStream
the input stream
in
)

Read the queue in using a custom routine.

public E
remove()

Implements java.util.Queue.remove.

Retrieves and removes the head of this queue.
public int

Returns:

this queue's size
size
()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size.

Returns the number of elements stored in the queue.
private void
writeObject(final ObjectOutputStream
the output stream
out
)

Write the queue out using a custom routine.

Inherited from java.util.AbstractCollection:
addAllcontainscontainsAllremoveremoveAllretainAlltoArraytoArraytoString

Field Detail

elementsback to summary
private transient E[] elements

Underlying storage array.

endback to summary
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].

fullback to summary
private transient boolean full

Flag to indicate if the queue is currently full.

maxElementsback to summary
private final int maxElements

Capacity of the queue.

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version.

startback to summary
private transient int start

Array index of first (oldest) queue element.

Constructor Detail

CircularFifoQueueback to summary
public CircularFifoQueue()

Constructor that creates a queue with the default size of 32.

CircularFifoQueueback to summary
public CircularFifoQueue(final int size)

Constructor that creates a queue with the specified size.

Parameters
size:int

the size of the queue (cannot be changed)

Annotations
@SuppressWarnings:unchecked
Exceptions
IllegalArgumentException:
if the size is < 1
CircularFifoQueueback to summary
public CircularFifoQueue(final Collection<? extends E> coll)

Constructor that creates a queue from the specified collection. The collection size also sets the queue size.

Parameters
coll:Collection<? extends E>

the collection to copy into the queue, may not be null

Exceptions
NullPointerException:
if the collection is null

Method Detail

addback to summary
public boolean add(final E element)

Overrides java.util.AbstractCollection.add.

Implements java.util.Queue.add, java.util.Collection.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.

Parameters
element:E

the element to add

Returns:boolean

true, always

Annotations
@Override
Exceptions
NullPointerException:
if the given element is null
clearback to summary
public void clear()

Overrides java.util.AbstractCollection.clear.

Implements java.util.Collection.clear.

Clears this queue.

Annotations
@Override
decrementback to summary
private int decrement(int index)

Decrements the internal index.

Parameters
index:int

the index to decrement

Returns:int

the updated index

elementback to summary
public E element()

Implements java.util.Queue.element.

Doc from java.util.Queue.element.

Retrieves, but does not remove, the head of this queue. This method differs from peek only in that it throws an exception if this queue is empty.

Returns:E

the head of this queue

Annotations
@Override
getback to summary
public E get(final int index)

Returns the element at the specified position in this queue.

Parameters
index:int

the position of the element in the queue

Returns:E

the element at position index

Exceptions
NoSuchElementException:
if the requested position is outside the range [0, size)
incrementback to summary
private int increment(int index)

Increments the internal index.

Parameters
index:int

the index to increment

Returns:int

the updated index

isAtFullCapacityback to summary
public boolean isAtFullCapacity()

Returns true 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:boolean

true if the capacity limit has been reached, false otherwise

Since
4.1
isEmptyback to summary
public boolean isEmpty()

Overrides java.util.AbstractCollection.isEmpty.

Implements java.util.Collection.isEmpty.

Returns true if this queue is empty; false otherwise.

Returns:boolean

true if this queue is empty

Annotations
@Override
isFullback to summary
public boolean isFull()

Implements org.apache.commons.collections4.BoundedCollection.isFull.

Doc from org.apache.commons.collections4.BoundedCollection.isFull.

Returns true if this collection is full and no new elements can be added.

A CircularFifoQueue can never be full, thus this returns always false.

Returns:boolean

always returns false

Annotations
@Override
iteratorback to summary
public Iterator<E> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Collection.iterator.

Returns an iterator over this queue's elements.

Returns:Iterator<E>

an iterator over this queue's elements

Annotations
@Override
maxSizeback to summary
public int maxSize()

Implements org.apache.commons.collections4.BoundedCollection.maxSize.

Gets the maximum size of the collection (the bound).

Returns:int

the maximum number of elements the collection can hold

Annotations
@Override
offerback to summary
public boolean offer(final E element)

Implements java.util.Queue.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.

Parameters
element:E

the element to add

Returns:boolean

true, always

Annotations
@Override
Exceptions
NullPointerException:
if the given element is null
peekback to summary
public E peek()

Implements java.util.Queue.peek.

Doc from java.util.Queue.peek.

Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Returns:E

the head of this queue, or null if this queue is empty

Annotations
@Override
pollback to summary
public E poll()

Implements java.util.Queue.poll.

Doc from java.util.Queue.poll.

Retrieves and removes the head of this queue, or returns null if this queue is empty.

Returns:E

the head of this queue, or null if this queue is empty

Annotations
@Override
readObjectback to summary
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException

Read the queue in using a custom routine.

Parameters
in:ObjectInputStream

the input stream

Annotations
@SuppressWarnings:unchecked
Exceptions
IOException:
if an I/O error occurs while writing to the output stream
ClassNotFoundException:
if the class of a serialized object can not be found
removeback to summary
public E remove()

Implements java.util.Queue.remove.

Doc from java.util.Queue.remove.

Retrieves and removes the head of this queue. This method differs from poll() only in that it throws an exception if this queue is empty.

Returns:E

the head of this queue

Annotations
@Override
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Collection.size.

Returns the number of elements stored in the queue.

Returns:int

this queue's size

Annotations
@Override
writeObjectback to summary
private void writeObject(final ObjectOutputStream out) throws IOException

Write the queue out using a custom routine.

Parameters
out:ObjectOutputStream

the output stream

Exceptions
IOException:
if an I/O error occurs while writing to the output stream