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