Package com.groiss.ds

Class TwoLevelMap<K1,K2,V>

java.lang.Object
com.groiss.ds.TwoLevelMap<K1,K2,V>
All Implemented Interfaces:
Map<K1,Map<K2,V>>

public class TwoLevelMap<K1,K2,V> extends Object implements Map<K1,Map<K2,V>>
HashMap based two-level Map. A value can be associated with a "key path" which is an ordered list of two keys. It can be also be determined if something has been associated with the first component of a key path irrespective of the concrete second component of the key path.
  • Constructor Details

    • TwoLevelMap

      public TwoLevelMap()
      Constructs an empty TwoLevelMap.
    • TwoLevelMap

      public TwoLevelMap(String name)
      Constructs an empty TwoLevelMap with the specified name.
      Parameters:
      name - the name of this map.
    • TwoLevelMap

      public TwoLevelMap(String name, int initialCapacity)
      Constructs an empty TwoLevelMap with the specified name and initial capacity.
      Parameters:
      name - the name of this map.
      initialCapacity - the initial capacity.
  • Method Details

    • clear

      public void clear()
      Specified by:
      clear in interface Map<K1,K2>
    • containsKey

      public boolean containsKey(K1 key1, K2 key2)
      Returns true if this map contains a mapping for the specified key path.
      Parameters:
      key1 - The first part of the key path whose presence in this map is to be tested
      key2 - The second part of the key path whose presence in this map is to be tested
      Returns:
      true if this map contains a mapping for the specified key path.
    • containsKey

      public boolean containsKey(Object key1)
      Returns true if this map contains a mapping for the specified key path component.
      Specified by:
      containsKey in interface Map<K1,K2>
      Parameters:
      key1 - The first part of the key path whose presence in this map is to be tested
      Returns:
      true if this map contains a mapping for the specified key path component.
    • containsKey

      public boolean containsKey(Pair<K1,K2> p)
      Returns true if this map contains a mapping for the specified key path designated by the specified Pair
      Parameters:
      p - the Pair designating the key path p.first designated the first path component, p.second designates the second path component
      Returns:
      true if this map contains a mapping for the specified key path.
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K1,K2>
    • entrySet

      public Set<Map.Entry<K1,Map<K2,V>>> entrySet()
      Specified by:
      entrySet in interface Map<K1,K2>
    • firstKeySet

      @Deprecated public Set<K1> firstKeySet()
      Deprecated.
      since 8.0; use keySet()
      Returns:
      the first level keyset
    • get

      public V get(K1 key1, K2 key2)
      Returns the value to which the specified key path is mapped in this map, or null if the map contains no mapping for this key path. A return value of null does not necessarily indicate that the map contains no mapping for the key path ; it is also possible that the map explicitly maps the key path to null. The containsKey method may be used to distinguish these two cases.
      Parameters:
      key1 - the first component of the key path whose associated map is to be returned.
      key2 - the second component of the key path whose associated map is to be returned.
      Returns:
      the value to which this map maps the specified key path, or null if the map contains no mapping for this key path.
      See Also:
    • get

      public Map<K2,V> get(Object key1)
      Returns the Map of values to which the specified key path component is mapped in this map, or null if the map contains no mapping for this key path component. A return value of null does not necessarily indicate that the map contains no mapping for the key path component; it is also possible that the map explicitly maps the key path component to null. The containsKey method may be used to distinguish these two cases.
      Specified by:
      get in interface Map<K1,K2>
      Parameters:
      key1 - the first component of the key path whose associated map is to be returned.
      Returns:
      the value to which this map maps the specified key path component, or null if the map contains no mapping for this key path component.
      See Also:
    • get

      public V get(Pair<K1,K2> p)
      Returns the value to which the specified key path designated by the Pair is mapped in this map, or null if the map contains no mapping for this key path. A return value of null does not necessarily indicate that the map contains no mapping for the key path ; it is also possible that the map explicitly maps the key path to null. The containsKey method may be used to distinguish these two cases.
      Parameters:
      p - the Pair designating the key path p.first designated the first path component, p.second designates the second path component
      Returns:
      the value to which this map maps the specified key path, or null if the map contains no mapping for this key path.
      See Also:
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K1,K2>
    • keySet

      public Set<K1> keySet()
      Specified by:
      keySet in interface Map<K1,K2>
    • put

      public Object put(K1 key1, K2 key2, V value)
      Associates the specified value with the specified key path key1 / key2 in this map. If the map previously contained a mapping for this key path, the old value is replaced.
      Parameters:
      key1 - the first component of the key path with which the specified value is to be associated.
      key2 - the second component of the key path with which the specified value is to be associated.
      value - value to be associated with the specified key path.
      Returns:
      previous value associated with specified key path, or null if there was no mapping for key path. A null return can also indicate that the HashMap previously associated null with the specified key.
    • put

      public Map<K2,V> put(K1 key, Map<K2,V> value)
      Specified by:
      put in interface Map<K1,K2>
    • put

      public Object put(Pair<K1,K2> p, V value)
      Associates the specified value with the key path designated by the Pair in this map. If the map previously contained a mapping for this key path, the old value is replaced.
      Parameters:
      p - the Pair designating the key path p.first() designated the first path component, p.second() designates the second path component
      value - value to be associated with the specified key path.
      Returns:
      previous value associated with specified key path, or null if there was no mapping for key path. A null return can also indicate that the HashMap previously associated null with the specified key.
    • putAll

      public void putAll(Map<? extends K1,? extends Map<K2,V>> t)
      Specified by:
      putAll in interface Map<K1,K2>
    • remove

      public Map<K2,V> remove(Object key1)
      Removes the mapping for this key path component from this map if present.
      Specified by:
      remove in interface Map<K1,K2>
      Parameters:
      key1 - key path component whose mapping is to be removed from the map.
      Returns:
      previous Map associated with specified key path component , or null if there was no mapping for key1. A null return can also indicate that the map previously associated null with the specified key.
    • removeByKeys

      public V removeByKeys(Object key1, Object key2)
      Removes the mapping for this key path from this map if present.
      Parameters:
      key1 - the first component of the key path whose mapping is to be removed from the map.
      key2 - the second component of the key path whose mapping is to be removed from the map.
      Returns:
      previous value associated with specified key path, or null if there was no mapping for the key path. A null return can also indicate that the value previously associated null with the specified key path.
      Since:
      ep9.0 (February 2015); formerly this method has been named remove. Renamed because of clash with new method in Java 8.
    • remove

      public V remove(Pair<?,?> p)
      Removes the mapping for the key path designated by the specified Pair from this map if present.
      Parameters:
      p - the Pair designating the key path p.first designated the first path component, p.second designates the second path component
      Returns:
      previous value associated with specified key path, or null if there was no mapping for the key path. A null return can also indicate that the value previously associated null with the specified key path.
    • size

      public int size()
      Returns the number of first level key path mappings in this map.
      Specified by:
      size in interface Map<K1,K2>
      Returns:
      the number of first level key path mappings in this map.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • values

      public Collection<Map<K2,V>> values()
      Specified by:
      values in interface Map<K1,K2>