Class CircularBuffer<E>

  • Type Parameters:
    E - the generic Object to be stored

    public class CircularBuffer<E>
    extends java.lang.Object
    simple circular ring buffer implementation for generic object type (with read == write position)
    Author:
    rstein
    • Constructor Summary

      Constructors 
      Constructor Description
      CircularBuffer​(int capacity)  
      CircularBuffer​(E[] initalElements, int capacity)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      int capacity()  
      E[] elements​(E[] returnVectorType)  
      E get()  
      E get​(int readPos)  
      E[] get​(E[] into, int length)  
      E[] get​(E[] into, int readPos, int length)  
      protected int getIndex​(int readPos)  
      boolean isBufferFlipped()  
      boolean put​(E element)  
      int put​(E[] newElements, int length)
      add multiple new elements
      int put​(E[] newElements, int startIndex, int length)
      add multiple new elements
      int remainingCapacity()  
      E replace​(E element)  
      E replace​(E element, int atIndex)  
      void reset()
      resets buffer
      int writePosition()  
      • Methods inherited from class java.lang.Object

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

      • CircularBuffer

        public CircularBuffer​(E[] initalElements,
                              int capacity)
        Parameters:
        initalElements - adds element the buffer should be initialised with
        capacity - maximum capacity of the buffer
      • CircularBuffer

        public CircularBuffer​(int capacity)
        Parameters:
        capacity - maximum capacity of the buffer
    • Method Detail

      • available

        public int available()
        Returns:
        number of available buffer elements
      • capacity

        public int capacity()
        Returns:
        the maximum possible/filled number of available buffer elements
      • elements

        public E[] elements​(E[] returnVectorType)
        Parameters:
        returnVectorType - need to supply export type vector
        Returns:
        internal field array
      • get

        public E get()
        Returns:
        value at head
      • get

        public E[] get​(E[] into,
                       int length)
        Parameters:
        into - storage container
        length - number of elements to be read
        Returns:
        either into or newly allocated array containing the result
      • get

        public E[] get​(E[] into,
                       int readPos,
                       int length)
        Parameters:
        into - storage container
        readPos - circular index (wraps around)
        length - number of elements to be read
        Returns:
        either into or newly allocated array containing the result
      • get

        public E get​(int readPos)
        Parameters:
        readPos - circular index (wraps around)
        Returns:
        the value
      • getIndex

        protected int getIndex​(int readPos)
      • isBufferFlipped

        public boolean isBufferFlipped()
        Returns:
        whether write position exceeded at least once the capacity
      • put

        public boolean put​(E element)
        Parameters:
        element - new element
        Returns:
        true
      • put

        public int put​(E[] newElements,
                       int length)
        add multiple new elements
        Parameters:
        newElements - array of new elements
        length - number of elements that are to be written from array
        Returns:
        true: write index is smaller than read index
      • put

        public int put​(E[] newElements,
                       int startIndex,
                       int length)
        add multiple new elements
        Parameters:
        newElements - array of new elements
        startIndex - 'null'
        length - number of elements that are to be written from array
        Returns:
        true: write index is smaller than read index
      • remainingCapacity

        public int remainingCapacity()
        Returns:
        number of available buffer elements that can be written before buffer wraps-around
      • replace

        public E replace​(E element)
        Parameters:
        element - to replace an existing element at the head buffer position
        Returns:
        the previous element stored at that location
      • replace

        public E replace​(E element,
                         int atIndex)
        Parameters:
        element - to replace an existing element at given buffer position
        atIndex - index at which to replace the value
        Returns:
        the previous element stored at that location
      • reset

        public void reset()
        resets buffer
      • writePosition

        public int writePosition()
        Returns:
        internal write position