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@GwtIncompatible 040@ElementTypesAreNonnullByDefault 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 @CanIgnoreReturnValue 051 @Override 052 public int drainTo(Collection<? super E> c, int maxElements) { 053 return delegate().drainTo(c, maxElements); 054 } 055 056 @CanIgnoreReturnValue 057 @Override 058 public int drainTo(Collection<? super E> c) { 059 return delegate().drainTo(c); 060 } 061 062 @CanIgnoreReturnValue // TODO(kak): consider removing this 063 @Override 064 public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { 065 return delegate().offer(e, timeout, unit); 066 } 067 068 @CanIgnoreReturnValue // TODO(kak): consider removing this 069 @Override 070 @CheckForNull 071 public E poll(long timeout, TimeUnit unit) throws InterruptedException { 072 return delegate().poll(timeout, unit); 073 } 074 075 @Override 076 public void put(E e) throws InterruptedException { 077 delegate().put(e); 078 } 079 080 @Override 081 public int remainingCapacity() { 082 return delegate().remainingCapacity(); 083 } 084 085 @CanIgnoreReturnValue // TODO(kak): consider removing this 086 @Override 087 public E take() throws InterruptedException { 088 return delegate().take(); 089 } 090}