Class OverwritingRingBuffer<T>

java.lang.Object
com.abstractkamen.datastructures.impl.queues.OverwritingRingBuffer<T>
Type Parameters:
T - the type of elements stored in the buffer.
All Implemented Interfaces:
RingBuffer<T>, Iterable<T>

public class OverwritingRingBuffer<T> extends Object implements RingBuffer<T>
Represents a ring buffer that overwrites the oldest element when full.
  • Constructor Details

    • OverwritingRingBuffer

      public OverwritingRingBuffer(int initialCapacity)
      Constructs a new OverwritingRingBuffer with the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity of the ring buffer (must be greater than zero).
      Throws:
      IllegalArgumentException - if the initial capacity is lesser than one.
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Description copied from interface: RingBuffer
      True if buffer is empty.
      Specified by:
      isEmpty in interface RingBuffer<T>
      Returns:
      true if buffer is empty
    • size

      public int size()
      Description copied from interface: RingBuffer
      Returns the number of elements currently stored in the buffer.
      Specified by:
      size in interface RingBuffer<T>
      Returns:
      the number of elements in the buffer.
    • capacity

      public int capacity()
      Description copied from interface: RingBuffer
      Returns the total capacity of the buffer.
      Specified by:
      capacity in interface RingBuffer<T>
      Returns:
      the capacity of the buffer.
    • enqueue

      public boolean enqueue(T item)
      Enqueues the specified item into the buffer, overwriting the oldest item if the buffer is full.
      Specified by:
      enqueue in interface RingBuffer<T>
      Parameters:
      item - the item to be enqueued.
      Returns:
      true always.
    • dequeue

      public T dequeue()
      Dequeues and returns the oldest item from the buffer.
      Specified by:
      dequeue in interface RingBuffer<T>
      Returns:
      the oldest item in the buffer.
      Throws:
      NoSuchElementException - if the buffer is empty.
    • stream

      public Stream<T> stream()
      Returns a sequential Stream of items in the buffer.
      Specified by:
      stream in interface RingBuffer<T>
      Returns:
      a sequential stream of items in the buffer.
    • spliterator

      public Spliterator<T> spliterator()
      Returns a sequential Spliterator of items in the buffer.
      Specified by:
      spliterator in interface Iterable<T>
      Returns:
      a sequential spliterator of items in the buffer.
    • iterator

      public Iterator<T> iterator()
      Returns an iterator over the items in the buffer.
      Specified by:
      iterator in interface Iterable<T>
      Returns:
      an iterator over the items in the buffer.
    • toString

      public String toString()
      Overrides:
      toString in class Object