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