Class IntRingBuffer

java.lang.Object
io.deephaven.base.ringbuffer.IntRingBuffer
All Implemented Interfaces:
RingBuffer, Serializable

public class IntRingBuffer extends Object implements RingBuffer, Serializable
A simple circular buffer for primitive values, like java.util.concurrent.ArrayBlockingQueue but without the synchronization and collection overhead. Storage is between head (inclusive) and tail (exclusive) using incrementing long values. Head and tail will not wrap around; instead we use storage arrays sized to 2^N to allow fast determination of storage indices through a mask operation.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    class 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    IntRingBuffer(int capacity)
    Create an unbounded-growth ring buffer of int primitives.
    IntRingBuffer(int capacity, boolean growable)
    Create a ring buffer of int primitives.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(int e)
    Adds an entry to the ring buffer, will throw an exception if buffer is full.
    int
    addOverwrite(int e, int notFullResult)
    Add an entry to the ring buffer.
    void
    addUnsafe(int e)
    Add values without overflow detection.
    int
    Returns the element at the tail of the ring buffer
    int
    Return how many items can fit in the buffer at its current capacity.
    void
    Clear the buffer of all values.
    protected void
    Copy the contents of the buffer to a destination buffer.
    int
    If the ring buffer is non-empty, returns the element at the head of the ring buffer.
    void
    ensureRemaining(int count)
    Ensure that there is sufficient empty space to store count items in the buffer.
    int
    Returns the element at the head of the ring buffer
    int
    front(int offset)
    Returns the element at the specified offset in the ring buffer.
    int[]
    Make a copy of the elements in the ring buffer.
    int[]
    Get the storage array for this ring buffer.
    protected void
    grow(int increase)
    Increase the capacity of the ring buffer.
    boolean
    Whether the buffer is entirely empty.
    boolean
    Whether the buffer is completely full.
    Create an iterator for the ring buffer
    boolean
    offer(int e)
    Attempt to add an entry to the ring buffer.
    int
    peek(int onEmpty)
    If the ring buffer is non-empty, returns the element at the head of the ring buffer.
    int
    peekBack(int onEmpty)
    If the ring buffer is non-empty, returns the element at the tail of the ring buffer.
    int
    poll(int onEmpty)
    If the ring buffer is non-empty, removes the element at the head of the ring buffer.
    int
    Return how many free slots exist in this buffer at its current capacity.
    int
    Remove one element from the front of the ring buffer.
    int[]
    remove(int count)
    Remove multiple elements from the front of the ring buffer
    int
    Remove an element without empty buffer detection.
    int
    Return how many items are currently in the buffer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • IntRingBuffer

      public IntRingBuffer(int capacity)
      Create an unbounded-growth ring buffer of int primitives.
      Parameters:
      capacity - minimum capacity of the ring buffer
    • IntRingBuffer

      public IntRingBuffer(int capacity, boolean growable)
      Create a ring buffer of int primitives.
      Parameters:
      capacity - minimum capacity of ring buffer
      growable - whether to allow growth when the buffer is full.
  • Method Details

    • grow

      protected void grow(int increase)
      Increase the capacity of the ring buffer.
      Parameters:
      increase - Increase amount. The ring buffer's capacity will be increased by at least this amount.
    • copyRingBufferToArray

      protected void copyRingBufferToArray(int[] dest)
      Copy the contents of the buffer to a destination buffer. If the destination buffer capacity is smaller than size(), the copy will not fail but will terminate after the buffer is full.
      Parameters:
      dest - The destination buffer.
    • isFull

      public boolean isFull()
      Description copied from interface: RingBuffer
      Whether the buffer is completely full.
      Specified by:
      isFull in interface RingBuffer
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: RingBuffer
      Whether the buffer is entirely empty.
      Specified by:
      isEmpty in interface RingBuffer
    • size

      public int size()
      Description copied from interface: RingBuffer
      Return how many items are currently in the buffer.
      Specified by:
      size in interface RingBuffer
    • capacity

      public int capacity()
      Description copied from interface: RingBuffer
      Return how many items can fit in the buffer at its current capacity. If the buffer can grow, this number can change.
      Specified by:
      capacity in interface RingBuffer
    • remaining

      public int remaining()
      Description copied from interface: RingBuffer
      Return how many free slots exist in this buffer at its current capacity.
      Specified by:
      remaining in interface RingBuffer
    • clear

      public void clear()
      Description copied from interface: RingBuffer
      Clear the buffer of all values. If this is an object ring buffer, this will additionally set all values to null.
      Specified by:
      clear in interface RingBuffer
    • add

      public boolean add(int e)
      Adds an entry to the ring buffer, will throw an exception if buffer is full. For a graceful failure, use offer(int)
      Parameters:
      e - the int to be added to the buffer
      Returns:
      true if the int was added successfully
      Throws:
      UnsupportedOperationException - when growable is false and buffer is full
    • ensureRemaining

      public void ensureRemaining(int count)
      Ensure that there is sufficient empty space to store count items in the buffer. If the buffer is growable, this may result in an internal growth operation. This call should be used in conjunction with addUnsafe(int).
      Specified by:
      ensureRemaining in interface RingBuffer
      Parameters:
      count - the minimum number of empty entries in the buffer after this call
      Throws:
      UnsupportedOperationException - when growable is false and buffer is full
    • addUnsafe

      public void addUnsafe(int e)
      Add values without overflow detection. The caller *must* ensure that there is at least one element of free space in the ring buffer before calling this method. The caller may use ensureRemaining(int) or remaining() for this purpose.
      Parameters:
      e - the value to add to the buffer
    • addOverwrite

      public int addOverwrite(int e, int notFullResult)
      Add an entry to the ring buffer. If the buffer is full, will overwrite the oldest entry with the new one.
      Parameters:
      e - the int to be added to the buffer
      notFullResult - value to return is the buffer is not full
      Returns:
      the overwritten entry if the buffer is full, the provided value otherwise
    • offer

      public boolean offer(int e)
      Attempt to add an entry to the ring buffer. If the buffer is full, the add will fail and the buffer will not grow even if growable.
      Parameters:
      e - the int to be added to the buffer
      Returns:
      true if the value was added successfully, false otherwise
    • remove

      public int[] remove(int count)
      Remove multiple elements from the front of the ring buffer
      Parameters:
      count - The number of elements to remove.
      Throws:
      NoSuchElementException - if the buffer is empty
    • remove

      public int remove()
      Remove one element from the front of the ring buffer.
      Throws:
      NoSuchElementException - if the buffer is empty
    • removeUnsafe

      public int removeUnsafe()
      Remove an element without empty buffer detection. The caller *must* ensure that there is at least one element in the ring buffer. The size() method may be used for this purpose.
      Returns:
      the value removed from the buffer
    • poll

      public int poll(int onEmpty)
      If the ring buffer is non-empty, removes the element at the head of the ring buffer. Otherwise does nothing.
      Parameters:
      onEmpty - the value to return if the ring buffer is empty
      Returns:
      The removed element if the ring buffer was non-empty, otherwise the value of 'onEmpty'
    • element

      public int element()
      If the ring buffer is non-empty, returns the element at the head of the ring buffer.
      Returns:
      The head element if the ring buffer is non-empty, otherwise the value of 'onEmpty'
      Throws:
      NoSuchElementException - if the buffer is empty
    • peek

      public int peek(int onEmpty)
      If the ring buffer is non-empty, returns the element at the head of the ring buffer. Otherwise returns the specified element.
      Parameters:
      onEmpty - the value to return if the ring buffer is empty
      Returns:
      The head element if the ring buffer is non-empty, otherwise the value of 'onEmpty'
    • front

      public int front()
      Returns the element at the head of the ring buffer
      Returns:
      The element at the head of the ring buffer
    • front

      public int front(int offset)
      Returns the element at the specified offset in the ring buffer.
      Parameters:
      offset - The specified offset.
      Returns:
      The element at the specified offset
      Throws:
      NoSuchElementException - if the buffer is empty
    • back

      public int back()
      Returns the element at the tail of the ring buffer
      Returns:
      The element at the tail of the ring buffer
      Throws:
      NoSuchElementException - if the buffer is empty
    • peekBack

      public int peekBack(int onEmpty)
      If the ring buffer is non-empty, returns the element at the tail of the ring buffer. Otherwise returns the specified element.
      Parameters:
      onEmpty - the value to return if the ring buffer is empty
      Returns:
      The tail element if the ring buffer is non-empty, otherwise the value of 'onEmpty'
    • getAll

      public int[] getAll()
      Make a copy of the elements in the ring buffer.
      Returns:
      An array containing a copy of the elements in the ring buffer.
    • iterator

      public IntRingBuffer.Iterator iterator()
      Create an iterator for the ring buffer
    • getStorage

      @TestOnly public int[] getStorage()
      Get the storage array for this ring buffer. This is intended for testing and debugging purposes only.
      Returns:
      The storage array for this ring buffer.