Class DerivedMap<BK,​K,​V>

  • Type Parameters:
    BK - The type of keys in the backing map.
    K - The type of keys in this map.
    V - The type of values in both this map and the underlying map.
    All Implemented Interfaces:
    Serializable, Map<K,​V>

    public abstract class DerivedMap<BK,​K,​V>
    extends AbstractMap<K,​V>
    implements Serializable
    A map whose keys are derived from an other map. The keys are derived only when requested, which make it possible to backup potentially large maps. Implementations need only to overrides baseToDerived(BK) and derivedToBase(K) methods. This set do not supports null key, since null is used when no mapping from base to this exists. This class is serializable if the underlying base set is serializable too.

    This class is not thread-safe. Synchronizations (if wanted) are user's reponsability.

    Since:
    2.0
    Author:
    Martin Desruisseaux (IRD)
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class AbstractMap

        AbstractMap.SimpleEntry<K extends Object,​V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,​V extends Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Map<BK,​V> base
      The base map whose keys are derived from.
    • Constructor Summary

      Constructors 
      Constructor Description
      DerivedMap​(Map<BK,​V> base, Class<K> keyType)
      Creates a new derived map from the specified base map.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected abstract K baseToDerived​(BK key)
      Transforms a key from the base map to a key in this map.
      boolean containsKey​(Object key)
      Returns true if this map contains a mapping for the specified key.
      boolean containsValue​(Object value)
      Returns true if this map maps one or more keys to this value.
      protected abstract BK derivedToBase​(K key)
      Transforms a key from this derived map to a key in the base map.
      Set<Map.Entry<K,​V>> entrySet()
      Returns a set view of the mappings contained in this map.
      V get​(Object key)
      Returns the value to which this map maps the specified key.
      boolean isEmpty()
      Returns true if this map contains no key-value mappings.
      Set<K> keySet()
      Returns a set view of the keys contained in this map.
      V put​(K key, V value)
      Associates the specified value with the specified key in this map.
      V remove​(Object key)
      Removes the mapping for this key from this map if present.
      int size()
      Returns the number of key-value mappings in this map.
      Collection<V> values()
      Returns a collection view of the values contained in this map.
      • Methods inherited from class AbstractMap

        clear, clone, equals, hashCode, putAll, toString
      • Methods inherited from class Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • DerivedMap

        public DerivedMap​(Map<BK,​V> base,
                          Class<K> keyType)
        Creates a new derived map from the specified base map.
        Parameters:
        base - The base map.
        keyType - the type of keys in the derived map.
        Since:
        2.5
    • Method Detail

      • baseToDerived

        protected abstract K baseToDerived​(BK key)
        Transforms a key from the base map to a key in this map. If there is no key in the derived map for the specified base key, then this method returns null.
        Parameters:
        key - A ley from the base map.
        Returns:
        The key that this view should contains instead of key, or null.
      • derivedToBase

        protected abstract BK derivedToBase​(K key)
        Transforms a key from this derived map to a key in the base map.
        Parameters:
        key - A key in this map.
        Returns:
        The key stored in the base map.
      • size

        public int size()
        Returns the number of key-value mappings in this map.
        Specified by:
        size in interface Map<BK,​K>
        Overrides:
        size in class AbstractMap<K,​V>
        Returns:
        the number of key-value mappings in this map.
      • isEmpty

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

        public boolean containsValue​(Object value)
        Returns true if this map maps one or more keys to this value. The default implementation invokes base.containsValue(value).
        Specified by:
        containsValue in interface Map<BK,​K>
        Overrides:
        containsValue in class AbstractMap<K,​V>
        Returns:
        true if this map maps one or more keys to this value.
      • containsKey

        public boolean containsKey​(Object key)
        Returns true if this map contains a mapping for the specified key. The default implementation invokes base.containsKey(derivedToBase(key)).
        Specified by:
        containsKey in interface Map<BK,​K>
        Overrides:
        containsKey in class AbstractMap<K,​V>
        Parameters:
        key - key whose presence in this map is to be tested.
        Returns:
        true if this map contains a mapping for the specified key.
      • get

        public V get​(Object key)
        Returns the value to which this map maps the specified key. The default implementation invokes base.get(derivedToBase(key)).
        Specified by:
        get in interface Map<BK,​K>
        Overrides:
        get in class AbstractMap<K,​V>
        Parameters:
        key - key whose associated value is to be returned.
        Returns:
        the value to which this map maps the specified key.
      • put

        public V put​(K key,
                     V value)
              throws UnsupportedOperationException
        Associates the specified value with the specified key in this map. The default implementation invokes base.put(derivedToBase(key), value) .
        Specified by:
        put in interface Map<BK,​K>
        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 base map doesn't supports the put operation.
      • remove

        public V remove​(Object key)
                 throws UnsupportedOperationException
        Removes the mapping for this key from this map if present. The default implementation invokes base.remove(derivedToBase(key)).
        Specified by:
        remove in interface Map<BK,​K>
        Overrides:
        remove in class AbstractMap<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the map.
        Returns:
        previous value associated with specified key, or null if there was no entry for key.
        Throws:
        UnsupportedOperationException - if the base map doesn't supports the remove operation.
      • keySet

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

        public Collection<V> values()
        Returns a collection view of the values contained in this map.
        Specified by:
        values in interface Map<BK,​K>
        Overrides:
        values in class AbstractMap<K,​V>
        Returns:
        a collection view of the values contained in this map.
      • entrySet

        public Set<Map.Entry<K,​V>> entrySet()
        Returns a set view of the mappings contained in this map.
        Specified by:
        entrySet in interface Map<BK,​K>
        Specified by:
        entrySet in class AbstractMap<K,​V>
        Returns:
        a set view of the mappings contained in this map.