Package org.agrona.collections
Class LongArrayQueue
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<java.lang.Long>
-
- org.agrona.collections.LongArrayQueue
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Long>
,java.util.Collection<java.lang.Long>
,java.util.Queue<java.lang.Long>
public class LongArrayQueue extends java.util.AbstractQueue<java.lang.Long>
Queue of longs which stores the elements without boxing. Null is represented by a specialnullValue()
.The
LongArrayQueue.LongIterator
is cached by default to avoid allocation unless directed to do so in the constructor.Note: This class is not threadsafe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
LongArrayQueue.LongIterator
SpecialisedIterator
from which the value can be retrieved without boxing viaLongArrayQueue.LongIterator.nextValue()
.
-
Field Summary
Fields Modifier and Type Field Description static long
DEFAULT_NULL_VALUE
Default representation of null for an element.private long[]
elements
private int
head
private LongArrayQueue.LongIterator
iterator
static int
MIN_CAPACITY
Minimum capacity for the queue which must also be a power of 2.private long
nullValue
private boolean
shouldAvoidAllocation
private int
tail
-
Constructor Summary
Constructors Constructor Description LongArrayQueue()
Construct a new queue defaulting toMIN_CAPACITY
capacity,DEFAULT_NULL_VALUE
and cached iterators.LongArrayQueue(int initialCapacity, long nullValue)
Construct a new queue default to cached iterators.LongArrayQueue(int initialCapacity, long nullValue, boolean shouldAvoidAllocation)
Construct a new queue providing all the config options.LongArrayQueue(long nullValue)
Construct a new queue defaulting toMIN_CAPACITY
capacity and cached iterators.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(java.lang.Long element)
boolean
addLong(long element)
Offer an element to the tail of the queue without boxing.int
capacity()
The current capacity for the collection.void
clear()
java.lang.Long
element()
long
elementLong()
Peek at the element on the head of the queue without boxing.void
forEach(java.util.function.Consumer<? super java.lang.Long> action)
void
forEachLong(java.util.function.LongConsumer action)
Iterate over the collection without boxing.private void
increaseCapacity()
boolean
isEmpty()
LongArrayQueue.LongIterator
iterator()
long
nullValue()
The value representing a null element.boolean
offer(java.lang.Long element)
boolean
offerLong(long element)
Offer an element to the tail of the queue without boxing.java.lang.Long
peek()
long
peekLong()
Peek at the element on the head of the queue without boxing.java.lang.Long
poll()
long
pollLong()
Poll the element from the head of the queue without boxing.java.lang.Long
remove()
long
removeLong()
Remove the element at the head of the queue without boxing.int
size()
java.lang.String
toString()
-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, remove, removeAll, retainAll, toArray, toArray
-
-
-
-
Field Detail
-
DEFAULT_NULL_VALUE
public static final long DEFAULT_NULL_VALUE
Default representation of null for an element.- See Also:
- Constant Field Values
-
MIN_CAPACITY
public static final int MIN_CAPACITY
Minimum capacity for the queue which must also be a power of 2.- See Also:
- Constant Field Values
-
shouldAvoidAllocation
private final boolean shouldAvoidAllocation
-
head
private int head
-
tail
private int tail
-
nullValue
private final long nullValue
-
elements
private long[] elements
-
iterator
private LongArrayQueue.LongIterator iterator
-
-
Constructor Detail
-
LongArrayQueue
public LongArrayQueue()
Construct a new queue defaulting toMIN_CAPACITY
capacity,DEFAULT_NULL_VALUE
and cached iterators.
-
LongArrayQueue
public LongArrayQueue(long nullValue)
Construct a new queue defaulting toMIN_CAPACITY
capacity and cached iterators.- Parameters:
nullValue
- cannot be stored in the queue and used as a sentinel.
-
LongArrayQueue
public LongArrayQueue(int initialCapacity, long nullValue)
Construct a new queue default to cached iterators.- Parameters:
initialCapacity
- for the queue which will be rounded up to the nearest power of 2.nullValue
- which cannot be stored in the queue and used as a sentinel.
-
LongArrayQueue
public LongArrayQueue(int initialCapacity, long nullValue, boolean shouldAvoidAllocation)
Construct a new queue providing all the config options.- Parameters:
initialCapacity
- for the queue which will be rounded up to the nearest power of 2.nullValue
- which cannot be stored in the queue and used as a sentinel.shouldAvoidAllocation
- true to cache the iterator otherwise false to allocate a new iterator each time.
-
-
Method Detail
-
nullValue
public long nullValue()
The value representing a null element.- Returns:
- value representing a null element.
-
capacity
public int capacity()
The current capacity for the collection.- Returns:
- the current capacity for the collection.
-
size
public int size()
- Specified by:
size
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
size
in classjava.util.AbstractCollection<java.lang.Long>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Collection<java.lang.Long>
- Overrides:
isEmpty
in classjava.util.AbstractCollection<java.lang.Long>
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Collection<java.lang.Long>
- Overrides:
clear
in classjava.util.AbstractQueue<java.lang.Long>
-
offer
public boolean offer(java.lang.Long element)
-
offerLong
public boolean offerLong(long element)
Offer an element to the tail of the queue without boxing.- Parameters:
element
- to be offered to the queue.- Returns:
- will always be true as long as the underlying array can be expanded.
-
add
public boolean add(java.lang.Long element)
- Specified by:
add
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
add
in interfacejava.util.Queue<java.lang.Long>
- Overrides:
add
in classjava.util.AbstractQueue<java.lang.Long>
-
addLong
public boolean addLong(long element)
Offer an element to the tail of the queue without boxing.- Parameters:
element
- to be offered to the queue.- Returns:
- will always be true as long as the underlying array can be expanded.
-
peek
public java.lang.Long peek()
-
peekLong
public long peekLong()
Peek at the element on the head of the queue without boxing.- Returns:
- the element at the head of the queue without removing it.
-
poll
public java.lang.Long poll()
-
pollLong
public long pollLong()
Poll the element from the head of the queue without boxing.- Returns:
- the element at the head of the queue removing it. If empty then
nullValue
.
-
remove
public java.lang.Long remove()
- Specified by:
remove
in interfacejava.util.Queue<java.lang.Long>
- Overrides:
remove
in classjava.util.AbstractQueue<java.lang.Long>
-
element
public java.lang.Long element()
- Specified by:
element
in interfacejava.util.Queue<java.lang.Long>
- Overrides:
element
in classjava.util.AbstractQueue<java.lang.Long>
-
elementLong
public long elementLong()
Peek at the element on the head of the queue without boxing.- Returns:
- the element at the head of the queue without removing it.
- Throws:
java.util.NoSuchElementException
- if the queue is empty.
-
removeLong
public long removeLong()
Remove the element at the head of the queue without boxing.- Returns:
- the element at the head of the queue.
- Throws:
java.util.NoSuchElementException
- if the queue is empty.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.util.AbstractCollection<java.lang.Long>
-
forEach
public void forEach(java.util.function.Consumer<? super java.lang.Long> action)
-
forEachLong
public void forEachLong(java.util.function.LongConsumer action)
Iterate over the collection without boxing.- Parameters:
action
- to be taken for each element.
-
iterator
public LongArrayQueue.LongIterator iterator()
- Specified by:
iterator
in interfacejava.util.Collection<java.lang.Long>
- Specified by:
iterator
in interfacejava.lang.Iterable<java.lang.Long>
- Specified by:
iterator
in classjava.util.AbstractCollection<java.lang.Long>
-
increaseCapacity
private void increaseCapacity()
-
-