Class EnumToIntMap<K extends Enum<K>>

java.lang.Object
org.klojang.util.collection.EnumToIntMap<K>
Type Parameters:
K - The type of the enum class
All Implemented Interfaces:
org.klojang.check.aux.Emptyable

public final class EnumToIntMap<K extends Enum<K>> extends Object implements org.klojang.check.aux.Emptyable
A fast enum-to-int map. The map is backed by an int array with the same length as the number of constants in the enum class. One integer must be designated to signify the absence of a key. By default, this is Integer.MIN_VALUE. All elements in the int array are initialized to this value — meaning the map is empty. It is not allowed to put a key with this value to the map, as is would in effect amount to removing that key from the map. It is also not allowed to pass this value to containsValue(). In both cases an IllegalArgumentException is thrown. Empty enum classes (i.e. enum classes without enum constants) are not supported.
  • Constructor Summary

    Constructors
    Constructor
    Description
    EnumToIntMap(Class<K> enumClass)
    Creates a new empty EnumToIntMap for the specified enum class using Integer.MIN_VALUE as the key-absent-value value.
    EnumToIntMap(Class<K> enumClass, int keyAbsentValue)
    Creates a new EnumToIntMap for the specified enum class with the specified integer as the key-absent-value value.
    EnumToIntMap(Class<K> enumClass, int keyAbsentValue, ToIntFunction<K> initializer)
    Creates a new EnumToIntMap with the specified key-absent-value value and the specified initializer function.
    EnumToIntMap(Class<K> enumClass, ToIntFunction<K> initializer)
    Creates a new EnumToIntMap using Integer.MIN_VALUE as the key-absent-value value and with its keys initialized using the specified initializer function.
    Instantiates a new EnumToIntMap with the same key-value mappings as the specified EnumToIntMap and with the same key-absent-value value.
    EnumToIntMap(EnumToIntMap<K> other, int keyAbsentValue)
    Instantiates a new EnumToIntMap with the same key-value mappings as the specified EnumToIntMap, but (potentially) with a new key-absent-value.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all mappings from this map.
    boolean
    Returns true if this map contains a mapping for the specified key.
    boolean
    containsValue(int val)
    Returns true if this map maps one or more keys to the specified value.
    Returns a Set view of the mappings contained in this map.
    Returns the type of the enum keys in this map.
    boolean
    Returns true if the argument is an EnumToIntMap for the same enum class and if it contains the same key-value mappings.
    void
    Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
    int
    get(K key)
    Returns the value to which the specified enum constant is mapped, or the key-absent-value if this map contains no mapping for the key.
    int
    getOrDefault(K key, int dfault)
    Returns the value associated with the specified enum constant or dfault if the map did not contain an entry for the specified enum constant.
    int
     
    Returns an IntList containing the values of this map.
    boolean
    Returns true if this map contains no key-value mappings, false otherwise.
    int
    Returns the integer used to signify the absence of a key.
    Returns a Set view of the keys contained in this map.
    int
    put(K key, int val)
    Associates the specified value with the specified key in this map.
    void
    putAll(Map<K,Integer> other)
    Adds all entries of the specified map to this map.
    void
    Adds all entries of the specified map to this map, overwriting any previous values.
    int
    remove(K key)
    Removes the mapping for a key from this map if it is present.
    set(K key, int val)
    Much like put, but provides a fluent API for adding entries to the map.
    int
    Returns the number of key-value mappings in this map.
    Returns an immutable, fully-generic version of this map.
     
    Returns a Collection view of the values contained in this map.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.klojang.check.aux.Emptyable

    isDeepNotEmpty
  • Constructor Details

    • EnumToIntMap

      public EnumToIntMap(Class<K> enumClass)
      Creates a new empty EnumToIntMap for the specified enum class using Integer.MIN_VALUE as the key-absent-value value. All elements in the backing array will be initialized to this value (meaning that the map is empty).
      Parameters:
      enumClass - The type of the enum class
    • EnumToIntMap

      public EnumToIntMap(Class<K> enumClass, int keyAbsentValue)
      Creates a new EnumToIntMap for the specified enum class with the specified integer as the key-absent-value value. All elements in the backing array will be initialized to this value (meaning that the map is empty).
      Parameters:
      enumClass - The type of the enum class
      keyAbsentValue - The value used to signify the absence of a key
    • EnumToIntMap

      public EnumToIntMap(Class<K> enumClass, ToIntFunction<K> initializer)
      Creates a new EnumToIntMap using Integer.MIN_VALUE as the key-absent-value value and with its keys initialized using the specified initializer function. For example:
      
       EnumToIntMap<DayOfWeek> map = new EnumToIntMap<>(DayOfWeek.class, k -> k.ordinal() + 1);
       
      Parameters:
      enumClass - The type of the enum class
      initializer - A function called to initialize the array elements
    • EnumToIntMap

      public EnumToIntMap(Class<K> enumClass, int keyAbsentValue, ToIntFunction<K> initializer)
      Creates a new EnumToIntMap with the specified key-absent-value value and the specified initializer function.
      Parameters:
      enumClass - The type of the enum class
      keyAbsentValue - The value used to signify the absence of a key
      initializer - A function called to initialize the array elements
    • EnumToIntMap

      public EnumToIntMap(EnumToIntMap<K> other)
      Instantiates a new EnumToIntMap with the same key-value mappings as the specified EnumToIntMap and with the same key-absent-value value.
      Parameters:
      other - The EnumToIntMap whose key-value mappings to copy
    • EnumToIntMap

      public EnumToIntMap(EnumToIntMap<K> other, int keyAbsentValue)
      Instantiates a new EnumToIntMap with the same key-value mappings as the specified EnumToIntMap, but (potentially) with a new key-absent-value.
      Parameters:
      other - The EnumToIntMap whose key-value mappings to copy
      keyAbsentValue - The value used to signify the absence of a key
  • Method Details

    • containsKey

      public boolean containsKey(K key)
      Returns true if this map contains a mapping for the specified key.
      Parameters:
      key - The enum constant
      Returns:
      Whether the map contains an entry for the enum constant
      See Also:
    • containsValue

      public boolean containsValue(int val)
      Returns true if this map maps one or more keys to the specified value. It is not permitted to search for the key-absent-value value. An IllegalArgumentException is thrown if you do.
      Parameters:
      val - The value
      Returns:
      Whether the map contains the value
      See Also:
    • get

      public int get(K key)
      Returns the value to which the specified enum constant is mapped, or the key-absent-value if this map contains no mapping for the key. (A regular Map would return null in the latter case.)
      Parameters:
      key - The key whose associated value is to be returned
      Returns:
      the value to which the specified key is mapped, or the key-absent-value if this map contains no mapping for the key
      See Also:
    • getOrDefault

      public int getOrDefault(K key, int dfault)
      Returns the value associated with the specified enum constant or dfault if the map did not contain an entry for the specified enum constant.
      Parameters:
      key - The key to retrieve the value of.
      dfault - The integer to return if the map did not contain the key
      Returns:
      the value associated with the key or dfault
      See Also:
    • put

      public int put(K key, int val)
      Associates the specified value with the specified key in this map.
      Parameters:
      key - The key
      val - The value
      Returns:
      the previous value associated with the specified enum constant or the key-absent-value value if the map did not contain an entry for the enum constant yet.
      See Also:
    • set

      public EnumToIntMap<K> set(K key, int val)
      Much like put, but provides a fluent API for adding entries to the map.
      Parameters:
      key - The key
      val - The value
      Returns:
      This instance
    • putAll

      public void putAll(EnumToIntMap<K> other)
      Adds all entries of the specified map to this map, overwriting any previous values. The source map must not contain the key-absent-value of this map. An IllegalArgumentException is thrown if it does.
      Parameters:
      other - The EnumToIntMap whose key-value mappings to copy
    • putAll

      public void putAll(Map<K,Integer> other)
      Adds all entries of the specified map to this map. This method acts as a bridge to fully-generic map implementations. The source map must not contain the key-absent-value of this map. An IllegalArgumentException is thrown if it does.
      Parameters:
      other - The Map whose key-value mappings to copy
    • toGenericMap

      public Map<K,Integer> toGenericMap()
      Returns an immutable, fully-generic version of this map.
      Returns:
      an immutable, fully-generic version of this map
    • remove

      public int remove(K key)
      Removes the mapping for a key from this map if it is present.
      Parameters:
      key - The key
      Returns:
      the previous value associated with key, or the key-absent-value value if there was no mapping for key.
      See Also:
    • keySet

      public Set<K> keySet()
      Returns a Set view of the keys contained in this map.
      Returns:
      a Set view of the keys contained in this map
      See Also:
    • values

      public Collection<Integer> values()
      Returns a Collection view of the values contained in this map.
      Returns:
      a Collection view of the values contained in this map
      See Also:
    • intValues

      public IntList intValues()
      Returns an IntList containing the values of this map.
      Returns:
      an IntList containing the values of this map
    • entrySet

      public Set<Map.Entry<K,Integer>> entrySet()
      Returns a Set view of the mappings contained in this map.
      Returns:
      a set view of the mappings contained in this map
      See Also:
    • isEmpty

      public boolean isEmpty()
      Returns true if this map contains no key-value mappings, false otherwise.
      Specified by:
      isEmpty in interface org.klojang.check.aux.Emptyable
      Returns:
      true if this map contains no key-value mappings, false otherwise
      See Also:
    • forEach

      public void forEach(ObjIntConsumer<K> action)
      Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
      Parameters:
      action - The action to be performed for each entry
      See Also:
    • clear

      public void clear()
      Removes all mappings from this map.
      See Also:
    • size

      public int size()
      Returns the number of key-value mappings in this map.
      Returns:
      the number of key-value mappings in this map
      See Also:
    • enumClass

      public Class<K> enumClass()
      Returns the type of the enum keys in this map.
      Returns:
      the type of the enum keys
    • keyAbsentValue

      public int keyAbsentValue()
      Returns the integer used to signify the absence of a key.
      Returns:
      the integer used to signify the absence of a key
    • equals

      public boolean equals(Object obj)
      Returns true if the argument is an EnumToIntMap for the same enum class and if it contains the same key-value mappings. The two maps need not have the same key-absent-value value.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object