• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.base.Objects;
21 import com.google.errorprone.annotations.CanIgnoreReturnValue;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import javax.annotation.CheckForNull;
25 import org.checkerframework.checker.nullness.qual.Nullable;
26 
27 /**
28  * A collection which forwards all its method calls to another collection. Subclasses should
29  * override one or more methods to modify the behavior of the backing collection as desired per the
30  * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
31  *
32  * <p><b>Warning:</b> The methods of {@code ForwardingCollection} forward <b>indiscriminately</b> to
33  * the methods of the delegate. For example, overriding {@link #add} alone <b>will not</b> change
34  * the behavior of {@link #addAll}, which can lead to unexpected behavior. In this case, you should
35  * override {@code addAll} as well, either providing your own implementation, or delegating to the
36  * provided {@code standardAddAll} method.
37  *
38  * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
39  * default} methods. Instead, it inherits their default implementations. When those implementations
40  * invoke methods, they invoke methods on the {@code ForwardingCollection}.
41  *
42  * <p>The {@code standard} methods are not guaranteed to be thread-safe, even when all of the
43  * methods that they depend on are thread-safe.
44  *
45  * @author Kevin Bourrillion
46  * @author Louis Wasserman
47  * @since 2.0
48  */
49 @GwtCompatible
50 @ElementTypesAreNonnullByDefault
51 public abstract class ForwardingCollection<E extends @Nullable Object> extends ForwardingObject
52     implements Collection<E> {
53   // TODO(lowasser): identify places where thread safety is actually lost
54 
55   /** Constructor for use by subclasses. */
ForwardingCollection()56   protected ForwardingCollection() {}
57 
58   @Override
delegate()59   protected abstract Collection<E> delegate();
60 
61   @Override
iterator()62   public Iterator<E> iterator() {
63     return delegate().iterator();
64   }
65 
66   @Override
size()67   public int size() {
68     return delegate().size();
69   }
70 
71   @CanIgnoreReturnValue
72   @Override
removeAll(Collection<?> collection)73   public boolean removeAll(Collection<?> collection) {
74     return delegate().removeAll(collection);
75   }
76 
77   @Override
isEmpty()78   public boolean isEmpty() {
79     return delegate().isEmpty();
80   }
81 
82   @Override
contains(@heckForNull Object object)83   public boolean contains(@CheckForNull Object object) {
84     return delegate().contains(object);
85   }
86 
87   @CanIgnoreReturnValue
88   @Override
add(@arametricNullness E element)89   public boolean add(@ParametricNullness E element) {
90     return delegate().add(element);
91   }
92 
93   @CanIgnoreReturnValue
94   @Override
remove(@heckForNull Object object)95   public boolean remove(@CheckForNull Object object) {
96     return delegate().remove(object);
97   }
98 
99   @Override
containsAll(Collection<?> collection)100   public boolean containsAll(Collection<?> collection) {
101     return delegate().containsAll(collection);
102   }
103 
104   @CanIgnoreReturnValue
105   @Override
addAll(Collection<? extends E> collection)106   public boolean addAll(Collection<? extends E> collection) {
107     return delegate().addAll(collection);
108   }
109 
110   @CanIgnoreReturnValue
111   @Override
retainAll(Collection<?> collection)112   public boolean retainAll(Collection<?> collection) {
113     return delegate().retainAll(collection);
114   }
115 
116   @Override
clear()117   public void clear() {
118     delegate().clear();
119   }
120 
121   @Override
toArray()122   public @Nullable Object[] toArray() {
123     return delegate().toArray();
124   }
125 
126   @CanIgnoreReturnValue
127   @Override
128   @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations
toArray(T[] array)129   public <T extends @Nullable Object> T[] toArray(T[] array) {
130     return delegate().toArray(array);
131   }
132 
133   /**
134    * A sensible definition of {@link #contains} in terms of {@link #iterator}. If you override
135    * {@link #iterator}, you may wish to override {@link #contains} to forward to this
136    * implementation.
137    *
138    * @since 7.0
139    */
standardContains(@heckForNull Object object)140   protected boolean standardContains(@CheckForNull Object object) {
141     return Iterators.contains(iterator(), object);
142   }
143 
144   /**
145    * A sensible definition of {@link #containsAll} in terms of {@link #contains} . If you override
146    * {@link #contains}, you may wish to override {@link #containsAll} to forward to this
147    * implementation.
148    *
149    * @since 7.0
150    */
standardContainsAll(Collection<?> collection)151   protected boolean standardContainsAll(Collection<?> collection) {
152     return Collections2.containsAllImpl(this, collection);
153   }
154 
155   /**
156    * A sensible definition of {@link #addAll} in terms of {@link #add}. If you override {@link
157    * #add}, you may wish to override {@link #addAll} to forward to this implementation.
158    *
159    * @since 7.0
160    */
standardAddAll(Collection<? extends E> collection)161   protected boolean standardAddAll(Collection<? extends E> collection) {
162     return Iterators.addAll(this, collection.iterator());
163   }
164 
165   /**
166    * A sensible definition of {@link #remove} in terms of {@link #iterator}, using the iterator's
167    * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link
168    * #remove} to forward to this implementation.
169    *
170    * @since 7.0
171    */
standardRemove(@heckForNull Object object)172   protected boolean standardRemove(@CheckForNull 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}, using the iterator's
185    * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link
186    * #removeAll} to forward to this implementation.
187    *
188    * @since 7.0
189    */
standardRemoveAll(Collection<?> collection)190   protected boolean standardRemoveAll(Collection<?> collection) {
191     return Iterators.removeAll(iterator(), collection);
192   }
193 
194   /**
195    * A sensible definition of {@link #retainAll} in terms of {@link #iterator}, using the iterator's
196    * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link
197    * #retainAll} to forward to this implementation.
198    *
199    * @since 7.0
200    */
standardRetainAll(Collection<?> collection)201   protected boolean standardRetainAll(Collection<?> collection) {
202     return Iterators.retainAll(iterator(), collection);
203   }
204 
205   /**
206    * A sensible definition of {@link #clear} in terms of {@link #iterator}, using the iterator's
207    * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link
208    * #clear} to forward to this implementation.
209    *
210    * @since 7.0
211    */
standardClear()212   protected void standardClear() {
213     Iterators.clear(iterator());
214   }
215 
216   /**
217    * A sensible definition of {@link #isEmpty} as {@code !iterator().hasNext}. If you override
218    * {@link #isEmpty}, you may wish to override {@link #isEmpty} to forward to this implementation.
219    * Alternately, it may be more efficient to implement {@code isEmpty} as {@code size() == 0}.
220    *
221    * @since 7.0
222    */
standardIsEmpty()223   protected boolean standardIsEmpty() {
224     return !iterator().hasNext();
225   }
226 
227   /**
228    * A sensible definition of {@link #toString} in terms of {@link #iterator}. If you override
229    * {@link #iterator}, you may wish to override {@link #toString} to forward to this
230    * implementation.
231    *
232    * @since 7.0
233    */
standardToString()234   protected String standardToString() {
235     return Collections2.toStringImpl(this);
236   }
237 
238   /**
239    * A sensible definition of {@link #toArray()} in terms of {@link #toArray(Object[])}. If you
240    * override {@link #toArray(Object[])}, you may wish to override {@link #toArray} to forward to
241    * this implementation.
242    *
243    * @since 7.0
244    */
standardToArray()245   protected @Nullable Object[] standardToArray() {
246     @Nullable Object[] newArray = new @Nullable Object[size()];
247     return toArray(newArray);
248   }
249 
250   /**
251    * A sensible definition of {@link #toArray(Object[])} in terms of {@link #size} and {@link
252    * #iterator}. If you override either of these methods, you may wish to override {@link #toArray}
253    * to forward to this implementation.
254    *
255    * @since 7.0
256    */
standardToArray(T[] array)257   protected <T extends @Nullable Object> T[] standardToArray(T[] array) {
258     return ObjectArrays.toArrayImpl(this, array);
259   }
260 }
261