@Beta @GwtCompatible public final class EvictingQueue<E> extends ForwardingQueue<E> implements SerializableA non-blocking queue which automatically evicts elements from the head of the queue when attempting to add new elements onto the queue and it is full. This queue orders elements FIFO (first-in-first-out). This data structure is logically equivalent to a circular buffer (i.e., cyclic buffer or ring buffer).
An evicting queue must be configured with a maximum size. Each time an element is added to a full queue, the queue automatically removes its head element. This is different from conventional bounded queues, which either block or reject new elements when full.
This class is not thread-safe, and does not accept null elements.
- Kurt Alfred Kluever
- See Also:
- Serialized Form
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description
add(E e)Adds the given element to this queue.
addAll(Collection<? extends E> collection)Adds all of the elements in the specified collection to this collection (optional operation).
static <E> EvictingQueue<E>
create(int maxSize)Creates and returns a new evicting queue that will hold up to
delegate()Returns the backing delegate instance that methods are forwarded to.
offer(E e)Adds the given element to this queue.
remainingCapacity()Returns the number of additional elements that this queue can accept without evicting; zero if the queue is currently full.
toArray()Returns an array containing all of the elements in this collection.
Methods inherited from class com.google.common.collect.ForwardingQueue
element, peek, poll, remove, standardOffer, standardPeek, standardPoll
Methods inherited from class com.google.common.collect.ForwardingCollection
clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, standardAddAll, standardClear, standardContains, standardContainsAll, standardIsEmpty, standardRemove, standardRemoveAll, standardRetainAll, standardToArray, standardToArray, standardToString, toArray
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
clear, contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray
public static <E> EvictingQueue<E> create(int maxSize)Creates and returns a new evicting queue that will hold up to
maxSizeis zero, elements will be evicted immediately after being added to the queue.
public int remainingCapacity()
delegateDescription copied from class:
ForwardingObjectReturns the backing delegate instance that methods are forwarded to. Abstract subclasses generally override this method with an abstract method that has a more specific return type, such as
ForwardingSet.delegate(). Concrete subclasses override this method to supply the instance being decorated.
@CanIgnoreReturnValue public boolean offer(E e)Adds the given element to this queue. If the queue is currently full, the element at the head of the queue is evicted to make room.
@CanIgnoreReturnValue public boolean add(E e)Adds the given element to this queue. If the queue is currently full, the element at the head of the queue is evicted to make room.
@CanIgnoreReturnValue public boolean addAll(Collection<? extends E> collection)Description copied from interface:
java.util.CollectionAdds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)
toArrayDescription copied from interface:
java.util.CollectionReturns an array containing all of the elements in this collection. If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array's runtime component type is
The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.