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.util.concurrent; 018 019import com.google.common.annotations.GwtIncompatible; 020import com.google.common.annotations.J2ktIncompatible; 021import com.google.common.collect.ForwardingDeque; 022import java.util.Collection; 023import java.util.concurrent.BlockingDeque; 024import java.util.concurrent.TimeUnit; 025import org.jspecify.annotations.Nullable; 026 027/** 028 * A {@link BlockingDeque} which forwards all its method calls to another {@code BlockingDeque}. 029 * Subclasses should override one or more methods to modify the behavior of the backing deque as 030 * desired per the <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 031 * 032 * <p><b>Warning:</b> The methods of {@code ForwardingBlockingDeque} forward <b>indiscriminately</b> 033 * to the methods of the delegate. For example, overriding {@link #add} alone <b>will not</b> change 034 * the behaviour of {@link #offer} which can lead to unexpected behaviour. In this case, you should 035 * override {@code offer} as well, either providing your own implementation, or delegating to the 036 * provided {@code standardOffer} method. 037 * 038 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 039 * default} methods. Instead, it inherits their default implementations. When those implementations 040 * invoke methods, they invoke methods on the {@code ForwardingBlockingDeque}. 041 * 042 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even when all of the 043 * methods that they depend on are thread-safe. 044 * 045 * @author Emily Soldal 046 * @since 21.0 (since 14.0 as {@link com.google.common.collect.ForwardingBlockingDeque}) 047 */ 048@J2ktIncompatible 049@GwtIncompatible 050public abstract class ForwardingBlockingDeque<E> extends ForwardingDeque<E> 051 implements BlockingDeque<E> { 052 053 /** Constructor for use by subclasses. */ 054 protected ForwardingBlockingDeque() {} 055 056 @Override 057 protected abstract BlockingDeque<E> delegate(); 058 059 @Override 060 public int remainingCapacity() { 061 return delegate().remainingCapacity(); 062 } 063 064 @Override 065 public void putFirst(E e) throws InterruptedException { 066 delegate().putFirst(e); 067 } 068 069 @Override 070 public void putLast(E e) throws InterruptedException { 071 delegate().putLast(e); 072 } 073 074 @Override 075 public boolean offerFirst(E e, long timeout, TimeUnit unit) throws InterruptedException { 076 return delegate().offerFirst(e, timeout, unit); 077 } 078 079 @Override 080 public boolean offerLast(E e, long timeout, TimeUnit unit) throws InterruptedException { 081 return delegate().offerLast(e, timeout, unit); 082 } 083 084 @Override 085 public E takeFirst() throws InterruptedException { 086 return delegate().takeFirst(); 087 } 088 089 @Override 090 public E takeLast() throws InterruptedException { 091 return delegate().takeLast(); 092 } 093 094 @Override 095 public @Nullable E pollFirst(long timeout, TimeUnit unit) throws InterruptedException { 096 return delegate().pollFirst(timeout, unit); 097 } 098 099 @Override 100 public @Nullable E pollLast(long timeout, TimeUnit unit) throws InterruptedException { 101 return delegate().pollLast(timeout, unit); 102 } 103 104 @Override 105 public void put(E e) throws InterruptedException { 106 delegate().put(e); 107 } 108 109 @Override 110 public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { 111 return delegate().offer(e, timeout, unit); 112 } 113 114 @Override 115 public E take() throws InterruptedException { 116 return delegate().take(); 117 } 118 119 @Override 120 public @Nullable E poll(long timeout, TimeUnit unit) throws InterruptedException { 121 return delegate().poll(timeout, unit); 122 } 123 124 @Override 125 public int drainTo(Collection<? super E> c) { 126 return delegate().drainTo(c); 127 } 128 129 @Override 130 public int drainTo(Collection<? super E> c, int maxElements) { 131 return delegate().drainTo(c, maxElements); 132 } 133}