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; 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. 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 044public abstract class ForwardingDeque<E> extends ForwardingQueue<E> implements Deque<E> { 045 046 /** Constructor for use by subclasses. */ 047 protected ForwardingDeque() {} 048 049 @Override 050 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 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 078 @Override 079 public boolean offerFirst(E e) { 080 return delegate().offerFirst(e); 081 } 082 083 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 084 @Override 085 public boolean offerLast(E e) { 086 return delegate().offerLast(e); 087 } 088 089 @Override 090 public E peekFirst() { 091 return delegate().peekFirst(); 092 } 093 094 @Override 095 public E peekLast() { 096 return delegate().peekLast(); 097 } 098 099 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 100 @Override 101 public E pollFirst() { 102 return delegate().pollFirst(); 103 } 104 105 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 106 @Override 107 public E pollLast() { 108 return delegate().pollLast(); 109 } 110 111 @CanIgnoreReturnValue 112 @Override 113 public E pop() { 114 return delegate().pop(); 115 } 116 117 @Override 118 public void push(E e) { 119 delegate().push(e); 120 } 121 122 @CanIgnoreReturnValue 123 @Override 124 public E removeFirst() { 125 return delegate().removeFirst(); 126 } 127 128 @CanIgnoreReturnValue 129 @Override 130 public E removeLast() { 131 return delegate().removeLast(); 132 } 133 134 @CanIgnoreReturnValue 135 @Override 136 public boolean removeFirstOccurrence(Object o) { 137 return delegate().removeFirstOccurrence(o); 138 } 139 140 @CanIgnoreReturnValue 141 @Override 142 public boolean removeLastOccurrence(Object o) { 143 return delegate().removeLastOccurrence(o); 144 } 145}