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
049public abstract class ForwardingQueue<E extends @Nullable Object> extends ForwardingCollection<E>
050    implements Queue<E> {
051
052  /** Constructor for use by subclasses. */
053  protected ForwardingQueue() {}
054
055  @Override
056  protected abstract Queue<E> delegate();
057
058  @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
059  @Override
060  public boolean offer(@ParametricNullness E o) {
061    return delegate().offer(o);
062  }
063
064  @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
065  @Override
066  @CheckForNull
067  public E poll() {
068    return delegate().poll();
069  }
070
071  @CanIgnoreReturnValue
072  @Override
073  @ParametricNullness
074  public E remove() {
075    return delegate().remove();
076  }
077
078  @Override
079  @CheckForNull
080  public E peek() {
081    return delegate().peek();
082  }
083
084  @Override
085  @ParametricNullness
086  public E element() {
087    return delegate().element();
088  }
089
090  /**
091   * A sensible definition of {@link #offer} in terms of {@link #add}. If you override {@link #add},
092   * you may wish to override {@link #offer} to forward to this implementation.
093   *
094   * @since 7.0
095   */
096  protected boolean standardOffer(@ParametricNullness E e) {
097    try {
098      return add(e);
099    } catch (IllegalStateException caught) {
100      return false;
101    }
102  }
103
104  /**
105   * A sensible definition of {@link #peek} in terms of {@link #element}. If you override {@link
106   * #element}, you may wish to override {@link #peek} to forward to this implementation.
107   *
108   * @since 7.0
109   */
110  @CheckForNull
111  protected E standardPeek() {
112    try {
113      return element();
114    } catch (NoSuchElementException caught) {
115      return null;
116    }
117  }
118
119  /**
120   * A sensible definition of {@link #poll} in terms of {@link #remove}. If you override {@link
121   * #remove}, you may wish to override {@link #poll} to forward to this implementation.
122   *
123   * @since 7.0
124   */
125  @CheckForNull
126  protected E standardPoll() {
127    try {
128      return remove();
129    } catch (NoSuchElementException caught) {
130      return null;
131    }
132  }
133}