Package de.gsi.dataset.utils
Class CircularBuffer<E>
- java.lang.Object
-
- de.gsi.dataset.utils.CircularBuffer<E>
-
- Type Parameters:
E- the generic Object to be stored
public class CircularBuffer<E> extends java.lang.Objectsimple circular ring buffer implementation for generic object type (with read == write position)- Author:
- rstein
-
-
Constructor Summary
Constructors Constructor Description CircularBuffer(int capacity)CircularBuffer(E[] initalElements, int capacity)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()intcapacity()E[]elements(E[] returnVectorType)Eget()Eget(int readPos)E[]get(E[] into, int length)E[]get(E[] into, int readPos, int length)protected intgetIndex(int readPos)booleanisBufferFlipped()booleanput(E element)intput(E[] newElements, int length)add multiple new elementsintput(E[] newElements, int startIndex, int length)add multiple new elementsintremainingCapacity()Ereplace(E element)Ereplace(E element, int atIndex)voidreset()resets bufferintwritePosition()
-
-
-
Constructor Detail
-
CircularBuffer
public CircularBuffer(E[] initalElements, int capacity)
- Parameters:
initalElements- adds element the buffer should be initialised withcapacity- maximum capacity of the buffer
-
CircularBuffer
public CircularBuffer(int capacity)
- Parameters:
capacity- maximum capacity of the buffer
-
-
Method Detail
-
available
public int available()
- Returns:
- number of available buffer elements
-
capacity
public int capacity()
- Returns:
- the maximum possible/filled number of available buffer elements
-
elements
public E[] elements(E[] returnVectorType)
- Parameters:
returnVectorType- need to supply export type vector- Returns:
- internal field array
-
get
public E get()
- Returns:
- value at head
-
get
public E[] get(E[] into, int length)
- Parameters:
into- storage containerlength- number of elements to be read- Returns:
- either into or newly allocated array containing the result
-
get
public E[] get(E[] into, int readPos, int length)
- Parameters:
into- storage containerreadPos- circular index (wraps around)length- number of elements to be read- Returns:
- either into or newly allocated array containing the result
-
get
public E get(int readPos)
- Parameters:
readPos- circular index (wraps around)- Returns:
- the value
-
getIndex
protected int getIndex(int readPos)
-
isBufferFlipped
public boolean isBufferFlipped()
- Returns:
- whether write position exceeded at least once the capacity
-
put
public boolean put(E element)
- Parameters:
element- new element- Returns:
- true
-
put
public int put(E[] newElements, int length)
add multiple new elements- Parameters:
newElements- array of new elementslength- number of elements that are to be written from array- Returns:
- true: write index is smaller than read index
-
put
public int put(E[] newElements, int startIndex, int length)
add multiple new elements- Parameters:
newElements- array of new elementsstartIndex- 'null'length- number of elements that are to be written from array- Returns:
- true: write index is smaller than read index
-
remainingCapacity
public int remainingCapacity()
- Returns:
- number of available buffer elements that can be written before buffer wraps-around
-
replace
public E replace(E element)
- Parameters:
element- to replace an existing element at the head buffer position- Returns:
- the previous element stored at that location
-
replace
public E replace(E element, int atIndex)
- Parameters:
element- to replace an existing element at given buffer positionatIndex- index at which to replace the value- Returns:
- the previous element stored at that location
-
reset
public void reset()
resets buffer
-
writePosition
public int writePosition()
- Returns:
- internal write position
-
-