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 com.google.common.annotations.J2ktIncompatible;
021import com.google.errorprone.annotations.CanIgnoreReturnValue;
022import java.util.Deque;
023import java.util.Iterator;
024import javax.annotation.CheckForNull;
025import org.checkerframework.checker.nullness.qual.Nullable;
026
027/**
028 * A deque which forwards all its method calls to another deque. Subclasses should override one or
029 * more methods to modify the behavior of the backing deque as desired per the <a
030 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
031 *
032 * <p><b>Warning:</b> The methods of {@code ForwardingDeque} forward <b>indiscriminately</b> to the
033 * methods of the delegate. For example, overriding {@link #add} alone <b>will not</b> change the
034 * behavior of {@link #offer} which can lead to unexpected behavior. In this case, you should
035 * override {@code offer} as well.
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 ForwardingDeque}.
040 *
041 * @author Kurt Alfred Kluever
042 * @since 12.0
043 */
044@J2ktIncompatible
045@GwtIncompatible
046public abstract class ForwardingDeque<E extends @Nullable Object> extends ForwardingQueue<E>
047    implements Deque<E> {
048
049  /** Constructor for use by subclasses. */
050  protected ForwardingDeque() {}
051
052  @Override
053  protected abstract Deque<E> delegate();
054
055  @Override
056  public void addFirst(@ParametricNullness E e) {
057    delegate().addFirst(e);
058  }
059
060  @Override
061  public void addLast(@ParametricNullness E e) {
062    delegate().addLast(e);
063  }
064
065  @Override
066  public Iterator<E> descendingIterator() {
067    return delegate().descendingIterator();
068  }
069
070  @Override
071  @ParametricNullness
072  public E getFirst() {
073    return delegate().getFirst();
074  }
075
076  @Override
077  @ParametricNullness
078  public E getLast() {
079    return delegate().getLast();
080  }
081
082  @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
083  @Override
084  public boolean offerFirst(@ParametricNullness E e) {
085    return delegate().offerFirst(e);
086  }
087
088  @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
089  @Override
090  public boolean offerLast(@ParametricNullness E e) {
091    return delegate().offerLast(e);
092  }
093
094  @Override
095  @CheckForNull
096  public E peekFirst() {
097    return delegate().peekFirst();
098  }
099
100  @Override
101  @CheckForNull
102  public E peekLast() {
103    return delegate().peekLast();
104  }
105
106  @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
107  @Override
108  @CheckForNull
109  public E pollFirst() {
110    return delegate().pollFirst();
111  }
112
113  @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
114  @Override
115  @CheckForNull
116  public E pollLast() {
117    return delegate().pollLast();
118  }
119
120  @CanIgnoreReturnValue
121  @Override
122  @ParametricNullness
123  public E pop() {
124    return delegate().pop();
125  }
126
127  @Override
128  public void push(@ParametricNullness E e) {
129    delegate().push(e);
130  }
131
132  @CanIgnoreReturnValue
133  @Override
134  @ParametricNullness
135  public E removeFirst() {
136    return delegate().removeFirst();
137  }
138
139  @CanIgnoreReturnValue
140  @Override
141  @ParametricNullness
142  public E removeLast() {
143    return delegate().removeLast();
144  }
145
146  @CanIgnoreReturnValue
147  @Override
148  public boolean removeFirstOccurrence(@CheckForNull Object o) {
149    return delegate().removeFirstOccurrence(o);
150  }
151
152  @CanIgnoreReturnValue
153  @Override
154  public boolean removeLastOccurrence(@CheckForNull Object o) {
155    return delegate().removeLastOccurrence(o);
156  }
157}