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
ConstructorsConstructorDescriptionEnumToIntMap(Class<K> enumClass) Creates an emptyEnumToIntMapfor the specifiedenumclass.EnumToIntMap(Class<K> enumClass, int initialValue) Creates anEnumToIntMapsaturated with all enum constants of the specified enum class.EnumToIntMap(Class<K> enumClass, ToIntFunction<K> initializer) Creates anEnumToIntMapsaturated with all enum constants of the specified enum class.EnumToIntMap(EnumToIntMap<K> other) Instantiates a newEnumToIntMapwith the same key-value mappings as the specifiedEnumToIntMap. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all mappings from this map.intcomputeIfPresent(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.booleancontainsKey(K key) Returnstrueif this map contains a mapping for the specified key.booleancontainsValue(int val) Returnstrueif this map contains the specified value.entrySet()Returns a Set view of the mappings contained in this map.booleanReturnstrueif the argument is anEnumToIntMapfor the same enum class and if it contains the same key-value mappings.voidforEach(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.Returns anOptionalIntcontaining the value to which the specified enum constant is mapped, or an emptyOptionalIntif this map contains no mapping for the key.intgetOrDefault(K key, int dfault) Returns the value associated with the specified enum constant ordfaultif the map did not contain an entry for the specified enum constant.inthashCode()Returns anIntListcontaining the values of this map.booleanisEmpty()Returnstrueif this map contains no key-value mappings,falseotherwise.keySet()Returns aSetview of the keys contained in this map.keyType()Returns the type of the keys in this map.Associates the specified value with the specified key in this map.voidAdds all entries of the specified map to this map.voidputAll(EnumToIntMap<K> other) Adds all entries of the specified map to this map, overwriting any previous values.Removes the mapping for a key from this map if it is present.Likeput(), but provides a fluent API for adding entries to the map.intsize()Returns the number of key-value mappings in this map.Converts this map to a fully-generic version of this map.toString()values()Returns aCollectionview of the values contained in this map.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.klojang.check.extra.Emptyable
isDeepNotEmpty
-
Constructor Details
-
EnumToIntMap
-
EnumToIntMap
Creates anEnumToIntMapsaturated 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 classinitialValue- the initial value for all enum constants
-
EnumToIntMap
Creates anEnumToIntMapsaturated 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 classinitializer- a function called to initialize the map's values
-
EnumToIntMap
Instantiates a newEnumToIntMapwith the same key-value mappings as the specifiedEnumToIntMap.- Parameters:
other- theEnumToIntMapwhose key-value mappings to copy
-
-
Method Details
-
keyType
-
containsKey
Returnstrueif 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) Returnstrueif this map contains the specified value.- Parameters:
val- the value to search for- Returns:
- whether the map contains the value
- See Also:
-
get
Returns anOptionalIntcontaining the value to which the specified enum constant is mapped, or an emptyOptionalIntif this map contains no mapping for the key. (A regularMapwould returnnullin 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
Returns the value associated with the specified enum constant ordfaultif 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
Computes a new value for the specified key if present, else associates the key with the specified initial value.- Parameters:
key- the keyoperator- the function to apply to the value currently associated with the keyinitialValue- the value to associate the key with if the key was not present in the map yet- Returns:
- the new value
-
put
Associates the specified value with the specified key in this map.- Parameters:
key- the keyval- the value enum constant yet.- See Also:
-
set
Likeput(), but provides a fluent API for adding entries to the map.- Parameters:
key- the keyval- the value- Returns:
- this instance
-
putAll
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. AnIllegalArgumentExceptionis thrown if it does.- Parameters:
other- theEnumToIntMapwhose key-value mappings to copy
-
putAll
Adds all entries of the specified map to this map. This method acts as a bridge to fully-generic map implementations.- Parameters:
other- theMapwhose key-value mappings to copynullValue- theintvalue to use fornullvalues in the source map
-
toGenericMap
-
remove
Removes the mapping for a key from this map if it is present.- Parameters:
key- the key- Returns:
- an
OptionalIntcontaining the previous value associated with key, or an emptyOptionalIntif there was no mapping for key. - See Also:
-
keySet
Returns aSetview of the keys contained in this map. TheSetmaintains the order of the enum constants, and can be iterated over quickly, but is inefficient for typical set operations such asconstains().- Returns:
- a Set view of the keys contained in this map
- See Also:
-
values
Returns aCollectionview of the values contained in this map.- Returns:
- a
Collectionview of the values contained in this map - See Also:
-
intValues
Returns anIntListcontaining the values of this map.- Returns:
- an
IntListcontaining the values of this map
-
entrySet
-
isEmpty
public boolean isEmpty()Returnstrueif this map contains no key-value mappings,falseotherwise.- Specified by:
isEmptyin interfaceorg.klojang.check.extra.Emptyable- Returns:
trueif this map contains no key-value mappings,falseotherwise- See Also:
-
forEach
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
-
hashCode
-
toString
-