001/* 002 * Copyright (C) 2007 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.GwtCompatible; 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import java.util.NoSuchElementException; 022import java.util.Queue; 023import javax.annotation.CheckForNull; 024import org.checkerframework.checker.nullness.qual.Nullable; 025 026/** 027 * A queue which forwards all its method calls to another queue. Subclasses should override one or 028 * more methods to modify the behavior of the backing queue 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 ForwardingQueue} 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, either providing your own implementation, or delegating to the 035 * provided {@code standardOffer} method. 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 ForwardingQueue}. 040 * 041 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even when all of the 042 * methods that they depend on are thread-safe. 043 * 044 * @author Mike Bostock 045 * @author Louis Wasserman 046 * @since 2.0 047 */ 048@GwtCompatible 049@ElementTypesAreNonnullByDefault 050public abstract class ForwardingQueue<E extends @Nullable Object> extends ForwardingCollection<E> 051 implements Queue<E> { 052 053 /** Constructor for use by subclasses. */ 054 protected ForwardingQueue() {} 055 056 @Override 057 protected abstract Queue<E> delegate(); 058 059 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 060 @Override 061 public boolean offer(@ParametricNullness E o) { 062 return delegate().offer(o); 063 } 064 065 @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this? 066 @Override 067 @CheckForNull 068 public E poll() { 069 return delegate().poll(); 070 } 071 072 @CanIgnoreReturnValue 073 @Override 074 @ParametricNullness 075 public E remove() { 076 return delegate().remove(); 077 } 078 079 @Override 080 @CheckForNull 081 public E peek() { 082 return delegate().peek(); 083 } 084 085 @Override 086 @ParametricNullness 087 public E element() { 088 return delegate().element(); 089 } 090 091 /** 092 * A sensible definition of {@link #offer} in terms of {@link #add}. If you override {@link #add}, 093 * you may wish to override {@link #offer} to forward to this implementation. 094 * 095 * @since 7.0 096 */ 097 protected boolean standardOffer(@ParametricNullness E e) { 098 try { 099 return add(e); 100 } catch (IllegalStateException caught) { 101 return false; 102 } 103 } 104 105 /** 106 * A sensible definition of {@link #peek} in terms of {@link #element}. If you override {@link 107 * #element}, you may wish to override {@link #peek} to forward to this implementation. 108 * 109 * @since 7.0 110 */ 111 @CheckForNull 112 protected E standardPeek() { 113 try { 114 return element(); 115 } catch (NoSuchElementException caught) { 116 return null; 117 } 118 } 119 120 /** 121 * A sensible definition of {@link #poll} in terms of {@link #remove}. If you override {@link 122 * #remove}, you may wish to override {@link #poll} to forward to this implementation. 123 * 124 * @since 7.0 125 */ 126 @CheckForNull 127 protected E standardPoll() { 128 try { 129 return remove(); 130 } catch (NoSuchElementException caught) { 131 return null; 132 } 133 } 134}