001/*
002 * Copyright (C) 2010 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import com.google.common.annotations.GwtIncompatible;
018import com.google.common.collect.ForwardingQueue;
019import com.google.errorprone.annotations.CanIgnoreReturnValue;
020import java.util.Collection;
021import java.util.concurrent.BlockingQueue;
022import java.util.concurrent.TimeUnit;
023import javax.annotation.CheckForNull;
024
025/**
026 * A {@link BlockingQueue} which forwards all its method calls to another {@link BlockingQueue}.
027 * Subclasses should override one or more methods to modify the behavior of the backing collection
028 * as desired per the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator
029 * pattern</a>.
030 *
031 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
032 * default} methods. Instead, it inherits their default implementations. When those implementations
033 * invoke methods, they invoke methods on the {@code ForwardingBlockingQueue}.
034 *
035 * @author Raimundo Mirisola
036 * @param <E> the type of elements held in this collection
037 * @since 4.0
038 */
039@CanIgnoreReturnValue // TODO(cpovirk): Consider being more strict.
040@GwtIncompatible
041@ElementTypesAreNonnullByDefault
042public abstract class ForwardingBlockingQueue<E> extends ForwardingQueue<E>
043    implements BlockingQueue<E> {
044
045  /** Constructor for use by subclasses. */
046  protected ForwardingBlockingQueue() {}
047
048  @Override
049  protected abstract BlockingQueue<E> delegate();
050
051  @Override
052  public int drainTo(Collection<? super E> c, int maxElements) {
053    return delegate().drainTo(c, maxElements);
054  }
055
056  @Override
057  public int drainTo(Collection<? super E> c) {
058    return delegate().drainTo(c);
059  }
060
061  @Override
062  public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
063    return delegate().offer(e, timeout, unit);
064  }
065
066  @Override
067  @CheckForNull
068  public E poll(long timeout, TimeUnit unit) throws InterruptedException {
069    return delegate().poll(timeout, unit);
070  }
071
072  @Override
073  public void put(E e) throws InterruptedException {
074    delegate().put(e);
075  }
076
077  @Override
078  public int remainingCapacity() {
079    return delegate().remainingCapacity();
080  }
081
082  @Override
083  public E take() throws InterruptedException {
084    return delegate().take();
085  }
086}