Class BiInt2ObjectMap<V>

  • Type Parameters:
    V - type of the object stored in the map.
    Direct Known Subclasses:
    BiInt2NullableObjectMap

    public class BiInt2ObjectMap<V>
    extends java.lang.Object
    Map that takes two part int key and associates with an object.
    • Constructor Summary

      Constructors 
      Constructor Description
      BiInt2ObjectMap()
      Construct an empty map.
      BiInt2ObjectMap​(int initialCapacity, float loadFactor)
      Construct a map that sets it initial capacity and load factor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int capacity()
      Get the total capacity for the map to which the load factor with be a fraction of.
      void clear()
      Clear out the map of all entries.
      void compact()
      Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.
      private void compactChain​(int deleteIndex)  
      V compute​(int keyPartA, int keyPartB, BiInt2ObjectMap.EntryRemap<? super V,​? extends V> remappingFunction)
      Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
      V computeIfAbsent​(int keyPartA, int keyPartB, BiInt2ObjectMap.EntryFunction<? extends V> mappingFunction)
      If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
      V computeIfPresent​(int keyPartA, int keyPartB, BiInt2ObjectMap.EntryRemap<? super V,​? extends V> remappingFunction)
      If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
      boolean containsKey​(int keyPartA, int keyPartB)
      Returns true if this map contains a mapping for the specified key.
      void forEach​(java.util.function.Consumer<V> consumer)
      Iterate over the contents of the map.
      void forEach​(BiInt2ObjectMap.EntryConsumer<V> consumer)
      Iterate over the contents of the map.
      V get​(int keyPartA, int keyPartB)
      Retrieve a value from the map.
      private V getMapping​(int keyPartA, int keyPartB)  
      V getOrDefault​(int keyPartA, int keyPartB, V defaultValue)
      Retrieve a value from the map or defaultValue if this map contains not mapping for the key.
      private void increaseCapacity()  
      boolean isEmpty()
      Is map empty or not.
      float loadFactor()
      Get the load factor beyond which the map will increase size.
      protected java.lang.Object mapNullValue​(java.lang.Object value)
      Interceptor for masking null values.
      V merge​(int keyPartA, int keyPartB, V value, java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
      If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
      V put​(int keyPartA, int keyPartB, V value)
      Put a value into the map.
      V putIfAbsent​(int keyPartA, int keyPartB, V value)
      If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.
      private void rehash​(int newCapacity)  
      V remove​(int keyPartA, int keyPartB)
      Remove a value from the map and return the value.
      boolean remove​(int keyPartA, int keyPartB, V value)
      Removes the entry for the specified key only if it is currently mapped to the specified value.
      V replace​(int keyPartA, int keyPartB, V value)
      Replaces the entry for the specified key only if currently mapped to the specified value.
      boolean replace​(int keyPartA, int keyPartB, V oldValue, V newValue)
      Replaces the entry for the specified key only if currently mapped to the specified value.
      int resizeThreshold()
      Get the actual threshold which when reached the map will resize.
      int size()
      Return the number of unique entries in the map.
      java.lang.String toString()
      protected V unmapNullValue​(java.lang.Object value)
      Interceptor for unmasking null values.
      • Methods inherited from class java.lang.Object

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

      • loadFactor

        private final float loadFactor
      • resizeThreshold

        private int resizeThreshold
      • size

        private int size
      • keys

        private long[] keys
      • values

        private java.lang.Object[] values
    • Constructor Detail

      • BiInt2ObjectMap

        public BiInt2ObjectMap()
        Construct an empty map.
      • BiInt2ObjectMap

        public BiInt2ObjectMap​(int initialCapacity,
                               float loadFactor)
        Construct a map that sets it initial capacity and load factor.
        Parameters:
        initialCapacity - for the underlying hash map.
        loadFactor - for the underlying hash map.
    • Method Detail

      • capacity

        public int capacity()
        Get the total capacity for the map to which the load factor with be a fraction of.
        Returns:
        the total capacity for the map.
      • loadFactor

        public float loadFactor()
        Get the load factor beyond which the map will increase size.
        Returns:
        load factor for when the map should increase size.
      • resizeThreshold

        public int resizeThreshold()
        Get the actual threshold which when reached the map will resize. This is a function of the current capacity and load factor.
        Returns:
        the threshold when the map will resize.
      • clear

        public void clear()
        Clear out the map of all entries.
        See Also:
        Map.clear()
      • compact

        public void compact()
        Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.
      • put

        public V put​(int keyPartA,
                     int keyPartB,
                     V value)
        Put a value into the map.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        value - to put into the map.
        Returns:
        the previous value if found otherwise null.
        See Also:
        Map.put(Object, Object)
      • mapNullValue

        protected java.lang.Object mapNullValue​(java.lang.Object value)
        Interceptor for masking null values.
        Parameters:
        value - value to mask.
        Returns:
        masked value.
      • unmapNullValue

        protected V unmapNullValue​(java.lang.Object value)
        Interceptor for unmasking null values.
        Parameters:
        value - value to unmask.
        Returns:
        unmasked value.
      • getMapping

        private V getMapping​(int keyPartA,
                             int keyPartB)
      • get

        public V get​(int keyPartA,
                     int keyPartB)
        Retrieve a value from the map.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        Returns:
        value matching the key if found or null if not found.
        See Also:
        Map.get(Object)
      • getOrDefault

        public V getOrDefault​(int keyPartA,
                              int keyPartB,
                              V defaultValue)
        Retrieve a value from the map or defaultValue if this map contains not mapping for the key.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        defaultValue - the default mapping of the key.
        Returns:
        value matching the key if found or defaultValue if not found.
        See Also:
        Map.getOrDefault(Object, Object)
      • containsKey

        public boolean containsKey​(int keyPartA,
                                   int keyPartB)
        Returns true if this map contains a mapping for the specified key.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        Returns:
        true if this map contains a mapping for the specified key
        See Also:
        Map.containsKey(Object)
      • remove

        public V remove​(int keyPartA,
                        int keyPartB)
        Remove a value from the map and return the value.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        Returns:
        the previous value if found otherwise null
        See Also:
        Map.remove(Object)
      • computeIfAbsent

        public V computeIfAbsent​(int keyPartA,
                                 int keyPartB,
                                 BiInt2ObjectMap.EntryFunction<? extends V> mappingFunction)
        If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        mappingFunction - creates values based upon keys if the key pair is missing.
        Returns:
        the newly created or stored value.
        See Also:
        Map.computeIfAbsent(Object, Function)
      • computeIfPresent

        public V computeIfPresent​(int keyPartA,
                                  int keyPartB,
                                  BiInt2ObjectMap.EntryRemap<? super V,​? extends V> remappingFunction)
        If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.

        If the function returns null, the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.

        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        remappingFunction - the function to compute a value.
        Returns:
        the new value associated with the specified key, or null if none.
        See Also:
        Map.computeIfPresent(Object, BiFunction)
      • compute

        public V compute​(int keyPartA,
                         int keyPartB,
                         BiInt2ObjectMap.EntryRemap<? super V,​? extends V> remappingFunction)
        Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        remappingFunction - the function to compute a value.
        Returns:
        the new value associated with the specified key, or null if none.
        See Also:
        Map.compute(Object, BiFunction)
      • merge

        public V merge​(int keyPartA,
                       int keyPartB,
                       V value,
                       java.util.function.BiFunction<? super V,​? super V,​? extends V> remappingFunction)
        If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        value - the non-null value to be merged with the existing value associated with the key or, if no existing value or a null value is associated with the key, to be associated with the key.
        remappingFunction - the function to recompute a value if present.
        Returns:
        the new value associated with the specified key, or null if no value is associated with the key.
        See Also:
        Map.merge(Object, Object, BiFunction)
      • forEach

        public void forEach​(java.util.function.Consumer<V> consumer)
        Iterate over the contents of the map.
        Parameters:
        consumer - to apply to each value in the map.
      • forEach

        public void forEach​(BiInt2ObjectMap.EntryConsumer<V> consumer)
        Iterate over the contents of the map.
        Parameters:
        consumer - to apply to each value in the map.
      • replace

        public V replace​(int keyPartA,
                         int keyPartB,
                         V value)
        Replaces the entry for the specified key only if currently mapped to the specified value.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        value - value to be associated with the specified key.
        Returns:
        the previous value associated with the specified key, or null if there was no mapping for the key. A null return can also indicate that the map previously associated null with the key, if the implementation supports null values.
        See Also:
        Map.replace(Object, Object)
      • replace

        public boolean replace​(int keyPartA,
                               int keyPartB,
                               V oldValue,
                               V newValue)
        Replaces the entry for the specified key only if currently mapped to the specified value.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        oldValue - value expected to be associated with the specified key.
        newValue - to be associated with the specified key.
        Returns:
        true if the value was replaced.
      • putIfAbsent

        public V putIfAbsent​(int keyPartA,
                             int keyPartB,
                             V value)
        If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        value - to put into the map.
        Returns:
        the previous value if found otherwise null.
      • remove

        public boolean remove​(int keyPartA,
                              int keyPartB,
                              V value)
        Removes the entry for the specified key only if it is currently mapped to the specified value.
        Parameters:
        keyPartA - for the key.
        keyPartB - for the key.
        value - value expected to be associated with the specified key.
        Returns:
        true if the value was removed.
        See Also:
        Map.remove(Object, Object)
      • size

        public int size()
        Return the number of unique entries in the map.
        Returns:
        number of unique entries in the map.
      • isEmpty

        public boolean isEmpty()
        Is map empty or not.
        Returns:
        boolean indicating empty map or not.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • rehash

        private void rehash​(int newCapacity)
      • compactChain

        private void compactChain​(int deleteIndex)
      • increaseCapacity

        private void increaseCapacity()