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