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 * @param <E> the type of elements held in this collection 036 * @since 4.0 037 */ 038@CanIgnoreReturnValue // TODO(cpovirk): Consider being more strict. 039@GwtIncompatible 040public abstract class ForwardingBlockingQueue<E> extends ForwardingQueue<E> 041 implements BlockingQueue<E> { 042 043 /** Constructor for use by subclasses. */ 044 protected ForwardingBlockingQueue() {} 045 046 @Override 047 protected abstract BlockingQueue<E> delegate(); 048 049 @Override 050 public int drainTo(Collection<? super E> c, int maxElements) { 051 return delegate().drainTo(c, maxElements); 052 } 053 054 @Override 055 public int drainTo(Collection<? super E> c) { 056 return delegate().drainTo(c); 057 } 058 059 @Override 060 public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { 061 return delegate().offer(e, timeout, unit); 062 } 063 064 @Override 065 public E poll(long timeout, TimeUnit unit) throws InterruptedException { 066 return delegate().poll(timeout, unit); 067 } 068 069 @Override 070 public void put(E e) throws InterruptedException { 071 delegate().put(e); 072 } 073 074 @Override 075 public int remainingCapacity() { 076 return delegate().remainingCapacity(); 077 } 078 079 @Override 080 public E take() throws InterruptedException { 081 return delegate().take(); 082 } 083}