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.common.base.Objects;
021import com.google.errorprone.annotations.CanIgnoreReturnValue;
022import java.util.Collection;
023import java.util.Iterator;
024import javax.annotation.CheckForNull;
025import org.checkerframework.checker.nullness.qual.Nullable;
026
027/**
028 * A collection which forwards all its method calls to another collection. Subclasses should
029 * override one or more methods to modify the behavior of the backing collection as desired per the
030 * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
031 *
032 * <p><b>Warning:</b> The methods of {@code ForwardingCollection} forward <b>indiscriminately</b> to
033 * the methods of the delegate. For example, overriding {@link #add} alone <b>will not</b> change
034 * the behavior of {@link #addAll}, which can lead to unexpected behavior. In this case, you should
035 * override {@code addAll} as well, either providing your own implementation, or delegating to the
036 * provided {@code standardAddAll} method.
037 *
038 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
039 * default} methods. Instead, it inherits their default implementations. When those implementations
040 * invoke methods, they invoke methods on the {@code ForwardingCollection}.
041 *
042 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even when all of the
043 * methods that they depend on are thread-safe.
044 *
045 * @author Kevin Bourrillion
046 * @author Louis Wasserman
047 * @since 2.0
048 */
049@GwtCompatible
050public abstract class ForwardingCollection<E extends @Nullable Object> extends ForwardingObject
051    implements Collection<E> {
052  // TODO(lowasser): identify places where thread safety is actually lost
053
054  /** Constructor for use by subclasses. */
055  protected ForwardingCollection() {}
056
057  @Override
058  protected abstract Collection<E> delegate();
059
060  @Override
061  public Iterator<E> iterator() {
062    return delegate().iterator();
063  }
064
065  @Override
066  public int size() {
067    return delegate().size();
068  }
069
070  @CanIgnoreReturnValue
071  @Override
072  public boolean removeAll(Collection<?> collection) {
073    return delegate().removeAll(collection);
074  }
075
076  @Override
077  public boolean isEmpty() {
078    return delegate().isEmpty();
079  }
080
081  @Override
082  public boolean contains(@CheckForNull Object object) {
083    return delegate().contains(object);
084  }
085
086  @CanIgnoreReturnValue
087  @Override
088  public boolean add(@ParametricNullness E element) {
089    return delegate().add(element);
090  }
091
092  @CanIgnoreReturnValue
093  @Override
094  public boolean remove(@CheckForNull Object object) {
095    return delegate().remove(object);
096  }
097
098  @Override
099  public boolean containsAll(Collection<?> collection) {
100    return delegate().containsAll(collection);
101  }
102
103  @CanIgnoreReturnValue
104  @Override
105  public boolean addAll(Collection<? extends E> collection) {
106    return delegate().addAll(collection);
107  }
108
109  @CanIgnoreReturnValue
110  @Override
111  public boolean retainAll(Collection<?> collection) {
112    return delegate().retainAll(collection);
113  }
114
115  @Override
116  public void clear() {
117    delegate().clear();
118  }
119
120  @Override
121  public @Nullable Object[] toArray() {
122    return delegate().toArray();
123  }
124
125  @CanIgnoreReturnValue
126  @Override
127  @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
128  public <T extends @Nullable Object> T[] toArray(T[] array) {
129    return delegate().toArray(array);
130  }
131
132  /**
133   * A sensible definition of {@link #contains} in terms of {@link #iterator}. If you override
134   * {@link #iterator}, you may wish to override {@link #contains} to forward to this
135   * implementation.
136   *
137   * @since 7.0
138   */
139  protected boolean standardContains(@CheckForNull Object object) {
140    return Iterators.contains(iterator(), object);
141  }
142
143  /**
144   * A sensible definition of {@link #containsAll} in terms of {@link #contains} . If you override
145   * {@link #contains}, you may wish to override {@link #containsAll} to forward to this
146   * implementation.
147   *
148   * @since 7.0
149   */
150  protected boolean standardContainsAll(Collection<?> collection) {
151    return Collections2.containsAllImpl(this, collection);
152  }
153
154  /**
155   * A sensible definition of {@link #addAll} in terms of {@link #add}. If you override {@link
156   * #add}, you may wish to override {@link #addAll} to forward to this implementation.
157   *
158   * @since 7.0
159   */
160  protected boolean standardAddAll(Collection<? extends E> collection) {
161    return Iterators.addAll(this, collection.iterator());
162  }
163
164  /**
165   * A sensible definition of {@link #remove} in terms of {@link #iterator}, using the iterator's
166   * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link
167   * #remove} to forward to this implementation.
168   *
169   * @since 7.0
170   */
171  protected boolean standardRemove(@CheckForNull Object object) {
172    Iterator<E> iterator = iterator();
173    while (iterator.hasNext()) {
174      if (Objects.equal(iterator.next(), object)) {
175        iterator.remove();
176        return true;
177      }
178    }
179    return false;
180  }
181
182  /**
183   * A sensible definition of {@link #removeAll} in terms of {@link #iterator}, using the iterator's
184   * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link
185   * #removeAll} to forward to this implementation.
186   *
187   * @since 7.0
188   */
189  protected boolean standardRemoveAll(Collection<?> collection) {
190    return Iterators.removeAll(iterator(), collection);
191  }
192
193  /**
194   * A sensible definition of {@link #retainAll} in terms of {@link #iterator}, using the iterator's
195   * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link
196   * #retainAll} to forward to this implementation.
197   *
198   * @since 7.0
199   */
200  protected boolean standardRetainAll(Collection<?> collection) {
201    return Iterators.retainAll(iterator(), collection);
202  }
203
204  /**
205   * A sensible definition of {@link #clear} in terms of {@link #iterator}, using the iterator's
206   * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link
207   * #clear} to forward to this implementation.
208   *
209   * @since 7.0
210   */
211  protected void standardClear() {
212    Iterators.clear(iterator());
213  }
214
215  /**
216   * A sensible definition of {@link #isEmpty} as {@code !iterator().hasNext}. If you override
217   * {@link #isEmpty}, you may wish to override {@link #isEmpty} to forward to this implementation.
218   * Alternately, it may be more efficient to implement {@code isEmpty} as {@code size() == 0}.
219   *
220   * @since 7.0
221   */
222  protected boolean standardIsEmpty() {
223    return !iterator().hasNext();
224  }
225
226  /**
227   * A sensible definition of {@link #toString} in terms of {@link #iterator}. If you override
228   * {@link #iterator}, you may wish to override {@link #toString} to forward to this
229   * implementation.
230   *
231   * @since 7.0
232   */
233  protected String standardToString() {
234    return Collections2.toStringImpl(this);
235  }
236
237  /**
238   * A sensible definition of {@link #toArray()} in terms of {@link #toArray(Object[])}. If you
239   * override {@link #toArray(Object[])}, you may wish to override {@link #toArray} to forward to
240   * this implementation.
241   *
242   * @since 7.0
243   */
244  protected @Nullable Object[] standardToArray() {
245    @Nullable Object[] newArray = new @Nullable Object[size()];
246    return toArray(newArray);
247  }
248
249  /**
250   * A sensible definition of {@link #toArray(Object[])} in terms of {@link #size} and {@link
251   * #iterator}. If you override either of these methods, you may wish to override {@link #toArray}
252   * to forward to this implementation.
253   *
254   * @since 7.0
255   */
256  protected <T extends @Nullable Object> T[] standardToArray(T[] array) {
257    return ObjectArrays.toArrayImpl(this, array);
258  }
259}