Top Description Fields Constructors Methods
com.google.common.collect

public final Class EvictingQueue<E>

extends ForwardingQueue<E>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull

A 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.

Author
Kurt Alfred Kluever
Since
15.0

Field Summary

Modifier and TypeField and Description
private final Queue<E>
pack-priv final int
private static final long

Constructor Summary

AccessConstructor and Description
private
EvictingQueue(int maxSize)

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true always
add
(E
the element to add
e
)

Overrides com.google.common.collect.ForwardingCollection.add.

Implements java.util.Queue.add, java.util.Collection.add.

Adds the given element to this queue.
public boolean
addAll(Collection<? extends E>
collection containing elements to be added to this collection
collection
)

Overrides com.google.common.collect.ForwardingCollection.addAll.

Implements java.util.Collection.addAll.

Adds all of the elements in the specified collection to this collection (optional operation).
public static <E> EvictingQueue<E>
create(int maxSize)

Creates and returns a new evicting queue that will hold up to maxSize elements.

protected Queue<E>
delegate()

Implements abstract com.google.common.collect.ForwardingQueue.delegate.

Returns the backing delegate instance that methods are forwarded to.
public boolean

Returns:

true always
offer
(E
the element to add
e
)

Overrides com.google.common.collect.ForwardingQueue.offer.

Implements java.util.Queue.offer.

Adds the given element to this queue.
public int
remainingCapacity()

Returns the number of additional elements that this queue can accept without evicting; zero if the queue is currently full.

public Object[]
toArray()

Overrides com.google.common.collect.ForwardingCollection.toArray.

Implements java.util.Collection.toArray.

Returns an array containing all of the elements in this collection.
Inherited from com.google.common.collect.ForwardingQueue:
elementpeekpollremovestandardOfferstandardPeekstandardPoll

Field Detail

delegateback to summary
private final Queue<E> delegate
maxSizeback to summary
pack-priv final int maxSize
Annotations
@VisibleForTesting
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

EvictingQueueback to summary
private EvictingQueue(int maxSize)

Method Detail

addback to summary
public boolean add(E e)

Overrides com.google.common.collect.ForwardingCollection.add.

Implements java.util.Queue.add, java.util.Collection.add.

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.

Parameters
e:E

Doc from java.util.Queue.add.

the element to add

Returns:boolean

true always

Annotations
@Override
@CanIgnoreReturnValue
addAllback to summary
public boolean addAll(Collection<? extends E> collection)

Overrides com.google.common.collect.ForwardingCollection.addAll.

Implements java.util.Collection.addAll.

Doc from java.util.Collection.addAll.

Adds 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.) If the specified collection has a defined encounter order, processing of its elements generally occurs in that order.

Parameters
collection:Collection<? extends E>

collection containing elements to be added to this collection

Returns:boolean

true if this collection changed as a result of the call

Annotations
@Override
@CanIgnoreReturnValue
createback to summary
public static <E> EvictingQueue<E> create(int maxSize)

Creates and returns a new evicting queue that will hold up to maxSize elements.

When maxSize is zero, elements will be evicted immediately after being added to the queue.

delegateback to summary
protected Queue<E> delegate()

Implements abstract com.google.common.collect.ForwardingQueue.delegate.

Doc from com.google.common.collect.ForwardingObject.delegate.

Returns 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.

Annotations
@Override
offerback to summary
public boolean offer(E e)

Overrides com.google.common.collect.ForwardingQueue.offer.

Implements java.util.Queue.offer.

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.

Parameters
e:E

Doc from java.util.Queue.offer.

the element to add

Returns:boolean

true always

Annotations
@Override
@CanIgnoreReturnValue
remainingCapacityback to summary
public int remainingCapacity()

Returns the number of additional elements that this queue can accept without evicting; zero if the queue is currently full.

Since
16.0
toArrayback to summary
public Object[] toArray()

Overrides com.google.common.collect.ForwardingCollection.toArray.

Implements java.util.Collection.toArray.

Doc from java.util.Collection.toArray.

Returns 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 Object.

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.

Returns:Object[]

an array, whose runtime component type is Object, containing all of the elements in this collection

Annotations
@Override
@J2ktIncompatible