Class HeteroMap

java.lang.Object
java.util.AbstractMap<HeteroMap.Key,Object>
eu.bandm.tools.util.java.HeteroMap
All Implemented Interfaces:
Map<HeteroMap.Key,Object>

public class HeteroMap extends AbstractMap<HeteroMap.Key,Object>
Immutable map with heterogeneously typed entries.

Keys are restricted to instances of a generic interface that is parameterized with the type of the corresponding value. Consequently, null keys are not supported.

This implementation is not suitable for high-performance applications.

  • Method Details

    • get

      public <V> @Opt V get(HeteroMap.Key<V> key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
      Type Parameters:
      V - the type of the value
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the value to which the specified key is mapped, or null if this map contains no mapping for the key
      Throws:
      NullPointerException - if key is null
    • entrySet

      public Set<Map.Entry<HeteroMap.Key,Object>> entrySet()
      Specified by:
      entrySet in interface Map<HeteroMap.Key,Object>
      Specified by:
      entrySet in class AbstractMap<HeteroMap.Key,Object>
    • empty

      public static HeteroMap empty()
      Returns an empty map.
      Returns:
      a map that contains no key–value pairs
    • with

      public <V> HeteroMap with(HeteroMap.Key<V> key, @Opt V value)
      Returns a map that has the same mappings as this map, with one exception.

      The new map may either add a value (for a key that has previously not been mapped), or replace/remove a value (for a key that has previously been mapped).

      The original map is not modified. The returned map may be identical to the original, if the operation has no effect. Namely, removing a null value and replacing a value by one that is equal according to Objects.equals(Object,Object) is considered to have no effect.

      Type Parameters:
      V - the type of the new value
      Parameters:
      key - the key to map to a new value
      value - the new value, or null for unmapping key
      Returns:
      a map that maps all keys of this to the same values, except that key is mapped to value