Class BoundedFifoBuffer

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable, java.util.Collection, BoundedCollection, Buffer
    Direct Known Subclasses:
    CircularFifoBuffer

    public class BoundedFifoBuffer
    extends java.util.AbstractCollection
    implements Buffer, BoundedCollection, java.io.Serializable
    The BoundedFifoBuffer is a very efficient implementation of Buffer that is of a fixed size.

    The removal order of a BoundedFifoBuffer is based on the insertion order; elements are removed in the same order in which they were added. The iteration order is the same as the removal order.

    The add(Object), remove() and get() operations all perform in constant time. All other operations perform in linear time or worse.

    Note that this implementation is not synchronized. The following can be used to provide synchronized access to your BoundedFifoBuffer:

       Buffer fifo = BufferUtils.synchronizedBuffer(new BoundedFifoBuffer());
     

    This buffer prevents null objects from being added.

    This class is Serializable from Commons Collections 3.1.

    Since:
    Commons Collections 3.0 (previously in main package v2.1)
    Version:
    $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Object[] elements
      Underlying storage array
      private int end
      Index mod maxElements of the array position following the last buffer element.
      private boolean full
      Flag to indicate if the buffer is currently full.
      private int maxElements
      Capacity of the buffer
      private static long serialVersionUID
      Serialization version
      private int start
      Array index of first (oldest) buffer element
    • Constructor Summary

      Constructors 
      Constructor Description
      BoundedFifoBuffer()
      Constructs a new BoundedFifoBuffer big enough to hold 32 elements.
      BoundedFifoBuffer​(int size)
      Constructs a new BoundedFifoBuffer big enough to hold the specified number of elements.
      BoundedFifoBuffer​(java.util.Collection coll)
      Constructs a new BoundedFifoBuffer big enough to hold all of the elements in the specified collection.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(java.lang.Object element)
      Adds the given element to this buffer.
      void clear()
      Clears this buffer.
      private int decrement​(int index)
      Decrements the internal index.
      java.lang.Object get()
      Returns the least recently inserted element in this buffer.
      private int increment​(int index)
      Increments the internal index.
      boolean isEmpty()
      Returns true if this buffer is empty; false otherwise.
      boolean isFull()
      Returns true if this collection is full and no new elements can be added.
      java.util.Iterator iterator()
      Returns an iterator over this buffer's elements.
      int maxSize()
      Gets the maximum size of the collection (the bound).
      private void readObject​(java.io.ObjectInputStream in)
      Read the buffer in using a custom routine.
      java.lang.Object remove()
      Removes the least recently inserted element from this buffer.
      int size()
      Returns the number of elements stored in the buffer.
      private void writeObject​(java.io.ObjectOutputStream out)
      Write the buffer out using a custom routine.
      • Methods inherited from class java.util.AbstractCollection

        addAll, contains, containsAll, 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

        addAll, contains, containsAll, equals, hashCode, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Field Detail

      • serialVersionUID

        private static final long serialVersionUID
        Serialization version
        See Also:
        Constant Field Values
      • elements

        private transient java.lang.Object[] elements
        Underlying storage array
      • start

        private transient int start
        Array index of first (oldest) buffer element
      • end

        private transient int end
        Index mod maxElements of the array position following the last buffer element. Buffer elements start at elements[start] and "wrap around" elements[maxElements-1], ending at elements[decrement(end)]. For example, elements = {c,a,b}, start=1, end=1 corresponds to the buffer [a,b,c].
      • full

        private transient boolean full
        Flag to indicate if the buffer is currently full.
      • maxElements

        private final int maxElements
        Capacity of the buffer
    • Constructor Detail

      • BoundedFifoBuffer

        public BoundedFifoBuffer()
        Constructs a new BoundedFifoBuffer big enough to hold 32 elements.
      • BoundedFifoBuffer

        public BoundedFifoBuffer​(int size)
        Constructs a new BoundedFifoBuffer big enough to hold the specified number of elements.
        Parameters:
        size - the maximum number of elements for this fifo
        Throws:
        java.lang.IllegalArgumentException - if the size is less than 1
      • BoundedFifoBuffer

        public BoundedFifoBuffer​(java.util.Collection coll)
        Constructs a new BoundedFifoBuffer big enough to hold all of the elements in the specified collection. That collection's elements will also be added to the buffer.
        Parameters:
        coll - the collection whose elements to add, may not be null
        Throws:
        java.lang.NullPointerException - if the collection is null
    • Method Detail

      • writeObject

        private void writeObject​(java.io.ObjectOutputStream out)
                          throws java.io.IOException
        Write the buffer out using a custom routine.
        Parameters:
        out - the output stream
        Throws:
        java.io.IOException
      • readObject

        private void readObject​(java.io.ObjectInputStream in)
                         throws java.io.IOException,
                                java.lang.ClassNotFoundException
        Read the buffer in using a custom routine.
        Parameters:
        in - the input stream
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • size

        public int size()
        Returns the number of elements stored in the buffer.
        Specified by:
        size in interface java.util.Collection
        Specified by:
        size in class java.util.AbstractCollection
        Returns:
        this buffer's size
      • isEmpty

        public boolean isEmpty()
        Returns true if this buffer is empty; false otherwise.
        Specified by:
        isEmpty in interface java.util.Collection
        Overrides:
        isEmpty in class java.util.AbstractCollection
        Returns:
        true if this buffer is empty
      • isFull

        public boolean isFull()
        Returns true if this collection is full and no new elements can be added.
        Specified by:
        isFull in interface BoundedCollection
        Returns:
        true if the collection is full
      • maxSize

        public int maxSize()
        Gets the maximum size of the collection (the bound).
        Specified by:
        maxSize in interface BoundedCollection
        Returns:
        the maximum number of elements the collection can hold
      • clear

        public void clear()
        Clears this buffer.
        Specified by:
        clear in interface java.util.Collection
        Overrides:
        clear in class java.util.AbstractCollection
      • add

        public boolean add​(java.lang.Object element)
        Adds the given element to this buffer.
        Specified by:
        add in interface java.util.Collection
        Overrides:
        add in class java.util.AbstractCollection
        Parameters:
        element - the element to add
        Returns:
        true, always
        Throws:
        java.lang.NullPointerException - if the given element is null
        BufferOverflowException - if this buffer is full
      • get

        public java.lang.Object get()
        Returns the least recently inserted element in this buffer.
        Specified by:
        get in interface Buffer
        Returns:
        the least recently inserted element
        Throws:
        BufferUnderflowException - if the buffer is empty
      • remove

        public java.lang.Object remove()
        Removes the least recently inserted element from this buffer.
        Specified by:
        remove in interface Buffer
        Returns:
        the least recently inserted element
        Throws:
        BufferUnderflowException - if the buffer is empty
      • increment

        private int increment​(int index)
        Increments the internal index.
        Parameters:
        index - the index to increment
        Returns:
        the updated index
      • decrement

        private int decrement​(int index)
        Decrements the internal index.
        Parameters:
        index - the index to decrement
        Returns:
        the updated index
      • iterator

        public java.util.Iterator iterator()
        Returns an iterator over this buffer's elements.
        Specified by:
        iterator in interface java.util.Collection
        Specified by:
        iterator in interface java.lang.Iterable
        Specified by:
        iterator in class java.util.AbstractCollection
        Returns:
        an iterator over this buffer's elements