Class RingBuffer<K>

java.lang.Object
io.aiven.commons.util.collections.RingBuffer<K>
Type Parameters:
K - the type of item in the queue. Must support equality check or a comparator must be provided.

public final class RingBuffer<K> extends Object
Implements a ring buffer of items. Items are inserted until maximum size is reached the earliest items are removed when newer items are added.

The ring buffer uses a comparator for determining if an entry is in the buffer. If no comparator is provided, a default comparator using Object.equals() is used.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    How to handle the duplicates in the buffer.
  • Constructor Summary

    Constructors
    Constructor
    Description
    RingBuffer(int size)
    Create a Ring Buffer of a maximum size that rejects duplicates.
    RingBuffer(int size, RingBuffer.DuplicateHandling duplicateHandling)
    Create a Ring Buffer of specified maximum size and potentially allowing duplicates.
    RingBuffer(int size, RingBuffer.DuplicateHandling duplicateHandling, Comparator<K> comparator)
    Create a Ring Buffer of specified maximum size and potentially allowing duplicates.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(K item)
    Adds a new item if it is not already present.
    boolean
    contains(K item)
    Determines if the item is in the buffer.
    boolean
    equals(Object object)
     
    Gets the next item to be ejected.
    int
     
    Returns but does not remove the head of the buffer.
    boolean
    Returns true if the buffer is full.
    void
    remove(K item)
    Removes a single instance of the item from the buffer.
    Returns but does not remove the teal of the buffer.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • RingBuffer

      public RingBuffer(int size)
      Create a Ring Buffer of a maximum size that rejects duplicates. If the size is less than or equal to 0 then the buffer is always empty. Duplicate is detection is implemented using Object.equals().
      Parameters:
      size - The maximum size of the ring buffer
      See Also:
    • RingBuffer

      public RingBuffer(int size, RingBuffer.DuplicateHandling duplicateHandling)
      Create a Ring Buffer of specified maximum size and potentially allowing duplicates. If the size is less than or equal to 0 then the buffer is always empty. Duplicate id detection is implemented using Object.equals().
      Parameters:
      size - The maximum size of the ring buffer
      duplicateHandling - defines how to handle duplicate values in the buffer.
    • RingBuffer

      public RingBuffer(int size, RingBuffer.DuplicateHandling duplicateHandling, Comparator<K> comparator)
      Create a Ring Buffer of specified maximum size and potentially allowing duplicates. If the size is less than or equal to 0 then the buffer is always empty.
      Parameters:
      size - The maximum size of the ring buffer
      duplicateHandling - defines how to handle duplicate values in the buffer.
      comparator - the comparator to use for duplicate detection.
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • add

      public K add(K item)
      Adds a new item if it is not already present.
      • If the buffer is always empty the item is ignored and not enqueued.
      • If the buffer already contains the item it is ignored and not enqueued.
      • If the buffer is full the oldest entry in the buffer is ejected.
      Parameters:
      item - Item T which is to be added to the Queue
      Returns:
      The item that was ejected. May be null.
    • remove

      public void remove(K item)
      Removes a single instance of the item from the buffer.
      Parameters:
      item - the item to remove.
    • contains

      public boolean contains(K item)
      Determines if the item is in the buffer. This implementation iterates over the elements in the collection, checking each element in turn for equality with the specified item using the comparator.
      Parameters:
      item - the item to look for.
      Returns:
      true if the item is in the buffer, false otherwise.
      Throws:
      NullPointerException - if the item is null.
    • head

      public K head()
      Returns but does not remove the head of the buffer.
      Returns:
      the item at the head of the buffer. May be null.
    • tail

      public K tail()
      Returns but does not remove the teal of the buffer.
      Returns:
      the item at the tail of the buffer. May be null.
    • isFull

      public boolean isFull()
      Returns true if the buffer is full.
      Returns:
      true if the buffer is full.
    • getNextEjected

      public K getNextEjected()
      Gets the next item to be ejected. If the buffer is full this will return the oldest value in the buffer. If the buffer is not full this method will return null.
      Returns:
      A value T from the last place in the buffer, returns null if buffer is not full.
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object