E
- the type of elements in this setpublic class EqualityTransformingSet<E> extends Object
equals
and
hashCode
which are used by the set.
It does so by storing the entries in an inner set and providing a transforming view on them. See the
package
documentation for general comments on that.
This implementation mitigates the type safety problems by optionally using a token of 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 still occur.
By default the inner set will be a HashSet
but another map can be provided to the builder. Such instances
must be empty and not be referenced anywhere else. The implementations of equals
and hashCode
are
provided as functions to the builder - see there for details.
The transformations used by this set preserve object identity of outer values. This means if values are added to this set, an iteration over it will return the same instances.
EqualityTransformingSet
s are created with a EqualityTransformingCollectionBuilder
.
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<org.codefx.libfx.collection.transform.EqHash<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 org.codefx.libfx.collection.transform.EqHash<E> |
transformToInner(E outerElement)
Transforms the specified element to an instance of the inner type.
|
protected E |
transformToOuter(org.codefx.libfx.collection.transform.EqHash<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
protected Set<org.codefx.libfx.collection.transform.EqHash<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(org.codefx.libfx.collection.transform.EqHash<E> innerElement) throws ClassCastException
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 nullClassCastException
- if the specified element is not of the correct typeprotected 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 org.codefx.libfx.collection.transform.EqHash<E> transformToInner(E outerElement) throws ClassCastException
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 nullClassCastException
- if the specified element is not of the correct typeprotected 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.