Class CircularBuffer<E>

java.lang.Object
de.freaklamarsch.systarest.CircularBuffer<E>
Type Parameters:
E - the type of elements stored in the buffer

public class CircularBuffer<E>
extends java.lang.Object
A circular buffer implementation that uses a fixed-size buffer to store elements in a FIFO (First In, First Out) manner. By default, this implementation denies adding new elements if the buffer is full. This behavior can be changed by enabling the overwrite setting.

This class is not thread-safe. External synchronization is required for concurrent access. Synchronization can be avoided, if you have only one reader and one writer accessing the CircularBuffer

  • Field Summary

    Fields
    Modifier and Type Field Description
    private int capacity
    The maximum number of elements the buffer can hold.
    private E[] data
    The underlying array used to store elements in the buffer.
    private static int DEFAULT_CAPACITY
    The default capacity of the buffer if no capacity is specified.
    private boolean overwrite
    Determines whether the buffer overwrites the oldest element when full.
    private int readIndex
    The index of the oldest element in the buffer.
    private int writeIndex
    The index where the next element will be written.
  • Constructor Summary

    Constructors
    Constructor Description
    CircularBuffer​(int capacity)
    Creates a CircularBuffer with a capacity of capacity elements.
  • Method Summary

    Modifier and Type Method Description
    boolean add​(E element)
    Adds an element to the buffer.
    int capacity()
    Returns the maximum number of elements the buffer can hold.
    void clear()
    Clears the buffer by resetting the read and write indices.
    E end()
    Returns the newest element in the buffer without removing it.
    boolean getOverwrite()
    Returns whether the buffer is configured to overwrite the oldest element when full.
    boolean isEmpty()
    Returns whether the buffer is empty.
    boolean isFull()
    Returns whether the buffer is full.
    private boolean isNotEmpty()
    Returns whether the buffer is not empty.
    private boolean isNotFull()
    Returns whether the buffer is not full.
    E peek()
    Returns the oldest element in the buffer without removing it.
    E remove()
    Removes and returns the oldest element from the CircularBuffer.
    void setOverwrite​(boolean overwrite)  
    int size()
    Returns the number of elements currently stored in the buffer.

    Methods inherited from class java.lang.Object

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

    • DEFAULT_CAPACITY

      private static final int DEFAULT_CAPACITY
      The default capacity of the buffer if no capacity is specified.
      See Also:
      Constant Field Values
    • capacity

      private final int capacity
      The maximum number of elements the buffer can hold.
    • data

      private final E[] data
      The underlying array used to store elements in the buffer.
    • writeIndex

      private volatile int writeIndex
      The index where the next element will be written.
    • readIndex

      private volatile int readIndex
      The index of the oldest element in the buffer.
    • overwrite

      private boolean overwrite
      Determines whether the buffer overwrites the oldest element when full.
  • Constructor Details

    • CircularBuffer

      public CircularBuffer​(int capacity)
      Creates a CircularBuffer with a capacity of capacity elements.
      Parameters:
      capacity - number of elements that can be stored in the CircularBuffer
  • Method Details

    • clear

      public void clear()
      Clears the buffer by resetting the read and write indices. Note, this does not destroy the stored elements in the buffer.
    • add

      public boolean add​(E element)
      Adds an element to the buffer. If the buffer is full and overwrite is true, the oldest element is overwritten. If overwrite is false, the element is not added.
      Parameters:
      element - the element to add to the buffer
      Returns:
      true if the element was added successfully; false if the buffer is full and overwriting is disabled
    • remove

      public E remove()
      Removes and returns the oldest element from the CircularBuffer.
      Returns:
      the oldest element, or null if the buffer is empty
    • peek

      public E peek()
      Returns the oldest element in the buffer without removing it.
      Returns:
      the oldest element, or null if the buffer is empty
    • end

      public E end()
      Returns the newest element in the buffer without removing it.
      Returns:
      the newest element, or null if the buffer is empty
    • capacity

      public int capacity()
      Returns the maximum number of elements the buffer can hold.
      Returns:
      the capacity of the buffer
    • size

      public int size()
      Returns the number of elements currently stored in the buffer.
      Returns:
      the number of elements in the buffer
    • setOverwrite

      public void setOverwrite​(boolean overwrite)
      Parameters:
      overwrite - Set the overwrite behavior if an element is added to a full CircularBuffer. If set to true, the oldest element is overwritten. If set to false, add will ignore the add request and return false.
    • getOverwrite

      public boolean getOverwrite()
      Returns whether the buffer is configured to overwrite the oldest element when full.
      Returns:
      true if overwriting is enabled; false otherwise
    • isEmpty

      public boolean isEmpty()
      Returns whether the buffer is empty.
      Returns:
      true if the buffer is empty; false otherwise
    • isFull

      public boolean isFull()
      Returns whether the buffer is full.
      Returns:
      true if the buffer is full; false otherwise
    • isNotEmpty

      private boolean isNotEmpty()
      Returns whether the buffer is not empty.
      Returns:
      true if the buffer is not empty; false otherwise
    • isNotFull

      private boolean isNotFull()
      Returns whether the buffer is not full.
      Returns:
      true if the buffer is not full; false otherwise