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.errorprone.annotations.CanIgnoreReturnValue; 021import java.util.Deque; 022import java.util.Iterator; 023import javax.annotation.CheckForNull; 024import org.checkerframework.checker.nullness.qual.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@GwtIncompatible 044@ElementTypesAreNonnullByDefault 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 @CheckForNull 095 public E peekFirst() { 096 return delegate().peekFirst(); 097 } 098 099 @Override 100 @CheckForNull 101 public E peekLast() { 102 return delegate().peekLast(); 103 } 104 105 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 106 @Override 107 @CheckForNull 108 public E pollFirst() { 109 return delegate().pollFirst(); 110 } 111 112 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 113 @Override 114 @CheckForNull 115 public E pollLast() { 116 return delegate().pollLast(); 117 } 118 119 @CanIgnoreReturnValue 120 @Override 121 @ParametricNullness 122 public E pop() { 123 return delegate().pop(); 124 } 125 126 @Override 127 public void push(@ParametricNullness E e) { 128 delegate().push(e); 129 } 130 131 @CanIgnoreReturnValue 132 @Override 133 @ParametricNullness 134 public E removeFirst() { 135 return delegate().removeFirst(); 136 } 137 138 @CanIgnoreReturnValue 139 @Override 140 @ParametricNullness 141 public E removeLast() { 142 return delegate().removeLast(); 143 } 144 145 @CanIgnoreReturnValue 146 @Override 147 public boolean removeFirstOccurrence(@CheckForNull Object o) { 148 return delegate().removeFirstOccurrence(o); 149 } 150 151 @CanIgnoreReturnValue 152 @Override 153 public boolean removeLastOccurrence(@CheckForNull Object o) { 154 return delegate().removeLastOccurrence(o); 155 } 156}