Class ForwardingBlockingDeque<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>
-
- com.google.common.util.concurrent.ForwardingBlockingDeque<E>
-
- All Implemented Interfaces:
Iterable<E>,Collection<E>,BlockingDeque<E>,BlockingQueue<E>,Deque<E>,Queue<E>
@GwtIncompatible public abstract class ForwardingBlockingDeque<E> extends ForwardingDeque<E> implements BlockingDeque<E>
ABlockingDequewhich forwards all its method calls to anotherBlockingDeque. 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
ForwardingBlockingDequeforward indiscriminately to the methods of the delegate. For example, overridingForwardingCollection.add(E)alone will not change the behaviour ofoffer(E, long, java.util.concurrent.TimeUnit)which can lead to unexpected behaviour. In this case, you should overrideofferas well, either providing your own implementation, or delegating to the providedstandardOffermethod.defaultmethod warning: This class does not forward calls todefaultmethods. Instead, it inherits their default implementations. When those implementations invoke methods, they invoke methods on theForwardingBlockingDeque.The
standardmethods are not guaranteed to be thread-safe, even when all of the methods that they depend on are thread-safe.- Since:
- 21.0 (since 14.0 as
ForwardingBlockingDeque) - Author:
- Emily Soldal
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedForwardingBlockingDeque()Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract BlockingDeque<E>delegate()Returns the backing delegate instance that methods are forwarded to.intdrainTo(Collection<? super E> c)Removes all available elements from this queue and adds them to the given collection.intdrainTo(Collection<? super E> c, int maxElements)Removes at most the given number of available elements from this queue and adds them to the given collection.booleanoffer(E e, long timeout, TimeUnit unit)Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting up to the specified wait time if necessary for space to become available.booleanofferFirst(E e, long timeout, TimeUnit unit)Inserts the specified element at the front of this deque, waiting up to the specified wait time if necessary for space to become available.booleanofferLast(E e, long timeout, TimeUnit unit)Inserts the specified element at the end of this deque, waiting up to the specified wait time if necessary for space to become available.Epoll(long timeout, TimeUnit unit)Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting up to the specified wait time if necessary for an element to become available.EpollFirst(long timeout, TimeUnit unit)Retrieves and removes the first element of this deque, waiting up to the specified wait time if necessary for an element to become available.EpollLast(long timeout, TimeUnit unit)Retrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available.voidput(E e)Inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting if necessary for space to become available.voidputFirst(E e)Inserts the specified element at the front of this deque, waiting if necessary for space to become available.voidputLast(E e)Inserts the specified element at the end of this deque, waiting if necessary for space to become available.intremainingCapacity()Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, orInteger.MAX_VALUEif there is no intrinsic limit.Etake()Retrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting if necessary until an element becomes available.EtakeFirst()Retrieves and removes the first element of this deque, waiting if necessary until an element becomes available.EtakeLast()Retrieves and removes the last element of this deque, waiting if necessary until an element becomes available.-
Methods inherited from class com.google.common.collect.ForwardingDeque
addFirst, addLast, descendingIterator, getFirst, getLast, offerFirst, offerLast, peekFirst, peekLast, pollFirst, pollLast, pop, push, removeFirst, removeFirstOccurrence, removeLast, removeLastOccurrence
-
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.concurrent.BlockingDeque
add, addFirst, addLast, contains, element, iterator, offer, offerFirst, offerLast, peek, poll, push, remove, remove, removeFirstOccurrence, removeLastOccurrence, size
-
Methods inherited from interface java.util.Collection
clear, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
-
-
-
Constructor Detail
-
ForwardingBlockingDeque
protected ForwardingBlockingDeque()
Constructor for use by subclasses.
-
-
Method Detail
-
delegate
protected abstract BlockingDeque<E> delegate()
Description 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 asForwardingSet.delegate(). Concrete subclasses override this method to supply the instance being decorated.- Specified by:
delegatein classForwardingDeque<E>
-
remainingCapacity
public int remainingCapacity()
Description copied from interface:java.util.concurrent.BlockingQueueReturns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking, orInteger.MAX_VALUEif there is no intrinsic limit.Note that you cannot always tell if an attempt to insert an element will succeed by inspecting
remainingCapacitybecause it may be the case that another thread is about to insert or remove an element.- Specified by:
remainingCapacityin interfaceBlockingQueue<E>- Returns:
- the remaining capacity
-
putFirst
public void putFirst(E e) throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeInserts the specified element at the front of this deque, waiting if necessary for space to become available.- Specified by:
putFirstin interfaceBlockingDeque<E>- Parameters:
e- the element to add- Throws:
InterruptedException- if interrupted while waiting
-
putLast
public void putLast(E e) throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeInserts the specified element at the end of this deque, waiting if necessary for space to become available.- Specified by:
putLastin interfaceBlockingDeque<E>- Parameters:
e- the element to add- Throws:
InterruptedException- if interrupted while waiting
-
offerFirst
public boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeInserts the specified element at the front of this deque, waiting up to the specified wait time if necessary for space to become available.- Specified by:
offerFirstin interfaceBlockingDeque<E>- Parameters:
e- the element to addtimeout- how long to wait before giving up, in units ofunitunit- aTimeUnitdetermining how to interpret thetimeoutparameter- Returns:
trueif successful, orfalseif the specified waiting time elapses before space is available- Throws:
InterruptedException- if interrupted while waiting
-
offerLast
public boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeInserts the specified element at the end of this deque, waiting up to the specified wait time if necessary for space to become available.- Specified by:
offerLastin interfaceBlockingDeque<E>- Parameters:
e- the element to addtimeout- how long to wait before giving up, in units ofunitunit- aTimeUnitdetermining how to interpret thetimeoutparameter- Returns:
trueif successful, orfalseif the specified waiting time elapses before space is available- Throws:
InterruptedException- if interrupted while waiting
-
takeFirst
public E takeFirst() throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeRetrieves and removes the first element of this deque, waiting if necessary until an element becomes available.- Specified by:
takeFirstin interfaceBlockingDeque<E>- Returns:
- the head of this deque
- Throws:
InterruptedException- if interrupted while waiting
-
takeLast
public E takeLast() throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeRetrieves and removes the last element of this deque, waiting if necessary until an element becomes available.- Specified by:
takeLastin interfaceBlockingDeque<E>- Returns:
- the tail of this deque
- Throws:
InterruptedException- if interrupted while waiting
-
pollFirst
@CheckForNull public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeRetrieves and removes the first element of this deque, waiting up to the specified wait time if necessary for an element to become available.- Specified by:
pollFirstin interfaceBlockingDeque<E>- Parameters:
timeout- how long to wait before giving up, in units ofunitunit- aTimeUnitdetermining how to interpret thetimeoutparameter- Returns:
- the head of this deque, or
nullif the specified waiting time elapses before an element is available - Throws:
InterruptedException- if interrupted while waiting
-
pollLast
@CheckForNull public E pollLast(long timeout, TimeUnit unit) throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeRetrieves and removes the last element of this deque, waiting up to the specified wait time if necessary for an element to become available.- Specified by:
pollLastin interfaceBlockingDeque<E>- Parameters:
timeout- how long to wait before giving up, in units ofunitunit- aTimeUnitdetermining how to interpret thetimeoutparameter- Returns:
- the tail of this deque, or
nullif the specified waiting time elapses before an element is available - Throws:
InterruptedException- if interrupted while waiting
-
put
public void put(E e) throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeInserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting if necessary for space to become available.This method is equivalent to
putLast.- Specified by:
putin interfaceBlockingDeque<E>- Specified by:
putin interfaceBlockingQueue<E>- Parameters:
e- the element to add- Throws:
InterruptedException- if interrupted while waiting
-
offer
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeInserts the specified element into the queue represented by this deque (in other words, at the tail of this deque), waiting up to the specified wait time if necessary for space to become available.This method is equivalent to
offerLast.- Specified by:
offerin interfaceBlockingDeque<E>- Specified by:
offerin interfaceBlockingQueue<E>- Parameters:
e- the element to addtimeout- how long to wait before giving up, in units ofunitunit- aTimeUnitdetermining how to interpret thetimeoutparameter- Returns:
trueif the element was added to this deque, elsefalse- Throws:
InterruptedException- if interrupted while waiting
-
take
public E take() throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeRetrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting if necessary until an element becomes available.This method is equivalent to
takeFirst.- Specified by:
takein interfaceBlockingDeque<E>- Specified by:
takein interfaceBlockingQueue<E>- Returns:
- the head of this deque
- Throws:
InterruptedException- if interrupted while waiting
-
poll
@CheckForNull public E poll(long timeout, TimeUnit unit) throws InterruptedException
Description copied from interface:java.util.concurrent.BlockingDequeRetrieves and removes the head of the queue represented by this deque (in other words, the first element of this deque), waiting up to the specified wait time if necessary for an element to become available.This method is equivalent to
pollFirst.- Specified by:
pollin interfaceBlockingDeque<E>- Specified by:
pollin interfaceBlockingQueue<E>- Parameters:
timeout- how long to wait before giving up, in units ofunitunit- aTimeUnitdetermining how to interpret thetimeoutparameter- Returns:
- the head of this deque, or
nullif the specified waiting time elapses before an element is available - Throws:
InterruptedException- if interrupted while waiting
-
drainTo
public int drainTo(Collection<? super E> c)
Description copied from interface:java.util.concurrent.BlockingQueueRemoves all available elements from this queue and adds them to the given collection. This operation may be more efficient than repeatedly polling this queue. A failure encountered while attempting to add elements to collectioncmay result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result inIllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.- Specified by:
drainToin interfaceBlockingQueue<E>- Parameters:
c- the collection to transfer elements into- Returns:
- the number of elements transferred
-
drainTo
public int drainTo(Collection<? super E> c, int maxElements)
Description copied from interface:java.util.concurrent.BlockingQueueRemoves at most the given number of available elements from this queue and adds them to the given collection. A failure encountered while attempting to add elements to collectioncmay result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result inIllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.- Specified by:
drainToin interfaceBlockingQueue<E>- Parameters:
c- the collection to transfer elements intomaxElements- the maximum number of elements to transfer- Returns:
- the number of elements transferred
-
-