Class SafeRingBuffer<E>
- Type Parameters:
E- the type of elements in the buffer
- Direct Known Subclasses:
BlockingRingBuffer
A ring buffer is a fixed-size first in - first out buffer
typically backed by an array with head and tail pointers.
Elements are added to the tail of the buffer and removed from the head.
When the end of the array is reached, the pointers wrap around to the beginning.
When the buffer is full, adding new elements overwrites the oldest elements
(but see the offer(E) and offer(E[]) methods in this implementation).
Pros: Fast, simple.
Cons: Size fixed at creation time, adding new elements overwrites the oldest ones if the buffer is full, elements can only be added or removed in FIFO order.
There are some useful look-ahead methods, peek(), peek(int), at(E),
at(E[]), atSkip(E)
and atSkip(E[]).
These are simple, hopefully fast, implementations.
They deliberately do not implement Java's Collection or Queue interfaces,
although the API is similar.
Variants:
FastRingBuffer: Simplest, fastest, not thread safe.SafeRingBuffer: Thread safe.BlockingRingBuffer: add and remove methods block if necessary; offer and poll methods are non-blocking.
remove() sets the underlying array slot to null to avoid memory leaks.
All other remove and poll methods call remove().
clear() discards the underlying array to avoid memory leaks.
removeAll() calls clear().
Elements can be null, but this means that,
if peek(), poll() or remove() return null,
this could be because an element was null or because the buffer was empty.
Consider using isEmpty() or size() to disambiguate.
poll() might seem redundant, but is overridden in BlockingRingBuffer.
- Author:
- sherstDotNet@yahoo.com
-
Field Summary
Fields inherited from class net.sherst.util.FastRingBuffer
capacity, count -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds an element to the buffer.booleanAdds elements to the buffer, preserving their ordering in the array.booleanaddAll(Collection<E> c)Adds elements to the buffer, preserving their ordering in the collection.booleanReturnstrueif the head of the buffer (the next element that will be removed) is equal too, compared usingObjects.equals(java.lang.Object, java.lang.Object).booleanReturnstrueif the elements at the head of the buffer (the next elements that will be removed) are equal to the contents ofa, compared usingObjects.equals(java.lang.Object, java.lang.Object).booleanCompares the head of the buffer (the next element that will be removed) toousingObjects.equals(java.lang.Object, java.lang.Object)and removes it from the buffer if there is a match.booleanCompares the head of the buffer (the next elements that will be removed) toausingObjects.equals(java.lang.Object, java.lang.Object)and removes them from the buffer if there is a match.voidclear()Empties the buffer.intReturns the maximum capacity of this buffer.booleanisEmpty()Returnstrueif the buffer is empty.booleanAdds an element to the buffer if there is space for it.booleanAdds elements to the buffer if there is space for them.booleanoffer(Collection<E> c)Adds elements to the buffer if there is space for them.peek()Returns the head of the buffer (the next element that will be removed) without actually removing it.peek(int n)Returns thenth element that will be removed from the buffer, counting from 0 (peek(0)returns the first element that will be removed), ornullif the buffer does not contain that many elements.poll()Removes and returns the element at the head of the buffer, ornullif the buffer is empty.E[]Removes and returnsnconsecutive elements from the head of the buffer in the order they were added, ornullif the buffer does not containnelements.remove()Removes and returns the element at the head of the buffer, ornullif the buffer is empty.E[]Removes and returns up tonconsecutive elements from the head of the buffer in the order they were added.booleanEmpties the buffer.intsize()Returns the number of elements in the buffer.intskip(int n)Removesnelements from the head of the buffer (and discards them).Methods inherited from class net.sherst.util.FastRingBuffer
contains, toList
-
Constructor Details
-
SafeRingBuffer
Creates a new buffer.- Parameters:
capacity- Maximum capacity of the buffer
-
-
Method Details
-
add
Adds an element to the buffer. If the buffer is full, silently overwrites the oldest element.- Overrides:
addin classFastRingBuffer<E>- Parameters:
e- the element to add- Returns:
true
-
addAll
Adds elements to the buffer, preserving their ordering in the array. If the buffer is full, silently overwrites the oldest elements.- Overrides:
addAllin classFastRingBuffer<E>- Parameters:
a- the elements to add- Returns:
- true
-
addAll
Adds elements to the buffer, preserving their ordering in the collection. If the buffer is full, silently overwrites the oldest elements.- Overrides:
addAllin classFastRingBuffer<E>- Parameters:
c- the elements to add- Returns:
- true
-
at
Returnstrueif the head of the buffer (the next element that will be removed) is equal too, compared usingObjects.equals(java.lang.Object, java.lang.Object).- Overrides:
atin classFastRingBuffer<E>- Parameters:
o- the value to be compared- Returns:
trueif the head of the buffer equalso;falseif the buffer is empty
-
at
Returnstrueif the elements at the head of the buffer (the next elements that will be removed) are equal to the contents ofa, compared usingObjects.equals(java.lang.Object, java.lang.Object).- Overrides:
atin classFastRingBuffer<E>- Parameters:
a- the values to be compared- Returns:
trueif the elements at head of the buffer are equal to the elements ofa;falseif the buffer doesn't contain enough elements to match.
-
atSkip
Compares the head of the buffer (the next element that will be removed) toousingObjects.equals(java.lang.Object, java.lang.Object)and removes it from the buffer if there is a match.- Overrides:
atSkipin classFastRingBuffer<E>- Parameters:
o- the value to be compared- Returns:
trueif the head of the buffer equalsoand is removed;falseif the buffer is empty
-
atSkip
Compares the head of the buffer (the next elements that will be removed) toausingObjects.equals(java.lang.Object, java.lang.Object)and removes them from the buffer if there is a match.- Overrides:
atSkipin classFastRingBuffer<E>- Parameters:
a- the values to be compared- Returns:
trueif the elements at head of the buffer are equal to the elements ofa;falseif the buffer doesn't contain enough elements to match.
-
clear
Empties the buffer.- Overrides:
clearin classFastRingBuffer<E>
-
getCapacity
Returns the maximum capacity of this buffer.- Overrides:
getCapacityin classFastRingBuffer<E>- Returns:
- the maximum capacity of this buffer
-
isEmpty
Returnstrueif the buffer is empty.- Overrides:
isEmptyin classFastRingBuffer<E>- Returns:
trueif the buffer is empty
-
offer
Adds an element to the buffer if there is space for it.- Overrides:
offerin classFastRingBuffer<E>- Parameters:
e- the element to add.- Returns:
trueif there was space in the buffer and the element was added
-
offer
Adds elements to the buffer if there is space for them.- Overrides:
offerin classFastRingBuffer<E>- Parameters:
e- the element to add.- Returns:
trueif there was space in the buffer and the elements were added
-
offer
Adds elements to the buffer if there is space for them.- Overrides:
offerin classFastRingBuffer<E>- Parameters:
e- the element to add.- Returns:
trueif there was space in the buffer and the elements were added
-
peek
Returns the head of the buffer (the next element that will be removed) without actually removing it.- Overrides:
peekin classFastRingBuffer<E>- Returns:
- the element at the head of the buffer or
nullif the buffer is empty
-
peek
Returns thenth element that will be removed from the buffer, counting from 0 (peek(0)returns the first element that will be removed), ornullif the buffer does not contain that many elements.- Overrides:
peekin classFastRingBuffer<E>- Parameters:
n-- Returns:
- the
nth element that will be removed from the buffer ornullif the buffer does not contain that many elements
-
poll
Removes and returns the element at the head of the buffer, ornullif the buffer is empty.- Overrides:
pollin classFastRingBuffer<E>- Returns:
- the element that was at the head of the buffer which was removed,
or
nullif the buffer is empty
-
poll
Removes and returnsnconsecutive elements from the head of the buffer in the order they were added, ornullif the buffer does not containnelements.- Overrides:
pollin classFastRingBuffer<E>- Parameters:
n- the number of elements to removecls- theClassof the elements in the buffer (needed to create the array)- Returns:
- the elements that were at the head of the buffer which were removed,
or
nullif the buffer does not containnelements
-
remove
Removes and returns the element at the head of the buffer, ornullif the buffer is empty.- Overrides:
removein classFastRingBuffer<E>- Returns:
- the element that was at the head of the buffer which was removed,
or
nullif the buffer is empty
-
remove
Removes and returns up tonconsecutive elements from the head of the buffer in the order they were added.- Overrides:
removein classFastRingBuffer<E>- Parameters:
n- the number of elements to removecls- theClassof the elements in the buffer (needed to create the array)- Returns:
- the elements that were at the head of the buffer which were removed; the length of the array reflects the number of elements that were available
-
removeAll
Empties the buffer.- Overrides:
removeAllin classFastRingBuffer<E>- Returns:
true
-
size
Returns the number of elements in the buffer.- Overrides:
sizein classFastRingBuffer<E>- Returns:
- the number of elements in the buffer
-
skip
Removesnelements from the head of the buffer (and discards them). Ifn>size(), onlysize()elements are removed. Does not block if the buffer is empty. Inspired by and named after similar methods inInputStreams andReaders.- Overrides:
skipin classFastRingBuffer<E>- Parameters:
n- the number of elements to remove- Returns:
- the number of elements actually removed
-