Class CyclicBuffer<T>

  • Type Parameters:
    T - The type of object stored in the buffer.

    public final class CyclicBuffer<T>
    extends java.lang.Object
    A bounded buffer containing elements of type T. When the number of elements to be added will exceed the size of the buffer the oldest element will be overwritten. Access to the buffer is thread safe.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Class<T> clazz  
      private int first  
      private int last  
      private int numElems  
      private T[] ring  
    • Constructor Summary

      Constructors 
      Constructor Description
      CyclicBuffer​(java.lang.Class<T> clazz, int size)
      Instantiates a new CyclicBuffer of at most maxSize events.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(T item)
      Adds an item as the last event in the buffer.
      boolean isEmpty()
      Determines if the buffer contains elements.
      private T[] makeArray​(java.lang.Class<T> cls, int size)  
      T[] removeAll()
      Removes all the elements from the buffer and returns them.
      • Methods inherited from class java.lang.Object

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

      • ring

        private final T[] ring
      • first

        private int first
      • last

        private int last
      • numElems

        private int numElems
      • clazz

        private final java.lang.Class<T> clazz
    • Constructor Detail

      • CyclicBuffer

        public CyclicBuffer​(java.lang.Class<T> clazz,
                            int size)
                     throws java.lang.IllegalArgumentException
        Instantiates a new CyclicBuffer of at most maxSize events.
        Parameters:
        clazz - The Class associate with the type of object in the buffer.
        size - The number of items in the buffer.
        Throws:
        java.lang.IllegalArgumentException - if the size is negative.
    • Method Detail

      • makeArray

        private T[] makeArray​(java.lang.Class<T> cls,
                              int size)
      • add

        public void add​(T item)
        Adds an item as the last event in the buffer.
        Parameters:
        item - The item to add to the buffer.
      • removeAll

        public T[] removeAll()
        Removes all the elements from the buffer and returns them.
        Returns:
        An array of the elements in the buffer.
      • isEmpty

        public boolean isEmpty()
        Determines if the buffer contains elements.
        Returns:
        true if the buffer is empty, false otherwise.