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