Package com.google.common.collect
Class EvictingQueue<E>
-
- com.google.common.collect.ForwardingObject
-
- com.google.common.collect.ForwardingCollection<E>
-
- com.google.common.collect.ForwardingQueue<E>
-
- com.google.common.collect.EvictingQueue<E>
-
- All Implemented Interfaces:
,
<E>
,<E>
,<E>
@Beta @GwtCompatible public final class EvictingQueue<E> extends ForwardingQueue<E> implements
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.
- Since:
- 15.0
- Author:
- Kurt Alfred Kluever
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(E e)
Adds the given element to this queue.boolean
<? extends E> collection)
Adds all of the elements in the specified collection to this collection (optional operation).boolean
object)
Returnstrue
if this collection contains the specified element.static <E> EvictingQueue<E>
create(int maxSize)
Creates and returns a new evicting queue that will hold up tomaxSize
elements.protected <E>
delegate()
Returns the backing delegate instance that methods are forwarded to.boolean
offer(E e)
Adds the given element to this queue.int
remainingCapacity()
Returns the number of additional elements that this queue can accept without evicting; zero if the queue is currently full.boolean
object)
Removes a single instance of the specified element from this collection, if it is present (optional operation).-
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, containsAll, isEmpty, iterator, removeAll, retainAll, size, standardAddAll, standardClear, standardContains, standardContainsAll, standardIsEmpty, standardRemove, standardRemoveAll, standardRetainAll, standardToArray, standardToArray, standardToString, toArray, toArray
-
Methods inherited from class com.google.common.collect.ForwardingObject
toString
-
-
-
-
Method Detail
-
create
public static <E> EvictingQueue<E> create(int maxSize)
Creates and returns a new evicting queue that will hold up tomaxSize
elements.When
maxSize
is zero, elements will be evicted immediately after being added to the queue.
-
remainingCapacity
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
-
delegate
protected <E> delegate()
Description copied from class:ForwardingObject
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 asForwardingSet.delegate()
. Concrete subclasses override this method to supply the instance being decorated.- Specified by:
delegate
in classForwardingQueue<E>
-
offer
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.- Specified by:
in interface
<E>
- Overrides:
offer
in classForwardingQueue<E>
- Parameters:
e
- the element to add- Returns:
true
always
-
add
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.- Specified by:
in interface
<E>
- Specified by:
in interface
<E>
- Overrides:
add
in classForwardingCollection<E>
- Parameters:
e
- element whose presence in this collection is to be ensured- Returns:
true
always
-
addAll
public boolean <? extends E> collection)
Description copied from interface: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.)- Specified by:
in interface
<E>
- Overrides:
addAll
in classForwardingCollection<E>
- Parameters:
collection
- collection containing elements to be added to this collection- Returns:
true
if this collection changed as a result of the call- See Also:
-
contains
public boolean object)
Description copied from interface:Returnstrue
if this collection contains the specified element. More formally, returnstrue
if and only if this collection contains at least one elemente
such thatObjects.equals(o, e)
.- Specified by:
in interface
<E>
- Overrides:
contains
in classForwardingCollection<E>
- Parameters:
object
- element whose presence in this collection is to be tested- Returns:
true
if this collection contains the specified element
-
remove
public boolean object)
Description copied from interface:Removes a single instance of the specified element from this collection, if it is present (optional operation). More formally, removes an elemente
such thatObjects.equals(o, e)
, if this collection contains one or more such elements. Returnstrue
if this collection contained the specified element (or equivalently, if this collection changed as a result of the call).- Specified by:
in interface
<E>
- Overrides:
remove
in classForwardingCollection<E>
- Parameters:
object
- element to be removed from this collection, if present- Returns:
true
if an element was removed as a result of this call
-
-