Class AbstractDoubleKeyMap<Key1,​Key2,​Value>

  • All Implemented Interfaces:
    DoubleKeyMap<Key1,​Key2,​Value>
    Direct Known Subclasses:
    DoubleKeyHashMap

    public abstract class AbstractDoubleKeyMap<Key1,​Key2,​Value>
    extends java.lang.Object
    implements DoubleKeyMap<Key1,​Key2,​Value>
    Represents a general not thread safe DoubleKeyHashMap implementation.
    Since:
    8.1
    Author:
    Maxim Podkolzine (maxim.podkolzine@jetbrains.com)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the map.
      boolean contains​(Key1 k1, Key2 k2)
      Returns whether the map contains a value for the key pair (k1, k2).
      boolean containsAnyByKey1​(Key1 k1)
      Returns whether the map contains any value for the specified first key k1.
      boolean containsAnyByKey2​(Key2 k2)
      Returns whether the map contains any value for the specified second key k2.
      java.util.Set<java.util.Map.Entry<Key1,​java.util.Map<Key2,​Value>>> entrySet()
      Returns a set view of the map entries in a form (key1, key2 -> value).
      java.util.Set<java.util.Map.Entry<Key2,​Value>> entrySetByKey1​(Key1 k1)
      Returns a set view of the map slice by the specified first key k1.
      java.util.Set<java.util.Map.Entry<Key1,​Value>> entrySetByKey2​(Key2 k2)
      Returns a set view of the map slice by the specified second key k2.
      Value get​(Key1 k1, Key2 k2)
      Returns the value corresponding to the key pair (k1, k2).
      java.util.Map<Key2,​Value> getAllByKey1​(Key1 k1)
      Returns the map slice by the first key k1 specified.
      java.util.Map<Key1,​Value> getAllByKey2​(Key2 k2)
      Returns the map slice by the second key k2 specified.
      boolean isEmpty()
      Returns whether the map is empty.
      java.util.Map<Key1,​java.util.Set<Key2>> keys()
      Returns all the keys of this map in a form key1 -> {key2}.
      java.util.Set<Key2> keySet1​(Key1 k1)
      Returns the key set of the map slice by the specified first key k1.
      java.util.Set<Key1> keySet2​(Key2 k2)
      Returns the key set of the map slice by the specified second key k2.
      Value put​(Key1 k1, Key2 k2, Value v)
      Adds the triple to the map, overwrites existing one if present.
      void putAll​(DoubleKeyMap<Key1,​Key2,​Value> map)
      Adds another DoubleKeyMap to this map.
      void putAllForKey1​(Key1 k1, java.util.Map<Key2,​Value> map)
      Adds the bunch of values to the map, overwriting existing ones if present.
      void putAllForKey2​(Key2 k2, java.util.Map<Key1,​Value> map)
      Adds the bunch of values to the map, overwriting existing ones if present.
      protected void recalculateSize()  
      Value remove​(Key1 k1, Key2 k2)
      Removes the value from the map corresponding to the keys pair (k1, k2) (if present).
      void removeAll​(java.util.Collection<Key1> keys1, java.util.Collection<Key2> keys2)
      Removes the whole sector covered by the key sets keys1 and key2).
      java.util.Map<Key2,​Value> removeAllByKey1​(Key1 k1)
      Removes all values from the map corresponding to the first key k1 (if present).
      java.util.Map<Key1,​Value> removeAllByKey2​(Key2 k2)
      Removes all values from the map corresponding to the second key k2 (if present).
      int size()
      Returns the map size (the number of triples).
      java.util.Collection<Value> values()
      Returns a collection of values of this map.
      java.util.Collection<Value> valuesByKey1​(Key1 k1)
      Returns the values collection of the map slice by the specified first key k1.
      java.util.Collection<Value> valuesByKey2​(Key2 k2)
      Returns the values collection of the map slice by the specified second key k2.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • myMapByKey1

        protected final java.util.Map<Key1,​java.util.Map<Key2,​Value>> myMapByKey1
      • myMapByKey2

        protected final java.util.Map<Key2,​java.util.Map<Key1,​Value>> myMapByKey2
      • mySize

        protected int mySize
    • Constructor Detail

      • AbstractDoubleKeyMap

        protected AbstractDoubleKeyMap​(@NotNull
                                       java.util.Map<Key1,​java.util.Map<Key2,​Value>> mapByKey1,
                                       @NotNull
                                       java.util.Map<Key2,​java.util.Map<Key1,​Value>> mapByKey2)
    • Method Detail

      • put

        @Nullable
        public Value put​(@NotNull
                         Key1 k1,
                         @NotNull
                         Key2 k2,
                         @Nullable
                         Value v)
        Description copied from interface: DoubleKeyMap
        Adds the triple to the map, overwrites existing one if present.

        Uses the constant number of ordinary map operations (get and put).

        Specified by:
        put in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k1 - first key
        k2 - second key
        v - the value (can be null).
        Returns:
        previous value for the (k1, k2)
      • putAllForKey1

        public void putAllForKey1​(@NotNull
                                  Key1 k1,
                                  @NotNull
                                  java.util.Map<Key2,​Value> map)
        Description copied from interface: DoubleKeyMap
        Adds the bunch of values to the map, overwriting existing ones if present. More precisely, this method adds the entries (k1, k2, v), where (k2, v) are all mappings of the specified map.

        Works in a linear time of input map size.

        Specified by:
        putAllForKey1 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k1 - first key
        map - the second key based map
      • putAllForKey2

        public void putAllForKey2​(@NotNull
                                  Key2 k2,
                                  @NotNull
                                  java.util.Map<Key1,​Value> map)
        Description copied from interface: DoubleKeyMap
        Adds the bunch of values to the map, overwriting existing ones if present. More precisely, this method adds the entries (k1, k2, v), where (k1, v) are all mappings of the specified map.

        Works in a linear time of input map size.

        Specified by:
        putAllForKey2 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k2 - second key
        map - the second key based map
      • size

        public int size()
        Description copied from interface: DoubleKeyMap
        Returns the map size (the number of triples).

        Works in a constant time (O(1)). Can be invalid if DoubleKeyMap.entrySet().remove was used

        Specified by:
        size in interface DoubleKeyMap<Key1,​Key2,​Value>
        Returns:
        map size
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: DoubleKeyMap
        Returns whether the map is empty.

        Works in a constant time (O(1)).

        Specified by:
        isEmpty in interface DoubleKeyMap<Key1,​Key2,​Value>
        Returns:
        true if the map is empty
      • get

        @Nullable
        public Value get​(@NotNull
                         Key1 k1,
                         @NotNull
                         Key2 k2)
        Description copied from interface: DoubleKeyMap
        Returns the value corresponding to the key pair (k1, k2).

        Uses the constant number of ordinary map operations (get).

        Specified by:
        get in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k1 - first key
        k2 - second key
        Returns:
        the value if present, or null.
      • getAllByKey1

        @NotNull
        public java.util.Map<Key2,​Value> getAllByKey1​(@NotNull
                                                            Key1 k1)
        Description copied from interface: DoubleKeyMap
        Returns the map slice by the first key k1 specified. For all triples (k1, k2) -> value a map k2 -> value with fixed k1 is returned.

        Result map can be empty, but never null.

        Uses the constant number of ordinary map operations (get).

        Specified by:
        getAllByKey1 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k1 - first key
        Returns:
        map slice by the first key
      • getAllByKey2

        @NotNull
        public java.util.Map<Key1,​Value> getAllByKey2​(@NotNull
                                                            Key2 k2)
        Description copied from interface: DoubleKeyMap
        Returns the map slice by the second key k2 specified. For all triples (k1, k2) -> value a map k1 -> value with fixed k2 is returned.

        Result map can be empty, but never null.

        Uses the constant number of ordinary map operations (get).

        Specified by:
        getAllByKey2 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k2 - second key
        Returns:
        map slice by the second key
      • contains

        public boolean contains​(@NotNull
                                Key1 k1,
                                @NotNull
                                Key2 k2)
        Description copied from interface: DoubleKeyMap
        Returns whether the map contains a value for the key pair (k1, k2).

        Uses the constant number of ordinary map operations (get).

        Specified by:
        contains in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k1 - first key
        k2 - second key
        Returns:
        true if there is a value for the (k1, k2) pair.
      • containsAnyByKey1

        public boolean containsAnyByKey1​(@NotNull
                                         Key1 k1)
        Description copied from interface: DoubleKeyMap
        Returns whether the map contains any value for the specified first key k1.

        Uses the constant number of ordinary map operations (get).

        Specified by:
        containsAnyByKey1 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k1 - first key
        Returns:
        true if there is a value for the first key
      • containsAnyByKey2

        public boolean containsAnyByKey2​(@NotNull
                                         Key2 k2)
        Description copied from interface: DoubleKeyMap
        Returns whether the map contains any value for the specified second key k2.

        Uses the constant number of ordinary map operations (get).

        Specified by:
        containsAnyByKey2 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k2 - second key
        Returns:
        true if there is a value for the second key
      • entrySet

        @NotNull
        public java.util.Set<java.util.Map.Entry<Key1,​java.util.Map<Key2,​Value>>> entrySet()
        Description copied from interface: DoubleKeyMap
        Returns a set view of the map entries in a form (key1, key2 -> value).

        Works in a constant time (O(1)).

        Specified by:
        entrySet in interface DoubleKeyMap<Key1,​Key2,​Value>
        Returns:
        a set view of the mappings contained in this map
      • entrySetByKey1

        @NotNull
        public java.util.Set<java.util.Map.Entry<Key2,​Value>> entrySetByKey1​(@NotNull
                                                                                   Key1 k1)
        Description copied from interface: DoubleKeyMap
        Returns a set view of the map slice by the specified first key k1.

        Uses the constant number of ordinary map operations (get).

        Specified by:
        entrySetByKey1 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Returns:
        a set view of the map slice by the first key
      • entrySetByKey2

        @NotNull
        public java.util.Set<java.util.Map.Entry<Key1,​Value>> entrySetByKey2​(@NotNull
                                                                                   Key2 k2)
        Description copied from interface: DoubleKeyMap
        Returns a set view of the map slice by the specified second key k2.

        Uses the constant number of ordinary map operations (get).

        Specified by:
        entrySetByKey2 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Returns:
        a set view of the map slice by the second key
      • keys

        @NotNull
        public java.util.Map<Key1,​java.util.Set<Key2>> keys()
        Description copied from interface: DoubleKeyMap
        Returns all the keys of this map in a form key1 -> {key2}.

        Works in a linear time of map size.

        Changes in the keys map do not reflect the changes in this map.

        Specified by:
        keys in interface DoubleKeyMap<Key1,​Key2,​Value>
        Returns:
        all map keys as a map
      • keySet1

        @NotNull
        public java.util.Set<Key2> keySet1​(@NotNull
                                           Key1 k1)
        Description copied from interface: DoubleKeyMap
        Returns the key set of the map slice by the specified first key k1.

        Uses the constant number of ordinary map operations (get).

        Specified by:
        keySet1 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k1 - first key
        Returns:
        key set of the map slice by the first key
      • keySet2

        @NotNull
        public java.util.Set<Key1> keySet2​(@NotNull
                                           Key2 k2)
        Description copied from interface: DoubleKeyMap
        Returns the key set of the map slice by the specified second key k2.

        Uses the constant number of ordinary map operations (get).

        Specified by:
        keySet2 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k2 - second key
        Returns:
        key set of the map slice by the second key
      • values

        @NotNull
        public java.util.Collection<Value> values()
        Description copied from interface: DoubleKeyMap
        Returns a collection of values of this map. Result map can be empty, but never null.

        Works in a linear time of map size and uses linear memory.

        Changes in the values collection do not reflect the changes in this map.

        Specified by:
        values in interface DoubleKeyMap<Key1,​Key2,​Value>
        Returns:
        all map values
      • valuesByKey1

        @NotNull
        public java.util.Collection<Value> valuesByKey1​(@NotNull
                                                        Key1 k1)
        Description copied from interface: DoubleKeyMap
        Returns the values collection of the map slice by the specified first key k1. Result map can be empty, but never null.

        Uses the constant number of ordinary map operations (get).

        Specified by:
        valuesByKey1 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k1 - first key
        Returns:
        values collection of the map slice by the first key
      • valuesByKey2

        @NotNull
        public java.util.Collection<Value> valuesByKey2​(@NotNull
                                                        Key2 k2)
        Description copied from interface: DoubleKeyMap
        Returns the values collection of the map slice by the specified second key k2. Result map can be empty, but never null.

        Uses the constant number of ordinary map operations (get).

        Specified by:
        valuesByKey2 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k2 - second key
        Returns:
        values collection of the map slice by the second key
      • remove

        @Nullable
        public Value remove​(@NotNull
                            Key1 k1,
                            @NotNull
                            Key2 k2)
        Description copied from interface: DoubleKeyMap
        Removes the value from the map corresponding to the keys pair (k1, k2) (if present).

        Uses the constant number of ordinary map operations (get, remove).

        Specified by:
        remove in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k1 - first key
        k2 - second key
        Returns:
        the previous value for the (k1, k2) pair, or null if it is not present
      • removeAllByKey1

        @Nullable
        public java.util.Map<Key2,​Value> removeAllByKey1​(@NotNull
                                                               Key1 k1)
        Description copied from interface: DoubleKeyMap
        Removes all values from the map corresponding to the first key k1 (if present).

        Works in a linear time of the size of the returned map.

        Specified by:
        removeAllByKey1 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k1 - first key
        Returns:
        the map slice by the first key k1 present just before the removal (can be null).
      • removeAllByKey2

        @Nullable
        public java.util.Map<Key1,​Value> removeAllByKey2​(@NotNull
                                                               Key2 k2)
        Description copied from interface: DoubleKeyMap
        Removes all values from the map corresponding to the second key k2 (if present).

        Works in a linear time of the size of the returned map.

        Specified by:
        removeAllByKey2 in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        k2 - second key
        Returns:
        the map slice by the second key k2 present just before the removal (can be null).
      • removeAll

        public void removeAll​(@NotNull
                              java.util.Collection<Key1> keys1,
                              @NotNull
                              java.util.Collection<Key2> keys2)
        Description copied from interface: DoubleKeyMap
        Removes the whole sector covered by the key sets keys1 and key2). More precisely, this method removes all entries (k1, k2, v), for which keys1.contains(k1) && keys2.contains(k2).

        Time complexity is O(n1) * O(n2), where n1 and n2 are the sizes of the input collections.

        Specified by:
        removeAll in interface DoubleKeyMap<Key1,​Key2,​Value>
        Parameters:
        keys1 - the first keys collection
        keys2 - the second keys collection
      • clear

        public void clear()
        Description copied from interface: DoubleKeyMap
        Clears the map. As a result of this call, the map size would be 0.

        Works in a linear time of the map size.

        Specified by:
        clear in interface DoubleKeyMap<Key1,​Key2,​Value>
      • recalculateSize

        protected void recalculateSize()