001 /*
002 * Copyright (C) 2007 Google Inc.
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
017 package com.google.common.collect;
018
019 import com.google.common.annotations.Beta;
020 import com.google.common.annotations.GwtCompatible;
021
022 import java.util.NoSuchElementException;
023 import java.util.Queue;
024
025 /**
026 * A queue which forwards all its method calls to another queue. Subclasses
027 * should override one or more methods to modify the behavior of the backing
028 * 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
032 * <b>indiscriminately</b> to the methods of the delegate. For example,
033 * overriding {@link #add} alone <b>will not</b> change the behavior of {@link
034 * #offer} which can lead to unexpected behavior. In this case, you should
035 * override {@code offer} as well, either providing your own implementation, or
036 * delegating to the provided {@code standardOffer} method.
037 *
038 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even
039 * when all of the methods that they depend on are thread-safe.
040 *
041 * @author Mike Bostock
042 * @author Louis Wasserman
043 * @since 2 (imported from Google Collections Library)
044 */
045 @GwtCompatible
046 public abstract class ForwardingQueue<E> extends ForwardingCollection<E>
047 implements Queue<E> {
048
049 /** Constructor for use by subclasses. */
050 protected ForwardingQueue() {}
051
052 @Override protected abstract Queue<E> delegate();
053
054 public boolean offer(E o) {
055 return delegate().offer(o);
056 }
057
058 public E poll() {
059 return delegate().poll();
060 }
061
062 public E remove() {
063 return delegate().remove();
064 }
065
066 public E peek() {
067 return delegate().peek();
068 }
069
070 public E element() {
071 return delegate().element();
072 }
073
074 /**
075 * A sensible definition of {@link #offer} in terms of {@link #add}. If you
076 * override {@link #add}, you may wish to override {@link #offer} to forward
077 * to this implementation.
078 *
079 * @since 7
080 */
081 @Beta protected boolean standardOffer(E e) {
082 try {
083 return add(e);
084 } catch (IllegalStateException caught) {
085 return false;
086 }
087 }
088
089 /**
090 * A sensible definition of {@link #peek} in terms of {@link #element}. If you
091 * override {@link #element}, you may wish to override {@link #peek} to
092 * forward to this implementation.
093 *
094 * @since 7
095 */
096 @Beta protected E standardPeek() {
097 try {
098 return element();
099 } catch (NoSuchElementException caught) {
100 return null;
101 }
102 }
103
104 /**
105 * A sensible definition of {@link #poll} in terms of {@link #remove}. If you
106 * override {@link #remove}, you may wish to override {@link #poll} to forward
107 * to this implementation.
108 *
109 * @since 7
110 */
111 @Beta protected E standardPoll() {
112 try {
113 return remove();
114 } catch (NoSuchElementException caught) {
115 return null;
116 }
117 }
118 }