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:

Constraints

  • This map does not support null keys, since null is used as a sentinel value when no mapping from storage to this 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 the size() method.
Since:
0.3
Version:
0.3
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Serial number for inter-operability with different versions.
      See Also:
    • storage

      protected final Map<SK,SV> storage
      The storage map whose keys are derived from.
    • keyConverter

      protected final ObjectConverter<SK,K> keyConverter
      The converter from the storage to the derived keys.
    • valueConverter

      protected final ObjectConverter<SV,V> valueConverter
      The converter from the storage to the derived values.
    • keySet

      private transient Set<K> keySet
      Key set. Will be constructed only when first needed.
      See Also:
    • entrySet

      private transient Set<Map.Entry<K,V>> 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, 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.
      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.
      Specified by:
      size in interface Map<SK,SV>
      Overrides:
      size in class AbstractMap<K,V>
      Returns:
      the number of entries in this map.
    • isEmpty

      public boolean isEmpty()
      Returns true if this map contains no key-value mappings.
      Specified by:
      isEmpty in interface Map<SK,SV>
      Overrides:
      isEmpty in class AbstractMap<K,V>
      Returns:
      true if this map contains no key-value mappings.
    • put

      public V put(K key, V value) throws UnsupportedOperationException
      Associates the specified value with the specified key in this map.
      Specified by:
      put in interface Map<SK,SV>
      Overrides:
      put in class AbstractMap<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 the put operation.
    • put

      final V put(K original, SK key, SV value)
      Implementation of the put(Object,Object) method storing the given converted entry to the storage map. The original key is used only for formatting an error message in case of failure.
    • keySet

      public final Set<K> keySet()
      Returns a set view of the keys contained in this map.
      Specified by:
      keySet in interface Map<SK,SV>
      Overrides:
      keySet in class AbstractMap<K,V>
      Returns:
      a view of the keys in this map.
    • entrySet

      public final Set<Map.Entry<K,V>> entrySet()
      Returns a set view of the mappings contained in this map.
      Specified by:
      entrySet in interface Map<SK,SV>
      Specified by:
      entrySet in class AbstractMap<K,V>
      Returns:
      a view of the entries in this map.
    • properties

      public final Set<FunctionProperty> properties()
      Returns the properties of the entry converter, as the union of some properties of the key and value converters.
      Specified by:
      properties in interface ObjectConverter<SK,SV>
      Returns:
      the manners in which source values are mapped to target values. May be an empty set, but never null.
    • getSourceClass

      public final Class<Map.Entry<SK,SV>> getSourceClass()
      Returns the source class of the map entry converter. Defined because the interface requires so but not used.
      Specified by:
      getSourceClass in interface ObjectConverter<SK,SV>
      Returns:
      the type of objects to convert.
    • getTargetClass

      public final Class<Map.Entry<K,V>> getTargetClass()
      Returns the target class of the map entry converter. Defined because the interface requires so but not used.
      Specified by:
      getTargetClass in interface ObjectConverter<SK,SV>
      Returns:
      the type of converted objects.
    • apply

      public final Map.Entry<K,V> apply(Map.Entry<SK,SV> entry)
      Converts the given entry.
      Specified by:
      apply in interface Function<SK,SV>
      Specified by:
      apply in interface ObjectConverter<SK,SV>
      Parameters:
      entry - an entry with the key and value from the storage.
      Returns:
      an entry with the key and value converted to types declared by this map, or null if the key is unconvertible.
    • inverse

      To be defined in the DerivedMap.Invertible sub-class only.
      Specified by:
      inverse in interface ObjectConverter<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: