Class RingBuffer<K>

java.lang.Object
io.aiven.commons.collections.RingBuffer<K>
Type Parameters:
K - the type of item in the queue. Must support equality check.

public final class RingBuffer<K> extends Object
Implements a ring buffer of items. Items are inserted until maximum size is reached and then the earliest items are removed when newer items are added.
  • 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.
  • 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.
      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.
      Parameters:
      size - The maximum size of the ring buffer
      duplicateHandling - defines how to handle duplicate values in the buffer.
  • 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.
      Parameters:
      item - the item to look for.
      Returns:
      true if the item is in the buffer, false othersie.
    • 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