T
- the objects in the buffer.public class CircularBuffer<T>
extends java.lang.Object
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.
Constructor and Description |
---|
CircularBuffer()
Creates the buffer with a capacity of 10 elements.
|
CircularBuffer(int n)
Creates the buffer with a capacity of n elements.
|
Modifier and Type | Method and 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.
|
T |
get(int elementNumber)
Gets the nth element of this buffer, counting from the TAIL.
|
T |
getlast()
Gets the last element of this buffer.
|
java.lang.String |
toString() |
public CircularBuffer()
public CircularBuffer(int n) throws java.lang.IllegalArgumentException
n
- the number of elements the buffer is to contain.java.lang.IllegalArgumentException
- if the the number of elements the buffer is to have is smaller than 1.public void add(T newElement)
NULL
elements are NOT added to the buffer.newElement
- the element to add. NULL elements are NOT added to the buffer.public void addNoDoubles(T newElement)
If buffer would be full, the first element is discarded.
newElement
- the element to add. NULL elements are NOT added to the buffer.public T getlast()
public T get(int elementNumber)
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.public java.lang.String toString()
toString
in class java.lang.Object