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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds an element to the buffer.boolean
Adds elements to the buffer, preserving their ordering in the array.boolean
addAll(Collection<E> c)
Adds elements to the buffer, preserving their ordering in the collection.boolean
Returnstrue
if the head of the buffer (the next element that will be removed) is equal too
, compared usingObjects.equals(java.lang.Object, java.lang.Object)
.boolean
Returnstrue
if 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)
.boolean
Compares the head of the buffer (the next element that will be removed) too
usingObjects.equals(java.lang.Object, java.lang.Object)
and removes it from the buffer if there is a match.boolean
Compares the head of the buffer (the next elements that will be removed) toa
usingObjects.equals(java.lang.Object, java.lang.Object)
and removes them from the buffer if there is a match.void
clear()
Empties the buffer.boolean
Returnstrue
if the buffer containse
, tested usingObjects.equals(java.lang.Object, java.lang.Object)
.int
Returns the maximum capacity of this buffer.boolean
isEmpty()
Returnstrue
if the buffer is empty.boolean
Adds an element to the buffer if there is space for it.boolean
Adds elements to the buffer if there is space for them.boolean
offer(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 then
th element that will be removed from the buffer, counting from 0 (peek(0)
returns the first element that will be removed), ornull
if the buffer does not contain that many elements.poll()
Removes and returns the element at the head of the buffer, ornull
if the buffer is empty.E[]
Removes and returnsn
consecutive elements from the head of the buffer in the order they were added, ornull
if the buffer does not containn
elements.remove()
Removes and returns the element at the head of the buffer, ornull
if the buffer is empty.E[]
Removes and returns up ton
consecutive elements from the head of the buffer in the order they were added.boolean
Empties the buffer.int
size()
Returns the number of elements in the buffer.int
skip(int n)
Removesn
elements 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
Returnstrue
if 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:
true
if the head of the buffer equalso
;false
if the buffer is empty
-
at
Returnstrue
if 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:
true
if the elements at head of the buffer are equal to the elements ofa
;false
if the buffer doesn't contain enough elements to match.
-
atSkip
Compares the head of the buffer (the next element that will be removed) too
usingObjects.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:
true
if the head of the buffer equalso
and is removed;false
if the buffer is empty
-
atSkip
Compares the head of the buffer (the next elements that will be removed) toa
usingObjects.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:
true
if the elements at head of the buffer are equal to the elements ofa
;false
if the buffer doesn't contain enough elements to match.
-
clear
Empties the buffer. -
contains
Returnstrue
if the buffer containse
, tested usingObjects.equals(java.lang.Object, java.lang.Object)
.- Returns:
true
if the buffer containse
;false
if not.
-
getCapacity
Returns the maximum capacity of this buffer.- Returns:
- the maximum capacity of this buffer
-
isEmpty
Returnstrue
if the buffer is empty.- Returns:
true
if the buffer is empty
-
offer
Adds an element to the buffer if there is space for it.- Parameters:
e
- the element to add.- Returns:
true
if 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:
true
if 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:
true
if 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
null
if the buffer is empty
-
peek
Returns then
th element that will be removed from the buffer, counting from 0 (peek(0)
returns the first element that will be removed), ornull
if the buffer does not contain that many elements.- Parameters:
n
-- Returns:
- the
n
th element that will be removed from the buffer ornull
if the buffer does not contain that many elements
-
poll
Removes and returns the element at the head of the buffer, ornull
if the buffer is empty.- Returns:
- the element that was at the head of the buffer which was removed,
or
null
if the buffer is empty
-
poll
Removes and returnsn
consecutive elements from the head of the buffer in the order they were added, ornull
if the buffer does not containn
elements.- Parameters:
n
- the number of elements to removecls
- theClass
of 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
null
if the buffer does not containn
elements
-
remove
Removes and returns the element at the head of the buffer, ornull
if the buffer is empty.- Returns:
- the element that was at the head of the buffer which was removed,
or
null
if the buffer is empty
-
remove
Removes and returns up ton
consecutive elements from the head of the buffer in the order they were added.- Parameters:
n
- the number of elements to removecls
- theClass
of 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
Removesn
elements from the head of the buffer (and discards them). Ifn
>size()
, onlysize()
elements are removed. Inspired by and named after similar methods inInputStream
s andReader
s.- Parameters:
n
- the number of elements to remove- Returns:
- the number of elements actually removed
-
toList
-