1 /* 2 * Copyright (C) 2010 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.util.concurrent; 16 17 import com.google.common.annotations.GwtIncompatible; 18 import com.google.common.collect.ForwardingQueue; 19 import com.google.errorprone.annotations.CanIgnoreReturnValue; 20 import java.util.Collection; 21 import java.util.concurrent.BlockingQueue; 22 import java.util.concurrent.TimeUnit; 23 import javax.annotation.CheckForNull; 24 25 /** 26 * A {@link BlockingQueue} which forwards all its method calls to another {@link BlockingQueue}. 27 * Subclasses should override one or more methods to modify the behavior of the backing collection 28 * as desired per the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator 29 * pattern</a>. 30 * 31 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 32 * default} methods. Instead, it inherits their default implementations. When those implementations 33 * invoke methods, they invoke methods on the {@code ForwardingBlockingQueue}. 34 * 35 * @author Raimundo Mirisola 36 * @param <E> the type of elements held in this collection 37 * @since 4.0 38 */ 39 @CanIgnoreReturnValue // TODO(cpovirk): Consider being more strict. 40 @GwtIncompatible 41 @ElementTypesAreNonnullByDefault 42 public abstract class ForwardingBlockingQueue<E> extends ForwardingQueue<E> 43 implements BlockingQueue<E> { 44 45 /** Constructor for use by subclasses. */ ForwardingBlockingQueue()46 protected ForwardingBlockingQueue() {} 47 48 @Override delegate()49 protected abstract BlockingQueue<E> delegate(); 50 51 @Override drainTo(Collection<? super E> c, int maxElements)52 public int drainTo(Collection<? super E> c, int maxElements) { 53 return delegate().drainTo(c, maxElements); 54 } 55 56 @Override drainTo(Collection<? super E> c)57 public int drainTo(Collection<? super E> c) { 58 return delegate().drainTo(c); 59 } 60 61 @Override offer(E e, long timeout, TimeUnit unit)62 public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { 63 return delegate().offer(e, timeout, unit); 64 } 65 66 @Override 67 @CheckForNull poll(long timeout, TimeUnit unit)68 public E poll(long timeout, TimeUnit unit) throws InterruptedException { 69 return delegate().poll(timeout, unit); 70 } 71 72 @Override put(E e)73 public void put(E e) throws InterruptedException { 74 delegate().put(e); 75 } 76 77 @Override remainingCapacity()78 public int remainingCapacity() { 79 return delegate().remainingCapacity(); 80 } 81 82 @Override take()83 public E take() throws InterruptedException { 84 return delegate().take(); 85 } 86 } 87