Package com.wavefront.common
Class SynchronizedEvictingRingBuffer<T>
- java.lang.Object
-
- java.util.AbstractCollection<T>
-
- com.wavefront.common.EvictingRingBuffer<T>
-
- com.wavefront.common.SynchronizedEvictingRingBuffer<T>
-
- Type Parameters:
T- type of objects stored
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<T>,Collection<T>,Queue<T>,RandomAccess
@ThreadSafe public class SynchronizedEvictingRingBuffer<T> extends EvictingRingBuffer<T>
A thread-safe implementation of a basic ring buffer with an ability to evict values on overflow.- Author:
- vasily@wavefont.com
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SynchronizedEvictingRingBuffer(int capacity)SynchronizedEvictingRingBuffer(int capacity, boolean throwOnOverflow)SynchronizedEvictingRingBuffer(int capacity, boolean throwOnOverflow, T defaultValue)SynchronizedEvictingRingBuffer(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.booleanaddAll(Collection<? extends T> coll)voidclear()booleancontains(Object o)booleancontainsAll(Collection<?> coll)Telement()Retrieves, but does not remove, the head of this buffer.booleanequals(Object obj)voidforEach(Consumer<? super T> consumer)Tget(int index)Return the element at the specified position in the buffer.inthashCode()booleanisEmpty()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.booleanremove(Object o)booleanremoveAll(Collection<?> coll)booleanremoveIf(Predicate<? super T> filter)booleanretainAll(Collection<?> coll)intsize()Returns number of elements in the buffer.Object[]toArray()<U> U[]toArray(U[] a)List<T>toList()Returns aList<T>containing all the elements in the buffer in proper sequence (first to last element).StringtoString()-
Methods inherited from class com.wavefront.common.EvictingRingBuffer
capacity, clone, iterator
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, spliterator, stream
-
-
-
-
Constructor Detail
-
SynchronizedEvictingRingBuffer
public SynchronizedEvictingRingBuffer(int capacity)
- Parameters:
capacity- desired capacity.
-
SynchronizedEvictingRingBuffer
public SynchronizedEvictingRingBuffer(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.
-
SynchronizedEvictingRingBuffer
public SynchronizedEvictingRingBuffer(int capacity, @Nullable T defaultValue)- Parameters:
capacity- desired capacity.defaultValue- pre-fill the buffer with this default value.
-
SynchronizedEvictingRingBuffer
public SynchronizedEvictingRingBuffer(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
-
size
public int size()
Description copied from class:EvictingRingBufferReturns number of elements in the buffer.- Specified by:
sizein interfaceCollection<T>- Overrides:
sizein classEvictingRingBuffer<T>- Returns:
- number of elements
-
get
public T get(int index)
Description copied from class:EvictingRingBufferReturn the element at the specified position in the buffer.- Overrides:
getin classEvictingRingBuffer<T>- Parameters:
index- index of the element to return- Returns:
- the element at the specified position in the buffer
-
add
public boolean add(T value)
Description copied from class:EvictingRingBufferAdd a value at the end of the ring buffer.- Specified by:
addin interfaceCollection<T>- Specified by:
addin interfaceQueue<T>- Overrides:
addin classEvictingRingBuffer<T>- Parameters:
value- element to be appended to the end of the buffer- Returns:
- true (as specified by
Collection.add(Object))
-
offer
public boolean offer(T value)
Description copied from class:EvictingRingBufferInserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
-
toList
public List<T> toList()
Description copied from class:EvictingRingBufferReturns aList<T>containing all the elements in the buffer in proper sequence (first to last element).- Overrides:
toListin classEvictingRingBuffer<T>- 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 classEvictingRingBuffer<T>
-
remove
public T remove()
Description copied from class:EvictingRingBufferRetrieves and removes the head of this buffer.
-
poll
public T poll()
Description copied from class:EvictingRingBufferRetrieves and removes the head of this buffer, or returns null if empty.
-
element
public T element()
Description copied from class:EvictingRingBufferRetrieves, 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()
Description copied from class:EvictingRingBufferRetrieves, but does not remove, the head of this buffer, or returns null if empty.
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceCollection<T>- Overrides:
isEmptyin classAbstractCollection<T>
-
contains
public boolean contains(Object o)
- Specified by:
containsin interfaceCollection<T>- Overrides:
containsin classAbstractCollection<T>
-
containsAll
public boolean containsAll(@Nonnull Collection<?> coll)
- Specified by:
containsAllin interfaceCollection<T>- Overrides:
containsAllin classAbstractCollection<T>
-
addAll
public boolean addAll(@Nonnull Collection<? extends T> coll)
- Specified by:
addAllin interfaceCollection<T>- Overrides:
addAllin classAbstractCollection<T>
-
removeAll
public boolean removeAll(@Nonnull Collection<?> coll)
- Specified by:
removeAllin interfaceCollection<T>- Overrides:
removeAllin classAbstractCollection<T>
-
retainAll
public boolean retainAll(@Nonnull Collection<?> coll)
- Specified by:
retainAllin interfaceCollection<T>- Overrides:
retainAllin classAbstractCollection<T>
-
clear
public void clear()
- Specified by:
clearin interfaceCollection<T>- Overrides:
clearin classAbstractCollection<T>
-
toString
public String toString()
- Overrides:
toStringin classAbstractCollection<T>
-
remove
public boolean remove(Object o)
- Specified by:
removein interfaceCollection<T>- Overrides:
removein classAbstractCollection<T>
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfaceCollection<T>- Overrides:
hashCodein classEvictingRingBuffer<T>
-
equals
public boolean equals(Object obj)
- Specified by:
equalsin interfaceCollection<T>- Overrides:
equalsin classEvictingRingBuffer<T>
-
toArray
@Nonnull public <U> U[] toArray(@Nonnull U[] a)
- Specified by:
toArrayin interfaceCollection<T>- Overrides:
toArrayin classAbstractCollection<T>
-
-