E
- the type of elements contained in the Optional
spublic final class OptionalTransformingList<E> extends Object
List
which is a flattened view on a list of Optional
s, i.e. it only presents the
contained values.
See the package
documentation for general comments on transformation.
The inner list must not contain null elements. The empty Optional
is mapped to an outer element specified
during construction. If null is chosen for this, this list will accept null elements. Otherwise it will reject them
with a NullPointerException
.
The transformations used by this list preserve object identity of outer elements with the exception of the default
element. This means if (non-default) elements are added to this list, 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 |
---|
OptionalTransformingList(List<Optional<E>> innerList)
Creates a new transforming list.
|
OptionalTransformingList(List<Optional<E>> innerList,
Class<? super E> outerTypeToken)
Creates a new transforming list which uses a type token to identify the outer elements.
|
OptionalTransformingList(List<Optional<E>> innerList,
Class<? super E> outerTypeToken,
E outerDefaultElement)
Creates a new transforming list which uses a type token to identify the outer elements.
|
OptionalTransformingList(List<Optional<E>> innerList,
E outerDefaultElement)
Creates a new transforming list which uses the type of the specified default element as a token to identify the
outer elements.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
O element) |
boolean |
add(O element) |
boolean |
addAll(Collection<? extends O> otherCollection) |
boolean |
addAll(int index,
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 |
callAddAllOnInner(int startIndex,
Collection<? extends O> otherCollection)
|
protected boolean |
callAddOnThis(Collection<? extends O> otherCollection)
Iterates over the specified collection and calls
add(O) (on this collection) for each
element. |
protected boolean |
callAddOnThis(int startIndex,
Collection<? extends O> otherCollection)
Iterates over the specified collection and calls
add() (on this list) 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) |
O |
get(int index) |
protected Collection<I> |
getInnerCollection() |
protected List<Optional<E>> |
getInnerList() |
int |
hashCode() |
int |
indexOf(Object object) |
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() |
int |
lastIndexOf(Object object) |
ListIterator<O> |
listIterator() |
ListIterator<O> |
listIterator(int startIndex) |
O |
remove(int index) |
boolean |
remove(Object object) |
boolean |
removeAll(Collection<?> otherCollection) |
boolean |
removeIf(Predicate<? super O> filter) |
void |
replaceAll(UnaryOperator<O> operator) |
boolean |
retainAll(Collection<?> otherCollection) |
protected boolean |
retainByCallingRemoveOnThis(Collection<?> otherCollection)
Iterates over this collection (i.e.
|
O |
set(int index,
O element) |
int |
size() |
void |
sort(Comparator<? super O> comparator) |
Spliterator<O> |
spliterator() |
List<O> |
subList(int fromIndex,
int toIndex) |
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 OptionalTransformingList(List<Optional<E>> innerList, Class<? super E> outerTypeToken, E outerDefaultElement)
innerList
- the wrapped listouterTypeToken
- 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 OptionalTransformingList(List<Optional<E>> innerList)
innerList
- the wrapped listpublic OptionalTransformingList(List<Optional<E>> innerList, Class<? super E> outerTypeToken)
innerList
- the wrapped setouterTypeToken
- the token for the outer typepublic OptionalTransformingList(List<Optional<E>> innerList, E outerDefaultElement)
innerList
- the wrapped listouterDefaultElement
- 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 List<Optional<E>> getInnerList()
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 int lastIndexOf(Object object)
lastIndexOf
in interface List<O>
public boolean addAll(int index, Collection<? extends O> otherCollection)
protected final boolean callAddAllOnInner(int startIndex, Collection<? extends O> otherCollection)
addAll
on the
innerList
.
Subclasses may choose to use this method if they override addAll(int, Collection)
.
Accessing the wrapped collection will lead to ClassCastException
s when its elements are not of this
list's outer type O
. Consider using callAddOnThis(int, Collection)
.
startIndex
- index at which to insert the first element from the specified collectionotherCollection
- the parameter to addAll
addAll
protected final boolean callAddOnThis(int startIndex, Collection<? extends O> otherCollection)
add()
(on this list) for each element.
Subclasses may choose to use this method if they override addAll(int, Collection)
.
Manually iterating over the specified collection and calling this.
add(int, Object)
individually
might break guarantees (e.g. regarding atomicity) or optimizations made by the inner collection. Consider using
callAddAllOnInner(int, Collection)
.
startIndex
- index at which to insert the first element from the specified collectionotherCollection
- the collection whose elements are passed to add
add
returns true; otherwise falsepublic void replaceAll(UnaryOperator<O> operator)
replaceAll
in interface List<O>
public void sort(Comparator<? super O> comparator)
public ListIterator<O> listIterator()
listIterator
in interface List<O>
public ListIterator<O> listIterator(int startIndex)
listIterator
in interface List<O>
public List<O> subList(int fromIndex, int toIndex)
public final boolean equals(Object object)
equals
in interface Collection<O>
equals
in interface List<O>
public final int hashCode()
hashCode
in interface Collection<O>
hashCode
in interface List<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.