Package com.google.common.collect
Class ForwardingDeque<E>
- java.lang.Object
-
- com.google.common.collect.ForwardingObject
-
- com.google.common.collect.ForwardingCollection<E>
-
- com.google.common.collect.ForwardingQueue<E>
-
- com.google.common.collect.ForwardingDeque<E>
-
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Deque<E>
,Queue<E>
- Direct Known Subclasses:
ForwardingBlockingDeque
,ForwardingBlockingDeque
@GwtIncompatible public abstract class ForwardingDeque<E> extends ForwardingQueue<E> implements Deque<E>
A deque which forwards all its method calls to another deque. Subclasses should override one or more methods to modify the behavior of the backing deque as desired per the decorator pattern.Warning: The methods of
ForwardingDeque
forward indiscriminately to the methods of the delegate. For example, overridingForwardingCollection.add(E)
alone will not change the behavior ofForwardingQueue.offer(E)
which can lead to unexpected behavior. In this case, you should overrideoffer
as well.default
method warning: This class does not forward calls todefault
methods. Instead, it inherits their default implementations. When those implementations invoke methods, they invoke methods on theForwardingDeque
.- Since:
- 12.0
- Author:
- Kurt Alfred Kluever
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ForwardingDeque()
Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addFirst(E e)
Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing anIllegalStateException
if no space is currently available.void
addLast(E e)
Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing anIllegalStateException
if no space is currently available.protected abstract Deque<E>
delegate()
Returns the backing delegate instance that methods are forwarded to.Iterator<E>
descendingIterator()
Returns an iterator over the elements in this deque in reverse sequential order.E
getFirst()
Retrieves, but does not remove, the first element of this deque.E
getLast()
Retrieves, but does not remove, the last element of this deque.boolean
offerFirst(E e)
Inserts the specified element at the front of this deque unless it would violate capacity restrictions.boolean
offerLast(E e)
Inserts the specified element at the end of this deque unless it would violate capacity restrictions.E
peekFirst()
Retrieves, but does not remove, the first element of this deque, or returnsnull
if this deque is empty.E
peekLast()
Retrieves, but does not remove, the last element of this deque, or returnsnull
if this deque is empty.E
pollFirst()
Retrieves and removes the first element of this deque, or returnsnull
if this deque is empty.E
pollLast()
Retrieves and removes the last element of this deque, or returnsnull
if this deque is empty.E
pop()
Pops an element from the stack represented by this deque.void
push(E e)
Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing anIllegalStateException
if no space is currently available.E
removeFirst()
Retrieves and removes the first element of this deque.boolean
removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element from this deque.E
removeLast()
Retrieves and removes the last element of this deque.boolean
removeLastOccurrence(Object o)
Removes the last occurrence of the specified element from this deque.-
Methods inherited from class com.google.common.collect.ForwardingQueue
element, offer, peek, poll, remove, standardOffer, standardPeek, standardPoll
-
Methods inherited from class com.google.common.collect.ForwardingCollection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, 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
-
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, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
-
-
-
Constructor Detail
-
ForwardingDeque
protected ForwardingDeque()
Constructor for use by subclasses.
-
-
Method Detail
-
delegate
protected abstract Deque<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>
-
addFirst
public void addFirst(E e)
Description copied from interface:java.util.Deque
Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing anIllegalStateException
if no space is currently available. When using a capacity-restricted deque, it is generally preferable to use methodDeque.offerFirst(E)
.
-
addLast
public void addLast(E e)
Description copied from interface:java.util.Deque
Inserts the specified element at the end of this deque if it is possible to do so immediately without violating capacity restrictions, throwing anIllegalStateException
if no space is currently available. When using a capacity-restricted deque, it is generally preferable to use methodDeque.offerLast(E)
.This method is equivalent to
Deque.add(E)
.
-
descendingIterator
public Iterator<E> descendingIterator()
Description copied from interface:java.util.Deque
Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head).- Specified by:
descendingIterator
in interfaceDeque<E>
- Returns:
- an iterator over the elements in this deque in reverse sequence
-
getFirst
public E getFirst()
Description copied from interface:java.util.Deque
Retrieves, but does not remove, the first element of this deque. This method differs frompeekFirst
only in that it throws an exception if this deque is empty.
-
getLast
public E getLast()
Description copied from interface:java.util.Deque
Retrieves, but does not remove, the last element of this deque. This method differs frompeekLast
only in that it throws an exception if this deque is empty.
-
offerFirst
@CanIgnoreReturnValue public boolean offerFirst(E e)
Description copied from interface:java.util.Deque
Inserts the specified element at the front of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to theDeque.addFirst(E)
method, which can fail to insert an element only by throwing an exception.- Specified by:
offerFirst
in interfaceDeque<E>
- Parameters:
e
- the element to add- Returns:
true
if the element was added to this deque, elsefalse
-
offerLast
@CanIgnoreReturnValue public boolean offerLast(E e)
Description copied from interface:java.util.Deque
Inserts the specified element at the end of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to theDeque.addLast(E)
method, which can fail to insert an element only by throwing an exception.
-
peekFirst
public E peekFirst()
Description copied from interface:java.util.Deque
Retrieves, but does not remove, the first element of this deque, or returnsnull
if this deque is empty.
-
peekLast
public E peekLast()
Description copied from interface:java.util.Deque
Retrieves, but does not remove, the last element of this deque, or returnsnull
if this deque is empty.
-
pollFirst
@CanIgnoreReturnValue public E pollFirst()
Description copied from interface:java.util.Deque
Retrieves and removes the first element of this deque, or returnsnull
if this deque is empty.
-
pollLast
@CanIgnoreReturnValue public E pollLast()
Description copied from interface:java.util.Deque
Retrieves and removes the last element of this deque, or returnsnull
if this deque is empty.
-
pop
@CanIgnoreReturnValue public E pop()
Description copied from interface:java.util.Deque
Pops an element from the stack represented by this deque. In other words, removes and returns the first element of this deque.This method is equivalent to
Deque.removeFirst()
.
-
push
public void push(E e)
Description copied from interface:java.util.Deque
Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing anIllegalStateException
if no space is currently available.This method is equivalent to
Deque.addFirst(E)
.
-
removeFirst
@CanIgnoreReturnValue public E removeFirst()
Description copied from interface:java.util.Deque
Retrieves and removes the first element of this deque. This method differs frompollFirst
only in that it throws an exception if this deque is empty.- Specified by:
removeFirst
in interfaceDeque<E>
- Returns:
- the head of this deque
-
removeLast
@CanIgnoreReturnValue public E removeLast()
Description copied from interface:java.util.Deque
Retrieves and removes the last element of this deque. This method differs frompollLast
only in that it throws an exception if this deque is empty.- Specified by:
removeLast
in interfaceDeque<E>
- Returns:
- the tail of this deque
-
removeFirstOccurrence
@CanIgnoreReturnValue public boolean removeFirstOccurrence(Object o)
Description copied from interface:java.util.Deque
Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first elemente
such thatObjects.equals(o, e)
(if such an element exists). Returnstrue
if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).- Specified by:
removeFirstOccurrence
in interfaceDeque<E>
- Parameters:
o
- element to be removed from this deque, if present- Returns:
true
if an element was removed as a result of this call
-
removeLastOccurrence
@CanIgnoreReturnValue public boolean removeLastOccurrence(Object o)
Description copied from interface:java.util.Deque
Removes the last occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the last elemente
such thatObjects.equals(o, e)
(if such an element exists). Returnstrue
if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).- Specified by:
removeLastOccurrence
in interfaceDeque<E>
- Parameters:
o
- element to be removed from this deque, if present- Returns:
true
if an element was removed as a result of this call
-
-