Class FastRingBuffer<E>
- Type Parameters:
E- the type of elements in the buffer
- Direct Known Subclasses:
SafeRingBuffer
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 -
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.booleanReturnstrueif the buffer containse, tested usingObjects.equals(java.lang.Object, java.lang.Object).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).toList()
-
Field Details
-
capacity
-
count
-
-
Constructor Details
-
FastRingBuffer
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.- 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.- 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.- 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).- 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).- 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.- 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.- 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. -
contains
Returnstrueif the buffer containse, tested usingObjects.equals(java.lang.Object, java.lang.Object).- Returns:
trueif the buffer containse;falseif not.
-
getCapacity
Returns the maximum capacity of this buffer.- Returns:
- the maximum capacity of this buffer
-
isEmpty
Returnstrueif the buffer is empty.- Returns:
trueif the buffer is empty
-
offer
Adds an element to the buffer if there is space for it.- 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.- 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.- 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.- 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.- 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.- 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.- 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.- 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.- 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.- Returns:
true
-
size
Returns the number of elements in the buffer.- 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. Inspired by and named after similar methods inInputStreams andReaders.- Parameters:
n- the number of elements to remove- Returns:
- the number of elements actually removed
-
toList
-