E
- the type of elements contained in the Optional
spublic final class OptionalTransformingSet<E> extends Object
Set
which is a flattened view on a set of Optional
s, i.e. it only presents the
contained values.
See the package
documentation for general comments on transformation.
The inner set must not contain null elements. The empty Optional
is mapped to an outer element specified
during construction. If null is chosen for this, this set will accept null elements. Otherwise it will reject them
with a NullPointerException
.
The transformations used by this set preserve object identity of outer elements with the exception of the default
element. This means if (non-default) elements are added to this set, an iteration over it will return the same
instances. The default value instance will be used to represent the empty Optional
, so when elements equal to
it are added, they will be retrieved as that instance (thus loosing their identity).
This implementation mitigates the type safety problems by using type tokens. Optional.class
is used as the
inner type token. The outer type token can be specified during construction. This solves some of the critical
situations but not all of them. In those other cases (e.g. if containsAll
is called
with a Collection<Optional<?>>
) ClassCastException
s might occur.
All method calls (of abstract and default methods existing in JDK 8) are forwarded to the same method on the wrapped set. This implies that all guarantees made by such methods (e.g. regarding atomicity) are upheld by the transformation.
Constructor and Description |
---|
OptionalTransformingSet(Set<Optional<E>> innerSet)
Creates a new transforming set.
|
OptionalTransformingSet(Set<Optional<E>> innerSet,
Class<? super E> outerTypeToken)
Creates a new transforming set which uses a type token to identify the outer elements.
|
OptionalTransformingSet(Set<Optional<E>> innerSet,
Class<? super E> outerTypeToken,
E outerDefaultElement)
Creates a new transforming set which uses a type token to identify the outer elements.
|
OptionalTransformingSet(Set<Optional<E>> innerSet,
E outerDefaultElement)
Creates a new transforming set which uses the type of the specified default element as a token to identify the
outer elements.
|
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() |
protected Set<Optional<E>> |
getInnerSet() |
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 Optional<E> |
transformToInner(E outerElement)
Transforms the specified element to an instance of the inner type.
|
protected E |
transformToOuter(Optional<E> innerElement)
Transforms the specified element to an instance of the outer type.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, spliterator, toArray, toArray
parallelStream, removeIf, stream
public OptionalTransformingSet(Set<Optional<E>> innerSet, Class<? super E> outerTypeToken, E outerDefaultElement)
innerSet
- the wrapped setouterTypeToken
- the token for the outer typeouterDefaultElement
- the element used to represent Optional.empty()
; can be null; it is of crucial importance that
this element does not occur inside a non-empty optional because then the transformations from that
optional to an element and back are not inverse, which will cause unexpected behaviorpublic OptionalTransformingSet(Set<Optional<E>> innerSet)
innerSet
- the wrapped setpublic OptionalTransformingSet(Set<Optional<E>> innerSet, Class<? super E> outerTypeToken)
innerSet
- the wrapped setouterTypeToken
- the token for the outer typepublic OptionalTransformingSet(Set<Optional<E>> innerSet, E outerDefaultElement)
innerSet
- the wrapped setouterDefaultElement
- the non-null element used to represent Optional.empty()
; it is of crucial importance that this
element does not occur inside an optional because then the transformations from that optional to an
element and back are not inverse, which will cause unexpected behavior; the instance's class will be
used as the outer type tokenprotected Set<Optional<E>> getInnerSet()
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 E transformToOuter(Optional<E> 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 Optional<E> transformToInner(E 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 nullprotected final Collection<I> getInnerCollection()
public final boolean equals(Object object)
equals
in interface Collection<O>
equals
in interface Set<O>
public final int hashCode()
hashCode
in interface Collection<O>
hashCode
in interface Set<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.