T
- The reference type stored in the buffer.public class RingBuffer<T> extends Object implements Queue<T>
A unsynchronized fixed capacity ring buffer. The buffer does not accept
null
elements. If you want a thread-safe Queue
consider ArrayBlockingQueue
instead.
Note: The "head" of the ring buffer is the insertion point, NOT the MRU
element which is located at the previous "head" index. The "tail" of the ring
buffer is the LRU position. Unfortunately, these labels are exactly the
reverse of the labels used to describe the Queue
semantics.
Modifier and Type | Field and Description |
---|---|
protected int |
capacity
The capacity of the buffer.
|
protected int |
size
The #of elements in the buffer.
|
Constructor and Description |
---|
RingBuffer(int capacity)
Ctor.
|
Modifier and Type | Method and Description |
---|---|
protected T |
_get(int i)
Return the reference at the specified index in the backing array.
|
boolean |
add(T ref) |
boolean |
addAll(Collection<? extends T> c) |
protected void |
beforeOffer(T ref)
All attempts to add an element to the buffer invoke this hook before
checking the remaining capacity in the buffer.
|
int |
capacity()
The capacity of the buffer as specified to the constructor.
|
void |
clear()
Clears the buffer, setting the references in the buffer to
null . |
void |
clear(boolean clearRefs)
Clears the buffer (sets the index of the head and the tail to zero and
sets the count to zero).
|
boolean |
contains(Object ref) |
boolean |
containsAll(Collection<?> c) |
T |
element() |
T |
get(int index)
Return the nth element in the buffer.
|
boolean |
isEmpty() |
boolean |
isFull()
True iff the buffer is full.
|
Iterator<T> |
iterator()
Return an iterator over the buffer visiting elements in LRU to MRU order
(the order in which those elements would be read by
poll() ). |
boolean |
offer(T ref) |
T |
peek()
Note: This is the reference at the "tail" of the
RingBuffer (the
LRU position). |
T |
poll() |
T |
remove() |
boolean |
remove(Object ref) |
boolean |
removeAll(Collection<?> c) |
boolean |
retainAll(Collection<?> c) |
boolean |
scanHead(int nscan,
T ref)
Scan the last nscan references for this reference.
|
boolean |
scanTail(int nscan,
T ref)
Return true iff the reference is found in the first N positions scanning
backwards from the tail of the queue.
|
int |
size() |
Object[] |
toArray()
Return the buffer elements in MRU (head) to LRU (tail) order.
|
<TX> TX[] |
toArray(TX[] a)
Return the buffer elements in MRU (head) to LRU (tail) order.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
equals, hashCode
protected final int capacity
protected int size
capacity
.
Note: Exposed to HardReferenceQueue
as an optimization for
isFull()
.
public RingBuffer(int capacity)
capacity
- The capacity of the buffer.public final int capacity()
public final int size()
size
in interface Collection<T>
public final boolean isEmpty()
isEmpty
in interface Collection<T>
public final boolean isFull()
public boolean add(T ref) throws IllegalStateException
add
in interface Collection<T>
add
in interface Queue<T>
IllegalStateException
protected void beforeOffer(T ref)
This hook provides an opportunity to realize an eviction protocol and is
used for that purpose by HardReferenceQueue
. It is also used to
realize the the stale reference protocol in
SynchronizedHardReferenceQueueWithTimeout
.
public boolean addAll(Collection<? extends T> c)
addAll
in interface Collection<T>
public void clear()
null
.clear
in interface Collection<T>
public final void clear(boolean clearRefs)
clearRefs
- When true
the references are explicitly set to
null
which can facilitate garbage collection.public Object[] toArray()
toArray
in interface Collection<T>
public <TX> TX[] toArray(TX[] a)
toArray
in interface Collection<T>
public final T get(int index)
size()
-1.index
- The index of the desired element.IllegalArgumentException
- if the index is less than ZERO (0).IllegalArgumentException
- if the index is greater than or equal to the size()
.protected final T _get(int i)
Note: Unlike get(int)
, this method does not adjust by the index
of the tail
. It is intended for use by subclasses which
require low level access into the backing array.
i
- The index into the backing array.public final T element() throws NoSuchElementException
element
in interface Queue<T>
NoSuchElementException
public final T peek()
RingBuffer
(the
LRU position).public boolean scanHead(int nscan, T ref)
nscan
- The #of positions to scan, starting with the most recently
added reference.ref
- The reference for which we are scanning.public final boolean scanTail(int nscan, T ref)
nscan
- The #of positions to be scanned. When one (1) only the tail of
the queue is scanned.ref
- The reference to scan for.public T remove() throws NoSuchElementException
remove
in interface Queue<T>
NoSuchElementException
public boolean contains(Object ref)
contains
in interface Collection<T>
public boolean containsAll(Collection<?> c)
containsAll
in interface Collection<T>
public Iterator<T> iterator()
poll()
). The
iterator supports Iterator.remove()
. The iterator is NOT
thread-safe.public boolean remove(Object ref)
remove
in interface Collection<T>
public boolean removeAll(Collection<?> c)
removeAll
in interface Collection<T>
public boolean retainAll(Collection<?> c)
retainAll
in interface Collection<T>
Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.