Class CircularBuffer<T>

  • All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>

    public class CircularBuffer<T>
    extends java.util.AbstractCollection<T>
    A particular circular buffer. New arrived data will be appended to the tail of the buffer. When buffer is full, the oldest data will be deleted when new data arrived.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private T[] buffer  
      private int bufferSize  
      private int count  
      private int head  
      private int tail  
    • Constructor Summary

      Constructors 
      Constructor Description
      CircularBuffer​(int bufferSize)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(T element)
      Add an element.
      void clear()
      clear the buffer;
      int getBufferSize()  
      T getElement​(int index)
      Get element
      T getHead()
      Get head element
      T getTail()
      Get tail element
      java.util.Iterator<T> iterator()  
      void setBufferSize​(int bufferSize, boolean clear)
      Set the buffer size.
      int size()  
      • Methods inherited from class java.util.AbstractCollection

        addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Field Detail

      • bufferSize

        private int bufferSize
      • buffer

        private T[] buffer
      • head

        private int head
      • tail

        private int tail
      • count

        private int count
    • Constructor Detail

      • CircularBuffer

        public CircularBuffer​(int bufferSize)
    • Method Detail

      • add

        public boolean add​(T element)
        Add an element.
        Specified by:
        add in interface java.util.Collection<T>
        Overrides:
        add in class java.util.AbstractCollection<T>
        Parameters:
        element -
      • getElement

        public T getElement​(int index)
        Get element
        Parameters:
        index - the index of the element in the buffer.
        Returns:
        the element. null if the data at the index doesn't exist.
      • getHead

        public T getHead()
        Get head element
        Returns:
        the head element. null if the buffer is empty.
      • getTail

        public T getTail()
        Get tail element
        Returns:
        the tail element. null if the buffer is empty.
      • clear

        public void clear()
        clear the buffer;
        Specified by:
        clear in interface java.util.Collection<T>
        Overrides:
        clear in class java.util.AbstractCollection<T>
      • setBufferSize

        public void setBufferSize​(int bufferSize,
                                  boolean clear)
        Set the buffer size.
        Parameters:
        bufferSize - the bufferSize to set
        clear - clear the buffer if true. Otherwise keep the exist data; Oldest data will be omitted if the new bufferSize is less than the exist data count.
      • getBufferSize

        public int getBufferSize()
        Returns:
        the bufferSize
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in class java.util.AbstractCollection<T>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in class java.util.AbstractCollection<T>