I
- the inner type, i.e. the type of the elements contained in the wrapped/inner collectionO
- the outer type, i.e. the type of elements appearing to be in this collectionpublic final class TransformingCollection<I,O> extends Object
Collection
which decorates another collection and transforms the element type from the inner type I
to an outer type O
.
See the package
documentation for general comments on transformation.
This implementation mitigates the type safety problems by using a token of the inner and the outer type to check
instances against them. This solves some of the critical situations but not all of them. In those other cases
ClassCastException
s might occur when an element can not be transformed by the transformation functions.
Null elements are allowed unless the inner collection does not accept them. These are handled explicitly and fixed to
the transformation null -> null
. The transforming functions specified during construction neither have to
handle that case nor are they allowed to produce null elements.
If the stream
returned by this collection is told to sort
itself, it will do so on the base of the comparator returned by the inner collection's spliterator (e.g. based on the
natural order of I
if it has one).
TransformingCollection
s are created with a TransformingCollectionBuilder
.
Modifier and Type | Method and Description |
---|---|
boolean |
add(O element) |
boolean |
addAll(Collection<? extends O> otherCollection) |
protected boolean |
callAddAllOnInner(Collection<? extends O> otherCollection)
Wraps the specified collection into a transformation and calls
addAll on
the innerCollection . |
protected boolean |
callAddOnThis(Collection<? extends O> otherCollection)
Iterates over the specified collection and calls
add(O) (on this collection) for each
element. |
protected boolean |
callContainsAllOnInner(Collection<?> otherCollection)
Wraps the specified collection into a transformation and calls
containsAll on the innerCollection . |
protected boolean |
callContainsOnThis(Collection<?> otherCollection)
Iterates over the specified collection and calls
contains(Object) (on this collection) for each element. |
protected boolean |
callRemoveAllOnInner(Collection<?> otherCollection)
Wraps the specified collection into a transformation and calls
removeAll
on the innerCollection . |
protected boolean |
callRemoveOnThis(Collection<?> otherCollection)
Iterates over the specified collection and calls
remove(Object) (on this collection) for each element. |
protected boolean |
callRetainAllOnInner(Collection<?> otherCollection)
Wraps the specified collection into a transformation and calls
retainAll
on the innerCollection . |
void |
clear() |
protected boolean |
clearToRemoveAll()
Calls
clear() to remove all instances from the innerCollection . |
boolean |
contains(Object object) |
boolean |
containsAll(Collection<?> otherCollection) |
boolean |
equals(Object object) |
protected Collection<I> |
getInnerCollection() |
int |
hashCode() |
boolean |
isEmpty() |
protected boolean |
isInnerElement(Object object)
Checks whether the specified object might be an inner element.
|
protected boolean |
isOuterElement(Object object)
Checks whether the specified object might be an outer element.
|
protected boolean |
isThisCollection(Collection<?> otherCollection)
Indicates whether the specified collection is equivalent to this one.
|
Iterator<O> |
iterator() |
boolean |
remove(Object object) |
boolean |
removeAll(Collection<?> otherCollection) |
boolean |
removeIf(Predicate<? super O> filter) |
boolean |
retainAll(Collection<?> otherCollection) |
protected boolean |
retainByCallingRemoveOnThis(Collection<?> otherCollection)
Iterates over this collection (i.e.
|
int |
size() |
Spliterator<O> |
spliterator() |
Object[] |
toArray() |
<T> T[] |
toArray(T[] inputArray) |
String |
toString() |
protected I |
transformToInner(O outerElement)
Transforms the specified element to an instance of the inner type.
|
protected O |
transformToOuter(I innerElement)
Transforms the specified element to an instance of the outer type.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
parallelStream, stream
protected Collection<I> getInnerCollection()
protected boolean isInnerElement(Object object)
This method does not have to be exact (which might be impossible due to involved generic types) and might produce false positives (but no false negatives).
object
- the object to check; may be nullprotected O transformToOuter(I innerElement)
It can not be guaranteed that the specified element is really of the inner type. If not, an exception can be thrown.
innerElement
- the element to transform; may be nullprotected boolean isOuterElement(Object object)
This method does not have to be exact (which might be impossible due to involved generic types) and might produce false positives (but no false negatives).
object
- the object to check; may be nullprotected I transformToInner(O outerElement)
It can not be guaranteed that the specified element is really of the outer type. If not, an exception can be thrown.
outerElement
- the element to transform; may be nullpublic boolean equals(Object object)
equals
in interface Collection<O>
public int hashCode()
hashCode
in interface Collection<O>
protected final boolean isThisCollection(Collection<?> otherCollection)
AbstractTransformingCollection
and wraps the same innerCollection
.otherCollection
- the Collection
which is compared with this onepublic int size()
size
in interface Collection<O>
public boolean isEmpty()
isEmpty
in interface Collection<O>
public boolean contains(Object object)
contains
in interface Collection<O>
public boolean containsAll(Collection<?> otherCollection)
containsAll
in interface Collection<O>
protected final boolean callContainsAllOnInner(Collection<?> otherCollection)
containsAll
on the innerCollection
.
Subclasses may chose to use this method if they override containsAll(Collection)
.
Accessing the wrapped collection will lead to ClassCastException
s when its elements are not of this
collection's outer type O
. Consider using callContainsOnThis(Collection)
.
otherCollection
- the parameter to containsAll
containsAll
protected final boolean callContainsOnThis(Collection<?> otherCollection)
contains(Object)
(on this collection) for each element.
Subclasses may chose to use this method if they override containsAll(Collection)
.
Manually iterating over the specified collection and calling this.
contains(Object)
individually
might break guarantees or optimizations made by the inner collection. Consider using
callContainsAllOnInner(Collection)
.
otherCollection
- the collection whose elements are passed to contains
contains
returns false; otherwise truepublic boolean add(O element)
add
in interface Collection<O>
public boolean addAll(Collection<? extends O> otherCollection)
addAll
in interface Collection<O>
protected final boolean callAddAllOnInner(Collection<? extends O> otherCollection)
addAll
on
the innerCollection
.
Subclasses may chose to use this method if they override addAll(Collection)
.
Accessing the wrapped collection will lead to ClassCastException
s when its elements are not of this
collection's outer type O
. Consider using callAddOnThis(Collection)
.
otherCollection
- the parameter to addAll
addAll
protected final boolean callAddOnThis(Collection<? extends O> otherCollection)
add(O)
(on this collection) for each
element.
Subclasses may chose to use this method if they override addAll(Collection)
.
Manually iterating over the specified collection and calling this.
add(Object)
individually might
break guarantees (e.g. regarding atomicity) or optimizations made by the inner collection. Consider using
callAddAllOnInner(Collection)
.
otherCollection
- the collection whose elements are passed to add
add
returns true; otherwise falsepublic boolean remove(Object object)
remove
in interface Collection<O>
public boolean removeIf(Predicate<? super O> filter)
removeIf
in interface Collection<O>
public boolean removeAll(Collection<?> otherCollection)
removeAll
in interface Collection<O>
protected final boolean clearToRemoveAll()
clear()
to remove all instances from the innerCollection
.protected final boolean callRemoveAllOnInner(Collection<?> otherCollection)
removeAll
on the innerCollection
.
Subclasses may chose to use this method if they override removeAll(Collection)
.
Accessing the wrapped collection will lead to ClassCastException
s when its elements are not of this
collection's outer type O
. Consider using callRemoveOnThis(Collection)
.
otherCollection
- the parameter to removeAll
removeAll
protected final boolean callRemoveOnThis(Collection<?> otherCollection)
remove(Object)
(on this collection) for each element.
Subclasses may chose to use this method if they override removeAll(Collection)
.
Manually iterating over the specified collection and calling this.
remove(Object)
individually
might break guarantees (e.g. regarding atomicity) or optimizations made by the inner collection. Consider using
callRemoveAllOnInner(Collection)
.
otherCollection
- the collection whose elements are passed to remove
remove
returns true; otherwise falsepublic boolean retainAll(Collection<?> otherCollection)
retainAll
in interface Collection<O>
protected final boolean callRetainAllOnInner(Collection<?> otherCollection)
retainAll
on the innerCollection
.
Subclasses may choose to use this method if they override retainAll(Collection)
.
Accessing the wrapped collection will lead to ClassCastException
s when its elements are not of this
collection's outer type O
. Consider using retainByCallingRemoveOnThis(Collection)
.
otherCollection
- the parameter to retainAll
retainAll
protected final boolean retainByCallingRemoveOnThis(Collection<?> otherCollection)
Subclasses may choose to use this method if they override retainAll(Collection)
.
Manually iterating over this collection and calling this.
remove(Object)
individually might break
guarantees (e.g. regarding atomicity) or optimizations made by the inner collection. Consider using
callRetainAllOnInner(Collection)
.
otherCollection
- the collection whose elements are not removed from this collectionpublic void clear()
clear
in interface Collection<O>
public Iterator<O> iterator()
iterator
in interface Iterable<O>
iterator
in interface Collection<O>
public Spliterator<O> spliterator()
spliterator
in interface Iterable<O>
spliterator
in interface Collection<O>
public Object[] toArray()
toArray
in interface Collection<O>
public <T> T[] toArray(T[] inputArray)
toArray
in interface Collection<O>
This documentation is licensed under CC-BY 4.0, attributed to Nicolai Parlog from CodeFX.