E
- the type of the elements in the collectionpublic abstract class AbstractCollectionDecorator<E>
extends java.lang.Object
implements java.util.Collection<E>, java.io.Serializable
Collection
to provide additional behaviour.
Each method call made on this Collection
is forwarded to the
decorated Collection
. This class is used as a framework on which
to build to extensions such as synchronized and unmodifiable behaviour. The
main advantage of decoration is that one decorator can wrap any implementation
of Collection
, whereas sub-classing requires a new class to be
written for each implementation.
This implementation does not perform any special processing with
iterator()
. Instead it simply returns the value from the
wrapped collection. This may be undesirable, for example if you are trying
to write an unmodifiable implementation it might provide a loophole.
This implementation does not forward the hashCode and equals methods through
to the backing object, but relies on Object's implementation. This is necessary
to preserve the symmetry of equals. Custom definitions of equality are usually
based on an interface, such as Set or List, so that the implementation of equals
can cast the object being tested for equality to the custom interface.
AbstractCollectionDecorator does not implement such custom interfaces directly;
they are implemented only in subclasses. Therefore, forwarding equals would break
symmetry, as the forwarding object might consider itself equal to the object being
tested, but the reverse could not be true. This behavior is consistent with the
JDK's collection wrappers, such as Collections.unmodifiableCollection(Collection)
.
Use an interface-specific subclass of AbstractCollectionDecorator, such as
AbstractListDecorator, to preserve equality behavior, or override equals directly.
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object) |
boolean |
addAll(java.util.Collection<? extends E> coll) |
void |
clear() |
boolean |
contains(java.lang.Object object) |
boolean |
containsAll(java.util.Collection<?> coll) |
boolean |
isEmpty() |
java.util.Iterator<E> |
iterator() |
boolean |
remove(java.lang.Object object) |
boolean |
removeAll(java.util.Collection<?> coll) |
boolean |
removeIf(java.util.function.Predicate<? super E> filter) |
boolean |
retainAll(java.util.Collection<?> coll) |
int |
size() |
java.lang.Object[] |
toArray() |
<T> T[] |
toArray(T[] object) |
java.lang.String |
toString() |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
public boolean addAll(java.util.Collection<? extends E> coll)
addAll
in interface java.util.Collection<E>
public void clear()
clear
in interface java.util.Collection<E>
public boolean contains(java.lang.Object object)
contains
in interface java.util.Collection<E>
public boolean isEmpty()
isEmpty
in interface java.util.Collection<E>
public java.util.Iterator<E> iterator()
public boolean remove(java.lang.Object object)
remove
in interface java.util.Collection<E>
public int size()
size
in interface java.util.Collection<E>
public java.lang.Object[] toArray()
toArray
in interface java.util.Collection<E>
public <T> T[] toArray(T[] object)
toArray
in interface java.util.Collection<E>
public boolean containsAll(java.util.Collection<?> coll)
containsAll
in interface java.util.Collection<E>
public boolean removeIf(java.util.function.Predicate<? super E> filter)
removeIf
in interface java.util.Collection<E>
public boolean removeAll(java.util.Collection<?> coll)
removeAll
in interface java.util.Collection<E>
public boolean retainAll(java.util.Collection<?> coll)
retainAll
in interface java.util.Collection<E>
public java.lang.String toString()
toString
in class java.lang.Object
Copyright © 2010 - 2023 Adobe. All Rights Reserved