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 046@ElementTypesAreNonnullByDefault 047public abstract class ForwardingDeque<E extends @Nullable Object> extends ForwardingQueue<E> 048 implements Deque<E> { 049 050 /** Constructor for use by subclasses. */ 051 protected ForwardingDeque() {} 052 053 @Override 054 protected abstract Deque<E> delegate(); 055 056 @Override 057 public void addFirst(@ParametricNullness E e) { 058 delegate().addFirst(e); 059 } 060 061 @Override 062 public void addLast(@ParametricNullness E e) { 063 delegate().addLast(e); 064 } 065 066 @Override 067 public Iterator<E> descendingIterator() { 068 return delegate().descendingIterator(); 069 } 070 071 @Override 072 @ParametricNullness 073 public E getFirst() { 074 return delegate().getFirst(); 075 } 076 077 @Override 078 @ParametricNullness 079 public E getLast() { 080 return delegate().getLast(); 081 } 082 083 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 084 @Override 085 public boolean offerFirst(@ParametricNullness E e) { 086 return delegate().offerFirst(e); 087 } 088 089 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 090 @Override 091 public boolean offerLast(@ParametricNullness E e) { 092 return delegate().offerLast(e); 093 } 094 095 @Override 096 @CheckForNull 097 public E peekFirst() { 098 return delegate().peekFirst(); 099 } 100 101 @Override 102 @CheckForNull 103 public E peekLast() { 104 return delegate().peekLast(); 105 } 106 107 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 108 @Override 109 @CheckForNull 110 public E pollFirst() { 111 return delegate().pollFirst(); 112 } 113 114 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 115 @Override 116 @CheckForNull 117 public E pollLast() { 118 return delegate().pollLast(); 119 } 120 121 @CanIgnoreReturnValue 122 @Override 123 @ParametricNullness 124 public E pop() { 125 return delegate().pop(); 126 } 127 128 @Override 129 public void push(@ParametricNullness E e) { 130 delegate().push(e); 131 } 132 133 @CanIgnoreReturnValue 134 @Override 135 @ParametricNullness 136 public E removeFirst() { 137 return delegate().removeFirst(); 138 } 139 140 @CanIgnoreReturnValue 141 @Override 142 @ParametricNullness 143 public E removeLast() { 144 return delegate().removeLast(); 145 } 146 147 @CanIgnoreReturnValue 148 @Override 149 public boolean removeFirstOccurrence(@CheckForNull Object o) { 150 return delegate().removeFirstOccurrence(o); 151 } 152 153 @CanIgnoreReturnValue 154 @Override 155 public boolean removeLastOccurrence(@CheckForNull Object o) { 156 return delegate().removeLastOccurrence(o); 157 } 158}