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;
023
024/**
025 * A {@link BlockingQueue} which forwards all its method calls to another {@link BlockingQueue}.
026 * Subclasses should override one or more methods to modify the behavior of the backing collection
027 * as desired per the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator
028 * pattern</a>.
029 *
030 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
031 * default} methods. Instead, it inherits their default implementations. When those implementations
032 * invoke methods, they invoke methods on the {@code ForwardingBlockingQueue}.
033 *
034 * @author Raimundo Mirisola
035 *
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
041public abstract class ForwardingBlockingQueue<E> extends ForwardingQueue<E>
042    implements BlockingQueue<E> {
043
044  /** Constructor for use by subclasses. */
045  protected ForwardingBlockingQueue() {}
046
047  @Override
048  protected abstract BlockingQueue<E> delegate();
049
050  @Override
051  public int drainTo(Collection<? super E> c, int maxElements) {
052    return delegate().drainTo(c, maxElements);
053  }
054
055  @Override
056  public int drainTo(Collection<? super E> c) {
057    return delegate().drainTo(c);
058  }
059
060  @Override
061  public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
062    return delegate().offer(e, timeout, unit);
063  }
064
065  @Override
066  public E poll(long timeout, TimeUnit unit) throws InterruptedException {
067    return delegate().poll(timeout, unit);
068  }
069
070  @Override
071  public void put(E e) throws InterruptedException {
072    delegate().put(e);
073  }
074
075  @Override
076  public int remainingCapacity() {
077    return delegate().remainingCapacity();
078  }
079
080  @Override
081  public E take() throws InterruptedException {
082    return delegate().take();
083  }
084}