Class ForwardingCollection<E>
java.lang.Object
com.google.common.collect.ForwardingObject
com.google.common.collect.ForwardingCollection<E>
- All Implemented Interfaces:
Iterable<E>, Collection<E>
- Direct Known Subclasses:
ForwardingList, ForwardingMultiset, ForwardingQueue, ForwardingSet
@GwtCompatible
public abstract class ForwardingCollection<E>
extends ForwardingObject
implements Collection<E>
A collection which forwards all its method calls to another collection.
Subclasses should override one or more methods to modify the behavior of the
backing collection as desired per the decorator pattern.
Warning: The methods of ForwardingCollection forward
indiscriminately to the methods of the delegate. For example,
overriding add(E) alone will not change the behavior of addAll(Collection), which can lead to unexpected behavior. In this case, you should
override addAll as well, either providing your own implementation, or
delegating to the provided standardAddAll method.
default method warning: This class does not forward calls to
default methods. Instead, it inherits their default implementations. When those implementations
invoke methods, they invoke methods on the ForwardingCollection.
The standard methods are not guaranteed to be thread-safe, even
when all of the methods that they depend on are thread-safe.
- Since:
- 2.0
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor for use by subclasses. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> collection) voidclear()booleanbooleancontainsAll(Collection<?> collection) protected abstract Collection<E> delegate()Returns the backing delegate instance that methods are forwarded to.booleanisEmpty()iterator()booleanbooleanremoveAll(Collection<?> collection) booleanretainAll(Collection<?> collection) intsize()protected booleanstandardAddAll(Collection<? extends E> collection) A sensible definition ofaddAll(Collection)in terms ofadd(E).protected voidprotected booleanstandardContains(Object object) A sensible definition ofcontains(Object)in terms ofiterator().protected booleanstandardContainsAll(Collection<?> collection) A sensible definition ofcontainsAll(Collection)in terms ofcontains(Object).protected booleanA sensible definition ofisEmpty()as!iterator().hasNext.protected booleanstandardRemove(Object object) protected booleanstandardRemoveAll(Collection<?> collection) A sensible definition ofremoveAll(Collection)in terms ofiterator(), using the iterator'sremovemethod.protected booleanstandardRetainAll(Collection<?> collection) A sensible definition ofretainAll(Collection)in terms ofiterator(), using the iterator'sremovemethod.protected Object[]A sensible definition oftoArray()in terms oftoArray(Object[]).protected <T> T[]standardToArray(T[] array) protected StringA sensible definition ofForwardingObject.toString()in terms ofiterator().Object[]toArray()<T> T[]toArray(T[] array) Methods inherited from class ForwardingObject
toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
equals, hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Constructor Details
-
ForwardingCollection
protected ForwardingCollection()Constructor for use by subclasses.
-
-
Method Details
-
delegate
Description copied from class:ForwardingObjectReturns the backing delegate instance that methods are forwarded to. Abstract subclasses generally override this method with an abstract method that has a more specific return type, such asForwardingSet.delegate(). Concrete subclasses override this method to supply the instance being decorated.- Specified by:
delegatein classForwardingObject
-
iterator
-
size
public int size()- Specified by:
sizein interfaceCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>
-
add
- Specified by:
addin interfaceCollection<E>
-
remove
- Specified by:
removein interfaceCollection<E>
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>
-
addAll
- Specified by:
addAllin interfaceCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>
-
toArray
- Specified by:
toArrayin interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] array) - Specified by:
toArrayin interfaceCollection<E>
-
standardContains
A sensible definition ofcontains(Object)in terms ofiterator(). If you overrideiterator(), you may wish to overridecontains(Object)to forward to this implementation.- Since:
- 7.0
-
standardContainsAll
A sensible definition ofcontainsAll(Collection)in terms ofcontains(Object). If you overridecontains(Object), you may wish to overridecontainsAll(Collection)to forward to this implementation.- Since:
- 7.0
-
standardAddAll
A sensible definition ofaddAll(Collection)in terms ofadd(E). If you overrideadd(E), you may wish to overrideaddAll(Collection)to forward to this implementation.- Since:
- 7.0
-
standardRemove
A sensible definition ofremove(Object)in terms ofiterator(), using the iterator'sremovemethod. If you overrideiterator(), you may wish to overrideremove(Object)to forward to this implementation.- Since:
- 7.0
-
standardRemoveAll
A sensible definition ofremoveAll(Collection)in terms ofiterator(), using the iterator'sremovemethod. If you overrideiterator(), you may wish to overrideremoveAll(Collection)to forward to this implementation.- Since:
- 7.0
-
standardRetainAll
A sensible definition ofretainAll(Collection)in terms ofiterator(), using the iterator'sremovemethod. If you overrideiterator(), you may wish to overrideretainAll(Collection)to forward to this implementation.- Since:
- 7.0
-
standardClear
protected void standardClear()A sensible definition ofclear()in terms ofiterator(), using the iterator'sremovemethod. If you overrideiterator(), you may wish to overrideclear()to forward to this implementation.- Since:
- 7.0
-
standardIsEmpty
-
standardToString
A sensible definition ofForwardingObject.toString()in terms ofiterator(). If you overrideiterator(), you may wish to overrideForwardingObject.toString()to forward to this implementation.- Since:
- 7.0
-
standardToArray
A sensible definition oftoArray()in terms oftoArray(Object[]). If you overridetoArray(Object[]), you may wish to overridetoArray()to forward to this implementation.- Since:
- 7.0
-
standardToArray
protected <T> T[] standardToArray(T[] array) A sensible definition oftoArray(Object[])in terms ofsize()anditerator(). If you override either of these methods, you may wish to overridetoArray()to forward to this implementation.- Since:
- 7.0
-