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.Nullable;
025
026/**
027 * A collection which forwards all its method calls to another collection.
028 * Subclasses should override one or more methods to modify the behavior of the
029 * backing collection as desired per the <a
030 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
031 *
032 * <p><b>Warning:</b> The methods of {@code ForwardingCollection} forward
033 * <b>indiscriminately</b> to the methods of the delegate. For example,
034 * overriding {@link #add} alone <b>will not</b> change the behavior of {@link
035 * #addAll}, which can lead to unexpected behavior. In this case, you should
036 * override {@code addAll} as well, either providing your own implementation, or
037 * delegating to the provided {@code standardAddAll} method.
038 *
039 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
040 * default} methods. Instead, it inherits their default implementations. When those implementations
041 * invoke methods, they invoke methods on the {@code ForwardingCollection}.
042 *
043 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even
044 * when all of the methods that they depend on are thread-safe.
045 *
046 * @author Kevin Bourrillion
047 * @author Louis Wasserman
048 * @since 2.0
049 */
050@GwtCompatible
051public abstract class ForwardingCollection<E> extends ForwardingObject 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(Object object) {
083    return delegate().contains(object);
084  }
085
086  @CanIgnoreReturnValue
087  @Override
088  public boolean add(E element) {
089    return delegate().add(element);
090  }
091
092  @CanIgnoreReturnValue
093  @Override
094  public boolean remove(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 Object[] toArray() {
122    return delegate().toArray();
123  }
124
125  @CanIgnoreReturnValue
126  @Override
127  public <T> T[] toArray(T[] array) {
128    return delegate().toArray(array);
129  }
130
131  /**
132   * A sensible definition of {@link #contains} in terms of {@link #iterator}.
133   * If you override {@link #iterator}, you may wish to override {@link
134   * #contains} to forward to this implementation.
135   *
136   * @since 7.0
137   */
138  protected boolean standardContains(@Nullable Object object) {
139    return Iterators.contains(iterator(), object);
140  }
141
142  /**
143   * A sensible definition of {@link #containsAll} in terms of {@link #contains}
144   * . If you override {@link #contains}, you may wish to override {@link
145   * #containsAll} to forward to this implementation.
146   *
147   * @since 7.0
148   */
149  protected boolean standardContainsAll(Collection<?> collection) {
150    return Collections2.containsAllImpl(this, collection);
151  }
152
153  /**
154   * A sensible definition of {@link #addAll} in terms of {@link #add}. If you
155   * override {@link #add}, you may wish to override {@link #addAll} to forward
156   * 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},
166   * using the iterator's {@code remove} method. If you override {@link
167   * #iterator}, you may wish to override {@link #remove} to forward to this
168   * implementation.
169   *
170   * @since 7.0
171   */
172  protected boolean standardRemove(@Nullable Object object) {
173    Iterator<E> iterator = iterator();
174    while (iterator.hasNext()) {
175      if (Objects.equal(iterator.next(), object)) {
176        iterator.remove();
177        return true;
178      }
179    }
180    return false;
181  }
182
183  /**
184   * A sensible definition of {@link #removeAll} in terms of {@link #iterator},
185   * using the iterator's {@code remove} method. If you override {@link
186   * #iterator}, you may wish to override {@link #removeAll} to forward to this
187   * implementation.
188   *
189   * @since 7.0
190   */
191  protected boolean standardRemoveAll(Collection<?> collection) {
192    return Iterators.removeAll(iterator(), collection);
193  }
194
195  /**
196   * A sensible definition of {@link #retainAll} in terms of {@link #iterator},
197   * using the iterator's {@code remove} method. If you override {@link
198   * #iterator}, you may wish to override {@link #retainAll} to forward to this
199   * implementation.
200   *
201   * @since 7.0
202   */
203  protected boolean standardRetainAll(Collection<?> collection) {
204    return Iterators.retainAll(iterator(), collection);
205  }
206
207  /**
208   * A sensible definition of {@link #clear} in terms of {@link #iterator},
209   * using the iterator's {@code remove} method. If you override {@link
210   * #iterator}, you may wish to override {@link #clear} to forward to this
211   * implementation.
212   *
213   * @since 7.0
214   */
215  protected void standardClear() {
216    Iterators.clear(iterator());
217  }
218
219  /**
220   * A sensible definition of {@link #isEmpty} as {@code !iterator().hasNext}.
221   * If you override {@link #isEmpty}, you may wish to override {@link #isEmpty}
222   * to forward to this implementation. Alternately, it may be more efficient to
223   * implement {@code isEmpty} as {@code size() == 0}.
224   *
225   * @since 7.0
226   */
227  protected boolean standardIsEmpty() {
228    return !iterator().hasNext();
229  }
230
231  /**
232   * A sensible definition of {@link #toString} in terms of {@link #iterator}.
233   * If you override {@link #iterator}, you may wish to override {@link
234   * #toString} to forward to this implementation.
235   *
236   * @since 7.0
237   */
238  protected String standardToString() {
239    return Collections2.toStringImpl(this);
240  }
241
242  /**
243   * A sensible definition of {@link #toArray()} in terms of {@link
244   * #toArray(Object[])}. If you override {@link #toArray(Object[])}, you may
245   * wish to override {@link #toArray} to forward to this implementation.
246   *
247   * @since 7.0
248   */
249  protected Object[] standardToArray() {
250    Object[] newArray = new Object[size()];
251    return toArray(newArray);
252  }
253
254  /**
255   * A sensible definition of {@link #toArray(Object[])} in terms of {@link
256   * #size} and {@link #iterator}. If you override either of these methods, you
257   * may wish to override {@link #toArray} to forward to this implementation.
258   *
259   * @since 7.0
260   */
261  protected <T> T[] standardToArray(T[] array) {
262    return ObjectArrays.toArrayImpl(this, array);
263  }
264}