001    // Copyright 2010 Google Inc. All Rights Reserved.
002    
003    package com.google.common.util.concurrent;
004    
005    import com.google.common.collect.ForwardingQueue;
006    
007    import java.util.Collection;
008    import java.util.concurrent.BlockingQueue;
009    import java.util.concurrent.TimeUnit;
010    
011    /**
012     * A {@link BlockingQueue} which forwards all its method calls to another
013     * {@link BlockingQueue}. Subclasses should override one or more methods to
014     * modify the behavior of the backing collection as desired per the <a
015     * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
016     *
017     * @author Raimundo Mirisola
018     *
019     * @param <E> the type of elements held in this collection
020     * @since 4
021     */
022    public abstract class ForwardingBlockingQueue<E> extends ForwardingQueue<E>
023        implements BlockingQueue<E> {
024    
025      /** Constructor for use by subclasses. */
026      protected ForwardingBlockingQueue() {}
027    
028      @Override protected abstract BlockingQueue<E> delegate();
029    
030      @Override public int drainTo(
031          Collection<? super E> c, int maxElements) {
032        return delegate().drainTo(c, maxElements);
033      }
034    
035      @Override public int drainTo(Collection<? super E> c) {
036        return delegate().drainTo(c);
037      }
038    
039      @Override public boolean offer(E e, long timeout, TimeUnit unit)
040          throws InterruptedException {
041        return delegate().offer(e, timeout, unit);
042      }
043    
044      @Override public E poll(long timeout, TimeUnit unit)
045          throws InterruptedException {
046        return delegate().poll(timeout, unit);
047      }
048    
049      @Override public void put(E e) throws InterruptedException {
050        delegate().put(e);
051      }
052    
053      @Override public int remainingCapacity() {
054        return delegate().remainingCapacity();
055      }
056    
057      @Override public E take() throws InterruptedException {
058        return delegate().take();
059      }
060    }