Class CircularArrayBuffer<E>

  • All Implemented Interfaces:
    CircularBuffer<E>, java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, java.util.RandomAccess

    public class CircularArrayBuffer<E>
    extends java.util.AbstractList<E>
    implements CircularBuffer<E>, java.lang.Cloneable, java.util.RandomAccess, java.io.Serializable
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected E[] array  
      protected int capacity  
      • Fields inherited from class java.util.AbstractList

        modCount
    • Constructor Summary

      Constructors 
      Constructor Description
      CircularArrayBuffer​(int capacity)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E e)  
      boolean addAll​(java.util.Collection<? extends E> elements)  
      int capacity()  
      void clear()  
      int end()  
      E get​(int i)  
      boolean isEmpty()  
      java.util.Iterator<E> iterator()  
      java.util.ListIterator<E> listIterator()
      Not supported
      java.util.ListIterator<E> listIterator​(int i)
      Not supported
      int offset()  
      E remove​(int i)
      Remove the element.
      CircularArrayBuffer<E> removeFirst​(int n)
      Remove the elements from the beginning.
      CircularArrayBuffer<E> removeLast​(int n)
      Remove the last elements
      E set​(int i, E e)
      Replace the element value
      int size()  
      java.util.List<E> subList​(int fromIndex, int toIndex)
      Not supported
      • Methods inherited from class java.util.AbstractList

        add, addAll, equals, hashCode, indexOf, lastIndexOf, removeRange
      • Methods inherited from class java.util.AbstractCollection

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

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

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

        forEach
      • Methods inherited from interface java.util.List

        add, addAll, contains, containsAll, equals, hashCode, indexOf, lastIndexOf, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
    • Field Detail

      • array

        protected final E[] array
      • capacity

        protected final int capacity
    • Constructor Detail

      • CircularArrayBuffer

        public CircularArrayBuffer​(int capacity)
    • Method Detail

      • capacity

        public final int capacity()
        Specified by:
        capacity in interface CircularBuffer<E>
        Returns:
        the buffer capacity
      • offset

        public final int offset()
        Specified by:
        offset in interface CircularBuffer<E>
        Returns:
        the index of the 1st element
      • end

        public final int end()
        Specified by:
        end in interface CircularBuffer<E>
        Returns:
        the index which points to the next element after the last one (exclusive), -1 if the buffer is empty
      • size

        public final int size()
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface java.util.List<E>
        Specified by:
        size in class java.util.AbstractCollection<E>
      • isEmpty

        public final boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E>
        Specified by:
        isEmpty in interface java.util.List<E>
        Overrides:
        isEmpty in class java.util.AbstractCollection<E>
      • clear

        public final void clear()
        Specified by:
        clear in interface java.util.Collection<E>
        Specified by:
        clear in interface java.util.List<E>
        Overrides:
        clear in class java.util.AbstractList<E>
      • add

        public final boolean add​(E e)
        Specified by:
        add in interface CircularBuffer<E>
        Specified by:
        add in interface java.util.Collection<E>
        Specified by:
        add in interface java.util.List<E>
        Overrides:
        add in class java.util.AbstractList<E>
        Parameters:
        e - element to add to the end of the buffer
        Returns:
        false if the buffer is full
      • addAll

        public final boolean addAll​(java.util.Collection<? extends E> elements)
        Specified by:
        addAll in interface CircularBuffer<E>
        Specified by:
        addAll in interface java.util.Collection<E>
        Specified by:
        addAll in interface java.util.List<E>
        Overrides:
        addAll in class java.util.AbstractCollection<E>
        Returns:
        true if all elements have been added, false if not enough capacity
      • get

        public E get​(int i)
        Specified by:
        get in interface CircularBuffer<E>
        Specified by:
        get in interface java.util.List<E>
        Specified by:
        get in class java.util.AbstractList<E>
        Parameters:
        i - element index
        Returns:
        element
      • set

        public E set​(int i,
                     E e)
        Description copied from interface: CircularBuffer
        Replace the element value
        Specified by:
        set in interface CircularBuffer<E>
        Specified by:
        set in interface java.util.List<E>
        Overrides:
        set in class java.util.AbstractList<E>
        Parameters:
        i - element index
        e - new element value
        Returns:
        previous element value
      • remove

        public final E remove​(int i)
        Description copied from interface: CircularBuffer
        Remove the element. The implementation is able to remove only 1st either last element.
        Specified by:
        remove in interface CircularBuffer<E>
        Specified by:
        remove in interface java.util.List<E>
        Overrides:
        remove in class java.util.AbstractList<E>
        Parameters:
        i - element index
        Returns:
        previous element value
      • subList

        public java.util.List<E> subList​(int fromIndex,
                                         int toIndex)
        Description copied from interface: CircularBuffer
        Not supported
        Specified by:
        subList in interface CircularBuffer<E>
        Specified by:
        subList in interface java.util.List<E>
        Overrides:
        subList in class java.util.AbstractList<E>
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface CircularBuffer<E>
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.List<E>
        Overrides:
        iterator in class java.util.AbstractList<E>
        Returns:
        the iterator instance
      • listIterator

        public java.util.ListIterator<E> listIterator()
        Description copied from interface: CircularBuffer
        Not supported
        Specified by:
        listIterator in interface CircularBuffer<E>
        Specified by:
        listIterator in interface java.util.List<E>
        Overrides:
        listIterator in class java.util.AbstractList<E>
      • listIterator

        public java.util.ListIterator<E> listIterator​(int i)
        Description copied from interface: CircularBuffer
        Not supported
        Specified by:
        listIterator in interface CircularBuffer<E>
        Specified by:
        listIterator in interface java.util.List<E>
        Overrides:
        listIterator in class java.util.AbstractList<E>