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
026 * should override one or more methods to modify the behavior of the backing
027 * 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
031 * <b>indiscriminately</b> to the methods of the delegate. For example,
032 * overriding {@link #add} alone <b>will not</b> change the behavior of {@link
033 * #offer} which can lead to unexpected behavior. In this case, you should
034 * override {@code offer} as well, either providing your own implementation, or
035 * delegating to the 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
042 * when all of the 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 ForwardingCollection<E> 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(E o) {
060    return delegate().offer(o);
061  }
062
063  @CanIgnoreReturnValue // TODO(cpovirk): Consider removing this?
064  @Override
065  public E poll() {
066    return delegate().poll();
067  }
068
069  @CanIgnoreReturnValue
070  @Override
071  public E remove() {
072    return delegate().remove();
073  }
074
075  @Override
076  public E peek() {
077    return delegate().peek();
078  }
079
080  @Override
081  public E element() {
082    return delegate().element();
083  }
084
085  /**
086   * A sensible definition of {@link #offer} in terms of {@link #add}. If you
087   * override {@link #add}, you may wish to override {@link #offer} to forward
088   * to this implementation.
089   *
090   * @since 7.0
091   */
092  protected boolean standardOffer(E e) {
093    try {
094      return add(e);
095    } catch (IllegalStateException caught) {
096      return false;
097    }
098  }
099
100  /**
101   * A sensible definition of {@link #peek} in terms of {@link #element}. If you
102   * override {@link #element}, you may wish to override {@link #peek} to
103   * forward to this implementation.
104   *
105   * @since 7.0
106   */
107  protected 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
117   * override {@link #remove}, you may wish to override {@link #poll} to forward
118   * to this implementation.
119   *
120   * @since 7.0
121   */
122  protected E standardPoll() {
123    try {
124      return remove();
125    } catch (NoSuchElementException caught) {
126      return null;
127    }
128  }
129}