Class CircularBuffer<T>

  • Type Parameters:
    T - buffer element type.

    public final class CircularBuffer<T>
    extends Object
    Implementaton of circular FIFO buffer. If buffer is full, the oldest element is replaced with the new one. Instance is not a bit synchronized.
    Author:
    Matej Sekoranja
    • Constructor Detail

      • CircularBuffer

        public CircularBuffer​(int capacity)
                       throws IllegalArgumentException
        Create a BoundedBuffer with the given capacity.
        Parameters:
        capacity - buffer capacity.
        Throws:
        IllegalArgumentException - if capacity less or equal to zero
    • Method Detail

      • size

        public int size()
        Get number of elements in the buffer.
        Returns:
        number of elements in the buffer.
      • capacity

        public int capacity()
        Get buffer capacity.
        Returns:
        buffer capacity.
      • insert

        public final T insert​(T x)
        Insert a new element in to the buffer. If buffer full, oldest element will be overriden.
        Parameters:
        x - element to insert.
        Returns:
        overriden element, if any.
      • extract

        public final T extract()
        Extract the oldest element from the buffer.
        Returns:
        the oldest element from the buffer.