Class LastNEventsQueue

java.lang.Object
io.deephaven.util.LastNEventsQueue

public class LastNEventsQueue extends Object
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
    Constructor
    Description
    LastNEventsQueue(int capacity)
    Creates a new object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addLast(long newValue)
    Add a new, most recent element to the queue.
    int
    The capacity of the queue.
    void
    Iterate over all values in the queue, from oldest to newest.
    boolean
    Returns true if the queue is empty.
    boolean
    Returns true if the queue is full.
    long
    Get the newest value in the queue.
    long
    Get the oldest value in the queue.
    void
    replaceLast(long newValue)
    Replace the most recent element in the queue.
    long
    Get the second oldest value in the queue.
    int
    The current number of elements in the queue.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public void forEach(LongConsumer lc)
      Iterate over all values in the queue, from oldest to newest.
      Parameters:
      lc - a long consumer to consumer values