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.extra.Emptyable

public final class EnumToIntMap<K extends Enum<K>> extends Object implements org.klojang.check.extra.Emptyable
An 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. Although EnumToIntMap loosely mimics the behaviour and interface of the Map interface, it obviously does not, and cannot implement the Map interface. Some methods need to behave differently due to the fact that the type of the values in the map is a primitive type (int).
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    EnumToIntMap(Class<K> enumClass)
    Creates an empty EnumToIntMap for the specified enum class.
    EnumToIntMap(Class<K> enumClass, int initialValue)
    Creates an EnumToIntMap saturated with all enum constants of the specified enum class.
    EnumToIntMap(Class<K> enumClass, ToIntFunction<K> initializer)
    Creates an EnumToIntMap saturated with all enum constants of the specified enum class.
    Instantiates a new EnumToIntMap with the same key-value mappings as the specified EnumToIntMap.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all mappings from this map.
    int
    computeIfPresent(K key, IntUnaryOperator operator, int initialValue)
    Computes a new value for the specified key if present, else associates the key with the specified initial value.
    boolean
    Returns true if this map contains a mapping for the specified key.
    boolean
    containsValue(int val)
    Returns true if this map contains the specified value.
    Returns a Set view of the mappings contained 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.
    get(K key)
    Returns an OptionalInt containing the value to which the specified enum constant is mapped, or an empty OptionalInt 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.
    Returns a Set view of the keys contained in this map.
    Returns the type of the keys in this map.
    put(K key, int val)
    Associates the specified value with the specified key in this map.
    void
    putAll(Map<K,Integer> other, int nullValue)
    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.
    remove(K key)
    Removes the mapping for a key from this map if it is present.
    set(K key, int val)
    Like put(), but provides a fluent API for adding entries to the map.
    int
    Returns the number of key-value mappings in this map.
    Converts this map to a 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.extra.Emptyable

    isDeepNotEmpty
  • Constructor Details

    • EnumToIntMap

      public EnumToIntMap(Class<K> enumClass)
      Creates an empty EnumToIntMap for the specified enum class.
      Parameters:
      enumClass - the type of the enum class
    • EnumToIntMap

      public EnumToIntMap(Class<K> enumClass, int initialValue)
      Creates an EnumToIntMap saturated with all enum constants of the specified enum class. The map's values are initialized to the specified initial value.
      Parameters:
      enumClass - the type of the enum class
      initialValue - the initial value for all enum constants
    • EnumToIntMap

      public EnumToIntMap(Class<K> enumClass, ToIntFunction<K> initializer)
      Creates an EnumToIntMap saturated with all enum constants of the specified enum class. The map's values are initialized using the provided function, which takes an enum constant and returns the initial value for the enum constant.
      Parameters:
      enumClass - the type of the enum class
      initializer - a function called to initialize the map's values
    • EnumToIntMap

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

    • keyType

      public Class<K> keyType()
      Returns the type of the keys in this map.
      Returns:
      the type of the keys in this map
    • 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 contains the specified value.
      Parameters:
      val - the value to search for
      Returns:
      whether the map contains the value
      See Also:
    • get

      public OptionalInt get(K key)
      Returns an OptionalInt containing the value to which the specified enum constant is mapped, or an empty OptionalInt 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
      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:
    • computeIfPresent

      public int computeIfPresent(K key, IntUnaryOperator operator, int initialValue)
      Computes a new value for the specified key if present, else associates the key with the specified initial value.
      Parameters:
      key - the key
      operator - the function to apply to the value currently associated with the key
      initialValue - the value to associate the key with if the key was not present in the map yet
      Returns:
      the new value
    • put

      public OptionalInt put(K key, int val)
      Associates the specified value with the specified key in this map.
      Parameters:
      key - the key
      val - the value enum constant yet.
      See Also:
    • set

      public EnumToIntMap<K> set(K key, int val)
      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, int nullValue)
      Adds all entries of the specified map to this map. This method acts as a bridge to fully-generic map implementations.
      Parameters:
      other - the Map whose key-value mappings to copy
      nullValue - the int value to use for null values in the source map
    • toGenericMap

      public EnumMap<K,Integer> toGenericMap()
      Converts this map to a fully-generic version of this map.
      Returns:
      a fully-generic version of this map
    • remove

      public OptionalInt remove(K key)
      Removes the mapping for a key from this map if it is present.
      Parameters:
      key - the key
      Returns:
      an OptionalInt containing the previous value associated with key, or an empty OptionalInt 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. The Set maintains the order of the enum constants, and can be iterated over quickly, but is inefficient for typical set operations such as constains().
      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.extra.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:
    • 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