Package dev.brachtendorf.datastructures
Class CircularQueue<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- dev.brachtendorf.datastructures.CircularQueue<E>
-
- Type Parameters:
E- the type of elements in this collection
- All Implemented Interfaces:
Serializable,Iterable<E>,Collection<E>,Queue<E>
public class CircularQueue<E> extends AbstractCollection<E> implements Queue<E>, Serializable
Apache commons CircularFifoQueue without the entire apache dependency CircularFifoQueue is a first-in first-out queue with a fixed size that replaces its oldest element if full.The removal order of a
CircularQueueis 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:
- 1.1.0 com.github.kilianB
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description CircularQueue()Constructor that creates a queue with the default size of 32.CircularQueue(int size)Constructor that creates a queue with the specified size.CircularQueue(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 booleanadd(E element)Adds the given element to this queue.voidclear()Clears this queue.Eelement()Eget(int index)Returns the element at the specified position in this queue.booleanisAtFullCapacity()Returnstrueif the capacity limit of this queue has been reached, i.e.booleanisEmpty()Returns true if this queue is empty; false otherwise.Iterator<E>iterator()Returns an iterator over this queue's elements.booleanoffer(E element)Adds the given element to this queue.Epeek()Epoll()Eremove()intsize()Returns the number of elements stored in the queue.-
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
-
Methods inherited from interface java.util.Collection
addAll, contains, containsAll, equals, hashCode, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
-
-
-
Constructor Detail
-
CircularQueue
public CircularQueue()
Constructor that creates a queue with the default size of 32.
-
CircularQueue
public CircularQueue(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
-
CircularQueue
public CircularQueue(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:
NullPointerException- if the collection is null
-
-
Method Detail
-
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
-
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 com.github.kilianB
-
clear
public void clear()
Clears this queue.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractCollection<E>
-
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.- 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
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:
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:
offerin interfaceQueue<E>- Parameters:
element- the element to add- Returns:
- true, always
- Throws:
NullPointerException- if the given element is null
-
-