Class LinearBuffer<T>

java.lang.Object
xal.tools.collections.LinearBuffer<T>
All Implemented Interfaces:
Iterable<T>, IBoundedList<T>

public class LinearBuffer<T> extends Object implements IBoundedList<T>

Implementation of a linear, fixed-length data buffer. The current version follows a list implementation and, thus, the collection is naturally ordered according to their relative point of insertion.

Objects are added to the head of the buffer. Once the buffer becomes "full", that is, the number of objects contained within equals the buffer size, any additional additions will cause objects to fall off the tail of the buffer.

The primary difference between this class and the class CircularBuffer are the iterators provided. The CircularBuffer class uses a Last In First Out (LIFO) iteration policy while this class uses a First In First Out (FIFO) policy.

Since:
Sep 2007
Author:
Christopher K. Allen
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Deprecated. 
  • Constructor Summary

    Constructors
    Constructor
    Description
    LinearBuffer(int szBuffLen)
    Create a new, empty FixedSizeBuffer instance with the specified length.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T objNew)
    Adds an object to the buffer, pushing off an old object if the buffer is full.
    void
    Clear out the contents of the data buffer.
    get(int index)
    Return the object at the specified position in this buffer.
    Return an iterator object for this buffer which traverses the buffer objects in Last In First Out order.
    iterator(int index)
    Return an iterator object for this buffer which starts with the object at the given index and ends with the last object in the buffer.
    void
    setBufferLength(int szBuffer)
    (Re)set the size of the buffer length.
    int
    Returns the maximum number of objects that this collection can hold.
    Print out the contents of the buffer as a string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

  • Constructor Details

    • LinearBuffer

      public LinearBuffer(int szBuffLen)
      Create a new, empty FixedSizeBuffer instance with the specified length.
      Parameters:
      szBuffLen - Length of the data buffer
  • Method Details

    • setBufferLength

      public void setBufferLength(int szBuffer)
      (Re)set the size of the buffer length. If the new size of the buffer is less the previous size objects at the tail are lost.
      Parameters:
      szBuffer - new length of the data buffer
    • size

      public int size()
      Returns the maximum number of objects that this collection can hold.
      Specified by:
      size in interface IBoundedList<T>
      Returns:
      capacity of the buffer
      Since:
      Apr 11, 2013
    • add

      public void add(T objNew)
      Adds an object to the buffer, pushing off an old object if the buffer is full.
      Specified by:
      add in interface IBoundedList<T>
      Parameters:
      objNew - object to be inserted at buffer head
    • get

      public T get(int index) throws IndexOutOfBoundsException
      Return the object at the specified position in this buffer. The buffer uses a last in first out (LIFO) policy.
      Specified by:
      get in interface IBoundedList<T>
      Parameters:
      index - index position from head
      Returns:
      object at position index
      Throws:
      IndexOutOfBoundsException - index value exceeds buffer size
    • clear

      public void clear()
      Clear out the contents of the data buffer.
      Specified by:
      clear in interface IBoundedList<T>
    • iterator

      public Iterator<T> iterator()
      Return an iterator object for this buffer which traverses the buffer objects in Last In First Out order.
      Specified by:
      iterator in interface Iterable<T>
      Returns:
      iterator for buffer
    • iterator

      public Iterator<T> iterator(int index)
      Return an iterator object for this buffer which starts with the object at the given index and ends with the last object in the buffer.
      Returns:
      offset iterator for the buffer
    • toString

      public String toString()
      Print out the contents of the buffer as a string.
      Overrides:
      toString in class Object
      Returns:
      buffer content in text form
      See Also: