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 org.checkerframework.checker.nullness.qual.Nullable; 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 042public abstract class ForwardingBlockingQueue<E> extends ForwardingQueue<E> 043 implements BlockingQueue<E> { 044 045 /** Constructor for use by subclasses. */ 046 protected ForwardingBlockingQueue() {} 047 048 @Override 049 protected abstract BlockingQueue<E> delegate(); 050 051 @CanIgnoreReturnValue 052 @Override 053 public int drainTo(Collection<? super E> c, int maxElements) { 054 return delegate().drainTo(c, maxElements); 055 } 056 057 @CanIgnoreReturnValue 058 @Override 059 public int drainTo(Collection<? super E> c) { 060 return delegate().drainTo(c); 061 } 062 063 @CanIgnoreReturnValue // TODO(kak): consider removing this 064 @Override 065 public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { 066 return delegate().offer(e, timeout, unit); 067 } 068 069 @CanIgnoreReturnValue // TODO(kak): consider removing this 070 @Override 071 public @Nullable 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}