Package de.freaklamarsch.systarest
Class CircularBuffer<E>
java.lang.Object
de.freaklamarsch.systarest.CircularBuffer<E>
- Type Parameters:
E- the type of elements stored in the buffer
public class CircularBuffer<E>
extends java.lang.Object
A circular buffer implementation that uses a fixed-size buffer to store elements in a FIFO (First In, First Out) manner.
By default, this implementation denies adding new elements if the buffer is full. This behavior can be changed by
enabling the
overwrite setting.
This class is not thread-safe. External synchronization is required for concurrent access. Synchronization can be avoided, if you
have only one reader and one writer accessing the CircularBuffer
-
Field Summary
Fields Modifier and Type Field Description private intcapacityThe maximum number of elements the buffer can hold.private E[]dataThe underlying array used to store elements in the buffer.private static intDEFAULT_CAPACITYThe default capacity of the buffer if no capacity is specified.private booleanoverwriteDetermines whether the buffer overwrites the oldest element when full.private intreadIndexThe index of the oldest element in the buffer.private intwriteIndexThe index where the next element will be written. -
Constructor Summary
Constructors Constructor Description CircularBuffer(int capacity)Creates aCircularBufferwith a capacity ofcapacityelements. -
Method Summary
Modifier and Type Method Description booleanadd(E element)Adds an element to the buffer.intcapacity()Returns the maximum number of elements the buffer can hold.voidclear()Clears the buffer by resetting the read and write indices.Eend()Returns the newest element in the buffer without removing it.booleangetOverwrite()Returns whether the buffer is configured to overwrite the oldest element when full.booleanisEmpty()Returns whether the buffer is empty.booleanisFull()Returns whether the buffer is full.private booleanisNotEmpty()Returns whether the buffer is not empty.private booleanisNotFull()Returns whether the buffer is not full.Epeek()Returns the oldest element in the buffer without removing it.Eremove()Removes and returns the oldest element from theCircularBuffer.voidsetOverwrite(boolean overwrite)intsize()Returns the number of elements currently stored in the buffer.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
DEFAULT_CAPACITY
private static final int DEFAULT_CAPACITYThe default capacity of the buffer if no capacity is specified.- See Also:
- Constant Field Values
-
capacity
private final int capacityThe maximum number of elements the buffer can hold. -
data
The underlying array used to store elements in the buffer. -
writeIndex
private volatile int writeIndexThe index where the next element will be written. -
readIndex
private volatile int readIndexThe index of the oldest element in the buffer. -
overwrite
private boolean overwriteDetermines whether the buffer overwrites the oldest element when full.
-
-
Constructor Details
-
CircularBuffer
public CircularBuffer(int capacity)Creates aCircularBufferwith a capacity ofcapacityelements.- Parameters:
capacity- number of elements that can be stored in theCircularBuffer
-
-
Method Details
-
clear
public void clear()Clears the buffer by resetting the read and write indices. Note, this does not destroy the stored elements in the buffer. -
add
Adds an element to the buffer. If the buffer is full andoverwriteistrue, the oldest element is overwritten. Ifoverwriteisfalse, the element is not added.- Parameters:
element- the element to add to the buffer- Returns:
trueif the element was added successfully;falseif the buffer is full and overwriting is disabled
-
remove
Removes and returns the oldest element from theCircularBuffer.- Returns:
- the oldest element, or
nullif the buffer is empty
-
peek
Returns the oldest element in the buffer without removing it.- Returns:
- the oldest element, or
nullif the buffer is empty
-
end
Returns the newest element in the buffer without removing it.- Returns:
- the newest element, or
nullif the buffer is empty
-
capacity
public int capacity()Returns the maximum number of elements the buffer can hold.- Returns:
- the capacity of the buffer
-
size
public int size()Returns the number of elements currently stored in the buffer.- Returns:
- the number of elements in the buffer
-
setOverwrite
public void setOverwrite(boolean overwrite)- Parameters:
overwrite- Set the overwrite behavior if an element is added to a fullCircularBuffer. If set totrue, the oldest element is overwritten. If set tofalse, add will ignore the add request and returnfalse.
-
getOverwrite
public boolean getOverwrite()Returns whether the buffer is configured to overwrite the oldest element when full.- Returns:
trueif overwriting is enabled;falseotherwise
-
isEmpty
public boolean isEmpty()Returns whether the buffer is empty.- Returns:
trueif the buffer is empty;falseotherwise
-
isFull
public boolean isFull()Returns whether the buffer is full.- Returns:
trueif the buffer is full;falseotherwise
-
isNotEmpty
private boolean isNotEmpty()Returns whether the buffer is not empty.- Returns:
trueif the buffer is not empty;falseotherwise
-
isNotFull
private boolean isNotFull()Returns whether the buffer is not full.- Returns:
trueif the buffer is not full;falseotherwise
-