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.annotations.J2ktIncompatible;
019import com.google.common.collect.ForwardingQueue;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import java.util.Collection;
022import java.util.concurrent.BlockingQueue;
023import java.util.concurrent.TimeUnit;
024import javax.annotation.CheckForNull;
025
026/**
027 * A {@link BlockingQueue} which forwards all its method calls to another {@link BlockingQueue}.
028 * Subclasses should override one or more methods to modify the behavior of the backing collection
029 * as desired per the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator
030 * pattern</a>.
031 *
032 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
033 * default} methods. Instead, it inherits their default implementations. When those implementations
034 * invoke methods, they invoke methods on the {@code ForwardingBlockingQueue}.
035 *
036 * @author Raimundo Mirisola
037 * @param <E> the type of elements held in this collection
038 * @since 4.0
039 */
040@J2ktIncompatible
041@GwtIncompatible
042@ElementTypesAreNonnullByDefault
043public abstract class ForwardingBlockingQueue<E> extends ForwardingQueue<E>
044    implements BlockingQueue<E> {
045
046  /** Constructor for use by subclasses. */
047  protected ForwardingBlockingQueue() {}
048
049  @Override
050  protected abstract BlockingQueue<E> delegate();
051
052  @CanIgnoreReturnValue
053  @Override
054  public int drainTo(Collection<? super E> c, int maxElements) {
055    return delegate().drainTo(c, maxElements);
056  }
057
058  @CanIgnoreReturnValue
059  @Override
060  public int drainTo(Collection<? super E> c) {
061    return delegate().drainTo(c);
062  }
063
064  @CanIgnoreReturnValue // TODO(kak): consider removing this
065  @Override
066  public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
067    return delegate().offer(e, timeout, unit);
068  }
069
070  @CanIgnoreReturnValue // TODO(kak): consider removing this
071  @Override
072  @CheckForNull
073  public E poll(long timeout, TimeUnit unit) throws InterruptedException {
074    return delegate().poll(timeout, unit);
075  }
076
077  @Override
078  public void put(E e) throws InterruptedException {
079    delegate().put(e);
080  }
081
082  @Override
083  public int remainingCapacity() {
084    return delegate().remainingCapacity();
085  }
086
087  @CanIgnoreReturnValue // TODO(kak): consider removing this
088  @Override
089  public E take() throws InterruptedException {
090    return delegate().take();
091  }
092}