Package org.apache.sis.util.collection
Class DerivedMap<SK,SV,K,V>
java.lang.Object
java.util.AbstractMap<K,V>
org.apache.sis.util.collection.DerivedMap<SK,SV,K,V>
- Type Parameters:
SK
- the type of keys in the storage map.SV
- the type of values in the storage map.K
- the type of keys in this map.V
- the type of values in this map.
- All Implemented Interfaces:
Serializable
,Function<Map.Entry<SK,
,SV>, Map.Entry<K, V>> Map<K,
,V> ObjectConverter<Map.Entry<SK,
SV>, Map.Entry<K, V>>
- Direct Known Subclasses:
DerivedMap.InvertibleKey
,DerivedMap.InvertibleValue
class DerivedMap<SK,SV,K,V>
extends AbstractMap<K,V>
implements ObjectConverter<Map.Entry<SK,SV>,Map.Entry<K,V>>, Serializable
A map whose keys and values are derived on-the-fly from another map.
Conversions are performed when needed by the following methods:
- The iterators over the key set or entry set
obtain the derived keys using the
keyConverter
. - The iterators over the values or entry set
obtain the derived values using the
valueConverter
. - Queries (
get
,containsKey
) and write operations (put
,remove
) obtain the storage values using the inverse of the above converters.
Constraints
- This map does not support
null
keys, sincenull
is used as a sentinel value when no mapping from storage tothis
exists. - Instances of this class are serializable if their underlying storage map is serializable.
- This class is not thread-safe.
Performance considerations
This class does not cache any value, since the storage map is presumed modifiable. If the storage map is known to be immutable, then sub-classes may consider to cache some values, especially the result of thesize()
method.- Since:
- 0.3
- Version:
- 0.3
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static class
ADerivedMap
used when thekeyConverter
is invertible.private static final class
ADerivedMap
used when thevalueConverter
is invertible.Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,
V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionEntry set.protected final ObjectConverter
<SK, K> The converter from the storage to the derived keys.Key set.private static final long
Serial number for inter-operability with different versions.The storage map whose keys are derived from.protected final ObjectConverter
<SV, V> The converter from the storage to the derived values. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
DerivedMap
(Map<SK, SV> storage, ObjectConverter<SK, K> keyConverter, ObjectConverter<SV, V> valueConverter) Creates a new derived map from the specified storage map. -
Method Summary
Modifier and TypeMethodDescriptionConverts the given entry.(package private) static <SK,
SV, K, V>
Map<K, V> create
(Map<SK, SV> storage, ObjectConverter<SK, K> keyConverter, ObjectConverter<SV, V> valueConverter) Creates a new derived map from the specified storage map.entrySet()
Returns a set view of the mappings contained in this map.Returns the source class of the map entry converter.Returns the target class of the map entry converter.inverse()
To be defined in theDerivedMap.Invertible
sub-class only.boolean
isEmpty()
Returnstrue
if this map contains no key-value mappings.keySet()
Returns a set view of the keys contained in this map.final Set
<FunctionProperty> Returns the properties of the entry converter, as the union of some properties of the key and value converters.(package private) final V
Implementation of theput(Object,Object)
method storing the given converted entry to the storage map.Associates the specified value with the specified key in this map.int
size()
Returns the number of entries in this map.Methods inherited from class java.util.AbstractMap
clear, clone, containsKey, containsValue, equals, get, hashCode, putAll, remove, toString, values
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDSerial number for inter-operability with different versions.- See Also:
-
storage
The storage map whose keys are derived from. -
keyConverter
The converter from the storage to the derived keys. -
valueConverter
The converter from the storage to the derived values. -
keySet
Key set. Will be constructed only when first needed.- See Also:
-
entrySet
Entry set. Will be constructed only when first needed.- See Also:
-
-
Constructor Details
-
DerivedMap
private DerivedMap(Map<SK, SV> storage, ObjectConverter<SK, K> keyConverter, ObjectConverter<SV, V> valueConverter) Creates a new derived map from the specified storage map.- Parameters:
storage
- the map which actually store the entries.keyConverter
- the converter for the keys.valueConverter
- the converter for the values.
-
-
Method Details
-
create
static <SK,SV, Map<K,K, V> V> create(Map<SK, SV> storage, ObjectConverter<SK, K> keyConverter, ObjectConverter<SV, V> valueConverter) Creates a new derived map from the specified storage map.- Parameters:
storage
- the map which actually store the entries.keyConverter
- the converter for the keys.valueConverter
- the converter for the values.
-
size
public int size()Returns the number of entries in this map. -
isEmpty
public boolean isEmpty()Returnstrue
if this map contains no key-value mappings. -
put
Associates the specified value with the specified key in this map.- Specified by:
put
in interfaceMap<SK,
SV> - Overrides:
put
in classAbstractMap<K,
V> - Parameters:
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.- Returns:
- previous value associated with specified key, or
null
if there was no mapping for key. - Throws:
UnsupportedOperationException
- if the converters are not invertible, or the storage map doesn't supports theput
operation.
-
put
Implementation of theput(Object,Object)
method storing the given converted entry to the storage map. Theoriginal
key is used only for formatting an error message in case of failure. -
keySet
Returns a set view of the keys contained in this map. -
entrySet
Returns a set view of the mappings contained in this map. -
properties
Returns the properties of the entry converter, as the union of some properties of the key and value converters.- Specified by:
properties
in interfaceObjectConverter<SK,
SV> - Returns:
- the manners in which source values are mapped to target values. May be an empty set, but never null.
-
getSourceClass
Returns the source class of the map entry converter. Defined because the interface requires so but not used.- Specified by:
getSourceClass
in interfaceObjectConverter<SK,
SV> - Returns:
- the type of objects to convert.
-
getTargetClass
Returns the target class of the map entry converter. Defined because the interface requires so but not used.- Specified by:
getTargetClass
in interfaceObjectConverter<SK,
SV> - Returns:
- the type of converted objects.
-
apply
Converts the given entry. -
inverse
public ObjectConverter<Map.Entry<K,V>, inverse() throws UnsupportedOperationExceptionMap.Entry<SK, SV>> To be defined in theDerivedMap.Invertible
sub-class only.- Specified by:
inverse
in interfaceObjectConverter<SK,
SV> - Returns:
- a converter for converting instances of T back to instances of S.
- Throws:
UnsupportedOperationException
- if this converter is not invertible.- See Also:
-