Class BatchArrayQueue<T>
- Type Parameters:
T- the type of elements
- All Implemented Interfaces:
Iterable<T>, Collection<T>, Queue<T>
A concurrent queue that groups elements in batches.
The elements are stored in a growable circular array along with their batch number:
head tail
| |
elements: [E1, E2, E3, E4, E5]
batches : [ 1, 1, 2, 3, 3]
Elements E1 and E2 belong to batch 1,
only element E3 belongs to batch 2, etc.
The batch can be "closed" and a new one "opened" by calling
nextBatch().
The elements can be copied with exportMessagesToBatch(Queue, long)
into a different queue, and removed with clearToBatch(long).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends T> items) voidclear()Removes all the elements from this queue and resets the batch number to1.voidclearToBatch(long batch) Removes all the elements up to the given batch number.booleanbooleancontainsAll(Collection<?> items) element()voidexportMessagesToBatch(Queue<T> target, long batch) Copies the elements of this queue into the given queue, up to the given batch number.longgetBatch()booleanisEmpty()iterator()voidCloses the current batch and starts a new batch.booleanAdds the given element to this queue, under the current batch as returned bygetBatch().peek()poll()remove()booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) intsize()Object[]toArray()<E> E[]toArray(E[] a) toString()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Constructor Details
-
BatchArrayQueue
-
-
Method Details
-
offer
Adds the given element to this queue, under the current batch as returned by
getBatch(). -
add
-
peek
-
element
-
poll
-
remove
-
remove
- Specified by:
removein interfaceCollection<T>
-
addAll
- Specified by:
addAllin interfaceCollection<T>
-
removeAll
- Specified by:
removeAllin interfaceCollection<T>
-
retainAll
- Specified by:
retainAllin interfaceCollection<T>
-
containsAll
- Specified by:
containsAllin interfaceCollection<T>
-
contains
- Specified by:
containsin interfaceCollection<T>
-
iterator
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<T>
-
size
public int size()- Specified by:
sizein interfaceCollection<T>
-
toArray
- Specified by:
toArrayin interfaceCollection<T>
-
toArray
public <E> E[] toArray(E[] a) - Specified by:
toArrayin interfaceCollection<T>
-
clear
public void clear()Removes all the elements from this queue and resets the batch number to
1.- Specified by:
clearin interfaceCollection<T>
-
getBatch
public long getBatch()- Returns:
- the current batch number
-
nextBatch
public void nextBatch()Closes the current batch and starts a new batch.
The next element offered to this queue will belong to the new batch.
-
clearToBatch
public void clearToBatch(long batch) Removes all the elements up to the given batch number.
For example, given:
head tail | | elements: [E1, E2, E3, E4, E5] batches : [ 1, 1, 2, 3, 3]then calling
clearToBatch(1)would leave the queue in this state:head tail | | elements: [null, null, E3, E4, E5] batches : [ 0, 0, 2, 3, 3]- Parameters:
batch- the batch number- See Also:
-
exportMessagesToBatch
Copies the elements of this queue into the given queue, up to the given batch number.
For example, given:
this queue: head tail | | elements: [E1, E2, E3, E4, E5] batches : [ 1, 1, 2, 3, 3] target queue: []then calling
exportMessagesToBatch(2)would copy the elements belonging to batches up to2into the target queue, leaving this queue unchanged:this queue: head tail | | elements: [E1, E2, E3, E4, E5] batches : [ 1, 1, 2, 3, 3] target queue: [E1, E2, E3]- Parameters:
target- the target queuebatch- the batch number- See Also:
-
toString
-