Package com.wavefront.common
Class EvictingRingBuffer<T>
- java.lang.Object
-
- java.util.AbstractCollection<T>
-
- com.wavefront.common.EvictingRingBuffer<T>
-
- Type Parameters:
T- type of objects stored
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<T>,Collection<T>,Queue<T>,RandomAccess
- Direct Known Subclasses:
SynchronizedEvictingRingBuffer
@NotThreadSafe public class EvictingRingBuffer<T> extends AbstractCollection<T> implements Queue<T>, RandomAccess, Cloneable, Serializable
A basic ring buffer with an ability to evict values on overflow.- Author:
- vasily@wavefont.com
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description EvictingRingBuffer(int capacity)EvictingRingBuffer(int capacity, boolean throwOnOverflow)EvictingRingBuffer(int capacity, boolean throwOnOverflow, T defaultValue)protectedEvictingRingBuffer(int capacity, boolean throwOnOverflow, T defaultValue, boolean preFill)EvictingRingBuffer(int capacity, T defaultValue)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(T value)Add a value at the end of the ring buffer.intcapacity()Returns buffer capacity (i.e.Objectclone()Telement()Retrieves, but does not remove, the head of this buffer.booleanequals(Object obj)Tget(int index)Return the element at the specified position in the buffer.inthashCode()Iterator<T>iterator()booleanoffer(T value)Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.Tpeek()Retrieves, but does not remove, the head of this buffer, or returns null if empty.Tpoll()Retrieves and removes the head of this buffer, or returns null if empty.Tremove()Retrieves and removes the head of this buffer.intsize()Returns number of elements in the buffer.Object[]toArray()List<T>toList()Returns aList<T>containing all the elements in the buffer in proper sequence (first to last element).-
Methods inherited from class java.util.AbstractCollection
addAll, clear, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toString
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
addAll, clear, contains, containsAll, isEmpty, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray
-
-
-
-
Constructor Detail
-
EvictingRingBuffer
public EvictingRingBuffer(int capacity)
- Parameters:
capacity- desired capacity.
-
EvictingRingBuffer
public EvictingRingBuffer(int capacity, boolean throwOnOverflow)- Parameters:
capacity- desired capacitythrowOnOverflow- Disables auto-eviction on overflow. When full capacity is reached, all subsequent append() operations would throwIllegalStateExceptionif this parameter is true, or evict the oldest value if this parameter is false.
-
EvictingRingBuffer
public EvictingRingBuffer(int capacity, @Nullable T defaultValue)- Parameters:
capacity- desired capacity.defaultValue- pre-fill the buffer with this default value.
-
EvictingRingBuffer
public EvictingRingBuffer(int capacity, boolean throwOnOverflow, @Nullable T defaultValue)- Parameters:
capacity- desired capacity.throwOnOverflow- disables auto-eviction on overflow. When full capacity is reached, all subsequent append() operations would throwIllegalStateExceptionif this parameter is true, or evict the oldest value if this parameter is false.defaultValue- pre-fill the buffer with this default value.
-
-
Method Detail
-
capacity
public int capacity()
Returns buffer capacity (i.e. max number of elements this buffer can hold).- Returns:
- buffer capacity
-
iterator
@Nonnull public Iterator<T> iterator()
- Specified by:
iteratorin interfaceCollection<T>- Specified by:
iteratorin interfaceIterable<T>- Specified by:
iteratorin classAbstractCollection<T>
-
size
public int size()
Returns number of elements in the buffer.- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein classAbstractCollection<T>- Returns:
- number of elements
-
get
public T get(int index)
Return the element at the specified position in the buffer.- Parameters:
index- index of the element to return- Returns:
- the element at the specified position in the buffer
- Throws:
IndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size())
-
add
public boolean add(T value)
Add a value at the end of the ring buffer.- Specified by:
addin interfaceCollection<T>- Specified by:
addin interfaceQueue<T>- Overrides:
addin classAbstractCollection<T>- Parameters:
value- element to be appended to the end of the buffer- Returns:
- true (as specified by
Collection.add(Object)) - Throws:
IllegalStateException- if the element cannot be added at this time due to capacity restrictions
-
offer
public boolean offer(T value)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
-
toList
public List<T> toList()
Returns aList<T>containing all the elements in the buffer in proper sequence (first to last element).- Returns:
- a
List<T>containing all the elements in the buffer in proper sequence
-
toArray
@Nonnull public Object[] toArray()
- Specified by:
toArrayin interfaceCollection<T>- Overrides:
toArrayin classAbstractCollection<T>
-
remove
public T remove()
Retrieves and removes the head of this buffer.- Specified by:
removein interfaceQueue<T>- Returns:
- removed element
- Throws:
NoSuchElementException- if buffer is empty
-
poll
public T poll()
Retrieves and removes the head of this buffer, or returns null if empty.
-
element
public T element()
Retrieves, but does not remove, the head of this buffer. This method differs from peek only in that it throws an exception if empty.
-
peek
public T peek()
Retrieves, but does not remove, the head of this buffer, or returns null if empty.
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfaceCollection<T>- Overrides:
hashCodein classObject
-
equals
public boolean equals(Object obj)
- Specified by:
equalsin interfaceCollection<T>- Overrides:
equalsin classObject
-
clone
public Object clone() throws CloneNotSupportedException
- Overrides:
clonein classObject- Throws:
CloneNotSupportedException
-
-