Package smsqmulator

Class CircularBuffer<T>

java.lang.Object
smsqmulator.CircularBuffer<T>
Type Parameters:
T - the objects in the buffer.

public class CircularBuffer<T> extends Object
A fixed size circular LIFO buffer which never overflows. If the buffer is filled to its capacity and a new element is added, the eldest element still present is simply overwritten (without generating any error!!!!),

Thus, elements are never removed from the buffer, unless by an add operation.

Null elements are never added to the buffer, but trying to do so will NOT generate any kind of error - they are just silently ignored.

The buffer is backed by an array of element T. A buffer must have at least a capacity of 1 element. There is no way to "empty" the buffer - getting en element will not "remove" it from the buffer.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates the buffer with a capacity of 10 elements.
    Creates the buffer with a capacity of n elements.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T newElement)
    Adds an element (to the end of) the buffer.
    void
    addNoDoubles(T newElement)
    Adds an element to the end of the buffer, if this is NOT equal to the same element already at the end of the buffer.
    get(int elementNumber)
    Gets the nth element of this buffer, counting from the TAIL.
    Gets the last element of this buffer.
    Shows some info about the buffer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • CircularBuffer

      public CircularBuffer()
      Creates the buffer with a capacity of 10 elements.
    • CircularBuffer

      public CircularBuffer(int n) throws IllegalArgumentException
      Creates the buffer with a capacity of n elements.
      Parameters:
      n - the number of elements the buffer is to contain.
      Throws:
      IllegalArgumentException - if the the number of elements the buffer is to have is smaller than 1.
  • Method Details

    • add

      public void add(T newElement)
      Adds an element (to the end of) the buffer. If buffer would be full, the first element is discarded. NULL elements are NOT added to the buffer.
      Parameters:
      newElement - the element to add. NULL elements are NOT added to the buffer.
    • addNoDoubles

      public void addNoDoubles(T newElement)
      Adds an element to the end of the buffer, if this is NOT equal to the same element already at the end of the buffer. What constitutes an equal element depends of the type of object in this circular buffer.

      If buffer would be full, the eldest element is discarded.

      Parameters:
      newElement - the element to add. NULL elements are NOT added to the buffer.
    • getlast

      public T getlast()
      Gets the last element of this buffer. This may be null if and only if the buffer is empty.
      Returns:
      the last element of this buffer. This will be null if and only if the buffer is empty.
    • get

      public T get(int elementNumber)
      Gets the nth element of this buffer, counting from the TAIL. This may be null if and only if the element at his place in buffer doesn't exist (which will be the case if the buffer is empty).
      Parameters:
      elementNumber - the number of the element to get. This is counted from the tail upwards, i.e. 0 = tail element, 1 = element before that etc.
      Returns:
      the element required. This will be null if this element doesn't existing (also if buffer is empty).
    • toString

      public String toString()
      Shows some info about the buffer.
      Overrides:
      toString in class Object
      Returns:
      string with info (buffer size, nbr of elements in it).