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