Package io.deephaven.util
Class LastNEventsQueue
java.lang.Object
io.deephaven.util.LastNEventsQueue
This is a very simple queue-like data structure with a max size and where enqueuing a new value when the queue is
full forces discarding of the oldest value. It allows querying for the oldest and newest values in the queue. It was
built for the use case of detecting "N events happened in M milliseconds or less".
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddLast(long newValue) Add a new, most recent element to the queue.intcapacity()The capacity of the queue.voidforEach(LongConsumer lc) Iterate over all values in the queue, from oldest to newest.booleanisEmpty()Returns true if the queue is empty.booleanisFull()Returns true if the queue is full.longGet the newest value in the queue.longGet the oldest value in the queue.voidreplaceLast(long newValue) Replace the most recent element in the queue.longGet the second oldest value in the queue.intsize()The current number of elements in the queue.
-
Constructor Details
-
LastNEventsQueue
public LastNEventsQueue(int capacity) Creates a new object.- Parameters:
capacity- the capacity for the queue; must be at least 2.
-
-
Method Details
-
size
public int size()The current number of elements in the queue.- Returns:
- the current number of elements in the queue
-
capacity
public int capacity()The capacity of the queue.- Returns:
- the capacity of the queue
-
isEmpty
public boolean isEmpty()Returns true if the queue is empty.- Returns:
- true if the queue is empty
-
isFull
public boolean isFull()Returns true if the queue is full. When full, inserting a new value will discard the oldest value.- Returns:
- true if the queue is full
-
oldestValue
public long oldestValue()Get the oldest value in the queue.- Returns:
- the oldest value in the queue.
- Throws:
IllegalStateException- if the queue is empty.
-
secondOldestValue
public long secondOldestValue()Get the second oldest value in the queue.- Returns:
- the second oldest value in the queue.
- Throws:
IllegalStateException- if the queue has less that 2 elements.
-
newestValue
public long newestValue()Get the newest value in the queue.- Returns:
- the newest value in the queue.
- Throws:
IllegalStateException- if the queue is empty.
-
addLast
public void addLast(long newValue) Add a new, most recent element to the queue. If the queue is already at capacity, the oldest existing element is discarded.- Parameters:
newValue- the value to add as the newest value.
-
replaceLast
public void replaceLast(long newValue) Replace the most recent element in the queue.- Parameters:
newValue- the value to be stored as the most recent
-
forEach
Iterate over all values in the queue, from oldest to newest.- Parameters:
lc- a long consumer to consumer values
-