001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import java.util.Collection; 020import java.util.concurrent.BlockingDeque; 021import java.util.concurrent.TimeUnit; 022 023/** 024 * A {@link BlockingDeque} which forwards all its method calls to another {@code BlockingDeque}. 025 * Subclasses should override one or more methods to modify the behavior of the backing deque as 026 * desired per the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 027 * 028 * <p><b>Warning:</b> The methods of {@code ForwardingBlockingDeque} forward 029 * <b>indiscriminately</b> to the methods of the delegate. For example, overriding {@link #add} 030 * alone <b>will not</b> change the behaviour of {@link #offer} which can lead to unexpected 031 * behaviour. In this case, you should override {@code offer} as well, either providing your own 032 * implementation, or delegating to the provided {@code standardOffer} method. 033 * 034 * <p> 035 * The {@code standard} methods are not guaranteed to be thread-safe, even when all of the methods 036 * that they depend on are thread-safe. 037 * 038 * @author Emily Soldal 039 * @since 14.0 040 */ 041public abstract class ForwardingBlockingDeque<E> extends ForwardingDeque<E> 042 implements BlockingDeque<E> { 043 044 /** Constructor for use by subclasses. */ 045 protected ForwardingBlockingDeque() {} 046 047 @Override 048 protected abstract BlockingDeque<E> delegate(); 049 050 @Override 051 public int remainingCapacity() { 052 return delegate().remainingCapacity(); 053 } 054 055 @Override 056 public void putFirst(E e) throws InterruptedException { 057 delegate().putFirst(e); 058 } 059 060 @Override 061 public void putLast(E e) throws InterruptedException { 062 delegate().putLast(e); 063 } 064 065 @Override 066 public boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException { 067 return delegate().offerFirst(e, timeout, unit); 068 } 069 070 @Override 071 public boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException { 072 return delegate().offerLast(e, timeout, unit); 073 } 074 075 @Override 076 public E takeFirst() throws InterruptedException { 077 return delegate().takeFirst(); 078 } 079 080 @Override 081 public E takeLast() throws InterruptedException { 082 return delegate().takeLast(); 083 } 084 085 @Override 086 public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException { 087 return delegate().pollFirst(timeout, unit); 088 } 089 090 @Override 091 public E pollLast(long timeout, TimeUnit unit) throws InterruptedException { 092 return delegate().pollLast(timeout, unit); 093 } 094 095 @Override 096 public void put(E e) throws InterruptedException { 097 delegate().put(e); 098 } 099 100 @Override 101 public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { 102 return delegate().offer(e, timeout, unit); 103 } 104 105 @Override 106 public E take() throws InterruptedException { 107 return delegate().take(); 108 } 109 110 @Override 111 public E poll(long timeout, TimeUnit unit) throws InterruptedException { 112 return delegate().poll(timeout, unit); 113 } 114 115 @Override 116 public int drainTo(Collection<? super E> c) { 117 return delegate().drainTo(c); 118 } 119 120 @Override 121 public int drainTo(Collection<? super E> c, int maxElements) { 122 return delegate().drainTo(c, maxElements); 123 } 124}