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