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.Beta;
020
021import java.util.Deque;
022import java.util.Iterator;
023
024/**
025 * A deque which forwards all its method calls to another deque. Subclasses
026 * should override one or more methods to modify the behavior of the backing
027 * deque as desired per the <a
028 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
029 *
030 * <p><b>Warning:</b> The methods of {@code ForwardingDeque} forward
031 * <b>indiscriminately</b> to the methods of the delegate. For example,
032 * overriding {@link #add} alone <b>will not</b> change the behavior of {@link
033 * #offer} which can lead to unexpected behavior. In this case, you should
034 * override {@code offer} as well, either providing your own implementation, or
035 * delegating to the provided {@code standardOffer} method.
036 *
037 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even
038 * when all of the methods that they depend on are thread-safe.
039 *
040 * @author Kurt Alfred Kluever
041 * @since 12.0
042 */
043@Beta
044public abstract class ForwardingDeque<E> extends ForwardingQueue<E>
045    implements Deque<E> {
046
047  /** Constructor for use by subclasses. */
048  protected ForwardingDeque() {}
049
050  @Override protected abstract Deque<E> delegate();
051
052  @Override
053  public void addFirst(E e) {
054    delegate().addFirst(e);
055  }
056
057  @Override
058  public void addLast(E e) {
059    delegate().addLast(e);
060  }
061
062  @Override
063  public Iterator<E> descendingIterator() {
064    return delegate().descendingIterator();
065  }
066
067  @Override
068  public E getFirst() {
069    return delegate().getFirst();
070  }
071
072  @Override
073  public E getLast() {
074    return delegate().getLast();
075  }
076
077  @Override
078  public boolean offerFirst(E e) {
079    return delegate().offerFirst(e);
080  }
081
082  @Override
083  public boolean offerLast(E e) {
084    return delegate().offerLast(e);
085  }
086
087  @Override
088  public E peekFirst() {
089    return delegate().peekFirst();
090  }
091
092  @Override
093  public E peekLast() {
094    return delegate().peekLast();
095  }
096
097  @Override
098  public E pollFirst() {
099    return delegate().pollFirst();
100  }
101
102  @Override
103  public E pollLast() {
104    return delegate().pollLast();
105  }
106
107  @Override
108  public E pop() {
109    return delegate().pop();
110  }
111
112  @Override
113  public void push(E e) {
114    delegate().push(e);
115  }
116
117  @Override
118  public E removeFirst() {
119    return delegate().removeFirst();
120  }
121
122  @Override
123  public E removeLast() {
124    return delegate().removeLast();
125  }
126
127  @Override
128  public boolean removeFirstOccurrence(Object o) {
129    return delegate().removeFirstOccurrence(o);
130  }
131
132  @Override
133  public boolean removeLastOccurrence(Object o) {
134    return delegate().removeLastOccurrence(o);
135  }
136}