Package org.apache.sis.util.collection
Class DerivedSet<S,E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
org.apache.sis.internal.util.SetOfUnknownSize<E>
org.apache.sis.util.collection.DerivedSet<S,E>
- Type Parameters:
S
- the type of elements in the storage set.E
- the type of elements in this set.
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,Set<E>
,CheckedContainer<E>
- Direct Known Subclasses:
DerivedSet.Invertible
A set whose values are derived on-the-fly from another set.
Conversions are performed when needed by two converters:
- The iterator obtain the derived values using the converter.
- Queries (
contains
) and write operations (add
,remove
) obtain the storage values using theDerivedSet.Invertible.inverse
converter.
Constraints
- This set does not support
null
values, sincenull
is used as a sentinel value when no mapping from storage tothis
exists. - Instances of this class are serializable if their underlying storage set and the converter are serializable.
- This class performs no synchronization by itself. Nevertheless instances of this class may be thread-safe (depending on the sub-class implementation) if the underlying storage set (including its iterator) and the converter are thread-safe.
Performance considerations
This class does not cache any value, since the storage set is presumed modifiable. If the storage set is known to be immutable, then sub-classes may consider to cache some values, especially the result of theSetOfUnknownSize.size()
method.- Since:
- 0.3
- Version:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
ADerivedSet
for converters that are both invertible and bijective.private static class
ADerivedSet
for invertible converters. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ObjectConverter<S,
E> The converter from the storage to the derived type.private static final long
Serial number for inter-operability with different versions.The storage set whose values are derived from. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
DerivedSet
(Set<S> storage, ObjectConverter<S, E> converter) Creates a new derived set from the specified storage set. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Ensures that this set contains the specified element.(package private) final boolean
Implementation of theadd(Object)
method adding the given converted value to the storage set.(package private) static <S,
E> Set<E> create
(Set<S> storage, ObjectConverter<S, E> converter) Creates a new derived set from the specified storage set.Returns the derived element type.boolean
isEmpty()
Returnstrue
if this set contains no elements.iterator()
Returns an iterator over the elements contained in this set.Methods inherited from class org.apache.sis.internal.util.SetOfUnknownSize
equals, isSizeKnown, removeAll, size, spliterator, toArray, toArray
Methods inherited from class java.util.AbstractSet
hashCode
Methods inherited from class java.util.AbstractCollection
addAll, clear, contains, containsAll, remove, retainAll, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerial number for inter-operability with different versions.- See Also:
-
storage
The storage set whose values are derived from. -
converter
The converter from the storage to the derived type.
-
-
Constructor Details
-
DerivedSet
Creates a new derived set from the specified storage set.- Parameters:
storage
- the set which actually store the elements.converter
- the converter from the storage to the derived type.
-
-
Method Details
-
create
Creates a new derived set from the specified storage set.- Parameters:
storage
- the set which actually store the elements.converter
- the converter from the type in the storage set to the type in the derived set.
-
getElementType
Returns the derived element type.- Specified by:
getElementType
in interfaceCheckedContainer<S>
- Returns:
- the element type.
-
iterator
Returns an iterator over the elements contained in this set. The iterator will invoke theObjectConverter.apply(Object)
method for each element. -
isEmpty
public boolean isEmpty()Returnstrue
if this set contains no elements.- Specified by:
isEmpty
in interfaceCollection<S>
- Specified by:
isEmpty
in interfaceSet<S>
- Overrides:
isEmpty
in classSetOfUnknownSize<E>
- Returns:
true
if this set contains no elements.
-
add
Ensures that this set contains the specified element. This method first checks if the given element is non-null, then delegates to thestorage
set like below:- Specified by:
add
in interfaceCollection<S>
- Specified by:
add
in interfaceSet<S>
- Overrides:
add
in classAbstractCollection<E>
- Parameters:
element
- element whose presence in this set is to be ensured.- Returns:
true
if the set changed as a result of the call.- Throws:
UnsupportedOperationException
- if the storage set doesn't supports theadd
operation.
-
add
Implementation of theadd(Object)
method adding the given converted value to the storage set. Theoriginal
value is used only for formatting an error message in case of failure.
-