Class EnumIntMap

java.lang.Object
com.github.tommyettinger.ds.EnumIntMap
All Implemented Interfaces:
Iterable<EnumIntMap.Entry>
Direct Known Subclasses:
EnumIntOrderedMap

public class EnumIntMap extends Object implements Iterable<EnumIntMap.Entry>
An unordered map where the keys are Enums and values are primitive ints. Null keys are not allowed. Unlike EnumMap, this does not require a Class at construction time, which can be useful for serialization purposes. No allocation is done unless this is changing its table size and/or key universe.
This class never actually hashes keys in its primary operations (get(), put(), remove(), containsKey(), etc.), since it can rely on keys having an Enum type, and so having Enum.ordinal() available. The ordinal allows constant-time access to a guaranteed-unique int that will always be non-negative and less than the size of the key universe. The table of possible values always starts sized to fit exactly as many values as there are keys in the key universe.
The key universe is an important concept here; it is simply an array of all possible Enum values the EnumIntMap can use as keys, in the specific order they are declared. You almost always get a key universe by calling MyEnum.values(), but you can also use Class.getEnumConstants() for an Enum class. You can and generally should reuse key universes in order to avoid allocations and/or save memory; the constructor EnumIntMap(Enum[]) (with no values given) creates an empty EnumIntMap with a given key universe. If you need to use the zero-argument constructor, you can, and the key universe will be obtained from the first key placed into the EnumIntMap. You can also set the key universe with clearToUniverse(Enum[]), in the process of clearing the map.
This class tries to be as compatible as possible with EnumMap while using primitive keys, though this expands on that where possible.
  • Field Details

    • keys

      protected EnumSet keys
    • valueTable

      protected int[] valueTable
    • entries1

      protected transient EnumIntMap.Entries entries1
    • entries2

      protected transient EnumIntMap.Entries entries2
    • values1

      protected transient EnumIntMap.Values values1
    • values2

      protected transient EnumIntMap.Values values2
    • keys1

      protected transient EnumIntMap.Keys keys1
    • keys2

      protected transient EnumIntMap.Keys keys2
    • defaultValue

      public int defaultValue
      Returned by get(Object) when no value exists for the given key, as well as some other methods to indicate that no value in the Map could be returned. Defaults to null.
  • Constructor Details

    • EnumIntMap

      public EnumIntMap()
      Empty constructor; using this will postpone creating the key universe and allocating the value table until put(java.lang.Enum<?>, int) is first called (potentially indirectly). You can also use clearToUniverse(java.lang.Enum<?>[]) to set the key universe and value table.
    • EnumIntMap

      public EnumIntMap(Enum<?>[] universe)
      Initializes this map so that it has exactly enough capacity as needed to contain each Enum constant defined in universe, assuming universe stores every possible constant in one Enum type. This map will start empty. You almost always obtain universe from calling values() on an Enum type, and you can share one reference to one Enum array across many EnumIntMap instances if you don't modify the shared array. Sharing the same universe helps save some memory if you have (very) many EnumIntMap instances.
      Parameters:
      universe - almost always, the result of calling values() on an Enum type; used directly, not copied
    • EnumIntMap

      public EnumIntMap(Class<? extends Enum<?>> universeClass)
      Initializes this map so that it has exactly enough capacity as needed to contain each Enum constant defined by the Class universeClass, assuming universeClass is non-null. This simply calls EnumIntMap(Enum[]) for convenience. Note that this constructor allocates a new array of Enum constants each time it is called, where if you use EnumIntMap(Enum[]), you can reuse an unmodified array to reduce allocations.
      Parameters:
      universeClass - the Class of an Enum type that defines the universe of valid Enum items this can hold
    • EnumIntMap

      public EnumIntMap(EnumIntMap map)
      Creates a new map identical to the specified EnumIntMap. This will share a key universe with the given EnumIntMap, if non-null.
      Parameters:
      map - an EnumIntMap to copy
    • EnumIntMap

      public EnumIntMap(Enum<?>[] keys, int[] values)
      Given two side-by-side arrays, one of Enum keys, one of int values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller array.
      Parameters:
      keys - an array of Enum keys
      values - an array of int values
    • EnumIntMap

      public EnumIntMap(Collection<? extends Enum<?>> keys, PrimitiveCollection.OfInt values)
      Given two side-by-side collections, one of Enum keys, one of int values, this constructs a map and inserts each pair of key and value into it. If keys and values have different lengths, this only uses the length of the smaller collection.
      Parameters:
      keys - a Collection of Enum keys
      values - a PrimitiveCollection of int values
  • Method Details

    • putAll

      public void putAll(Collection<? extends Enum<?>> keys, PrimitiveCollection.OfInt values)
      Given two side-by-side collections, one of Enum keys, one of int values, this inserts each pair of key and value into this map with put().
      Parameters:
      keys - a Collection of Enum keys
      values - a PrimitiveCollection of int values
    • put

      public int put(Enum<?> key, int value)
      Returns the old value associated with the specified key, or this map's defaultValue if there was no prior value. If this EnumIntMap does not yet have a key universe and/or value table, this gets the key universe from key and uses it from now on for this EnumIntMap.
      Parameters:
      key - the Enum key to try to place into this EnumIntMap
      value - the int value to associate with key
      Returns:
      the previous value associated with key, or getDefaultValue() if the given key was not present
    • putOrDefault

      public int putOrDefault(Enum<?> key, int value, int defaultValue)
      Acts like put(Enum, int), but uses the specified defaultValue instead of the default value for this EnumIntMap.
      Parameters:
      key - the Enum key to try to place into this EnumIntMap
      value - the int value to associate with key
      defaultValue - the int value to return if key was not already present
      Returns:
      the previous value associated with key, or the given defaultValue if the given key was not present
    • putAll

      public void putAll(EnumIntMap map)
      Puts every key-value pair in the given map into this, with the values from the given map overwriting the previous values if two keys are identical. If this EnumIntMap doesn't yet have a key universe, it will now share a key universe with the given map. Even if the given EnumIntMap is empty, it can still be used to obtain a key universe for this EnumIntMap (assuming it has a key universe).
      Parameters:
      map - another EnumIntMap with an equivalent key universe
    • putAll

      public void putAll(Enum<?>[] keys, int[] values)
      Given two side-by-side arrays, one of keys, one of values, this inserts each pair of key and value into this map with put(). Delegates to putAll(Enum[], int, int[], int, int).
      Parameters:
      keys - an array of keys
      values - an array of values
    • putAll

      public void putAll(Enum<?>[] keys, int[] values, int length)
      Given two side-by-side arrays, one of keys, one of values, this inserts each pair of key and value into this map with put(). Delegates to putAll(Enum[], int, int[], int, int).
      Parameters:
      keys - an array of keys
      values - an array of values
      length - how many items from keys and values to insert, at-most
    • putAll

      public void putAll(Enum<?>[] keys, int keyOffset, int[] values, int valueOffset, int length)
      Given two side-by-side arrays, one of keys, one of values, this inserts each pair of key and value into this map with put(Enum, int).
      Parameters:
      keys - an array of keys
      keyOffset - the first index in keys to insert
      values - an array of values
      valueOffset - the first index in values to insert
      length - how many items from keys and values to insert, at-most
    • get

      public int get(Object key)
      Returns the value for the specified key, or defaultValue if the key is not in the map. Note that defaultValue is often null, which is also a valid value that can be assigned to a legitimate key. Checking that the result of this method is null does not guarantee that the key is not present.
      Parameters:
      key - a non-null Object that should always be an Enum
    • getOrDefault

      public int getOrDefault(Object key, int defaultValue)
      Returns the value for the specified key, or the given default value if the key is not in the map.
    • remove

      public int remove(Object key)
    • putAll

      public void putAll(ObjectIntMap<Enum<?>> m)
      Copies all the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
      Note that putAll(EnumIntMap) is more specific and can be more efficient by using the internal details of this class.
      Parameters:
      m - mappings to be stored in this map
      Throws:
      UnsupportedOperationException - if the putAll operation is not supported by this map
      ClassCastException - if the class of a key or value in the specified map prevents it from being stored in this map
      NullPointerException - if the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys or values
      IllegalArgumentException - if some property of a key or value in the specified map prevents it from being stored in this map
    • getAndIncrement

      public int getAndIncrement(Enum<?> key, int defaultValue, int increment)
      Returns the key's current value and increments the stored value. If the key is not in the map, defaultValue + increment is put into the map and defaultValue is returned.
    • notEmpty

      public boolean notEmpty()
      Returns true if the map has one or more items.
    • size

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

      public boolean isEmpty()
      Returns true if the map is empty.
    • getDefaultValue

      public int getDefaultValue()
      Gets the default value, a int which is returned by get(Object) if the key is not found. If not changed, the default value is 0.
      Returns:
      the current default value
    • setDefaultValue

      public void setDefaultValue(int defaultValue)
      Sets the default value, a int which is returned by get(Object) if the key is not found. If not changed, the default value is 0. Note that getOrDefault(Object, int) is also available, which allows specifying a "not-found" value per-call.
      Parameters:
      defaultValue - may be any int; should usually be one that doesn't occur as a typical value
    • clear

      public void clear()
      Removes all the elements from this map. The map will be empty after this call returns. This does not change the universe of possible Enum items this can hold.
    • clearToUniverse

      public void clearToUniverse(Enum<?>[] universe)
      Removes all the elements from this map and can reset the universe of possible Enum items this can hold. The map will be empty after this call returns. This changes the universe of possible Enum items this can hold to match universe. If universe is null, this resets this map to the state it would have after EnumIntMap() was called. If the table this would need is the same size as or smaller than the current table (such as if universe is the same as the universe here), this will not allocate, but will still clear any items this holds and will set the universe to the given one. Otherwise, this allocates and uses a new table of a larger size, with nothing in it, and uses the given universe. This always uses universe directly, without copying.
      This can be useful to allow an EnumIntMap that was created with EnumIntMap() to share a universe with other EnumIntMaps.
      Parameters:
      universe - the universe of possible Enum items this can hold; almost always produced by values() on an Enum
    • clearToUniverse

      public void clearToUniverse(Class<? extends Enum<?>> universe)
      Removes all the elements from this map and can reset the universe of possible Enum items this can hold. The map will be empty after this call returns. This changes the universe of possible Enum items this can hold to match the Enum constants in universe. If universe is null, this resets this map to the state it would have after EnumIntMap() was called. If the table this would need is the same size as or smaller than the current table (such as if universe is the same as the universe here), this will not allocate, but will still clear any items this holds and will set the universe to the given one. Otherwise, this allocates and uses a new table of a larger size, with nothing in it, and uses the given universe. This calls Class.getEnumConstants() if universe is non-null, which allocates a new array.
      You may want to prefer calling clearToUniverse(Enum[]) (the overload that takes an array), because it can be used to share one universe array between many EnumIntMap instances. This overload, given a Class, has to call Class.getEnumConstants() and thus allocate a new array each time this is called.
      Parameters:
      universe - the Class of an Enum type that stores the universe of possible Enum items this can hold
    • getUniverse

      public Enum<?>[] getUniverse()
      Gets the current key universe; this is a technically-mutable array, but should never be modified. To set the universe on an existing EnumIntMap (with existing contents), you can use clearToUniverse(Enum[]). If an EnumIntMap has not been initialized, just adding a key will set the key universe to match the given item.
      Returns:
      the current key universe
    • containsValue

      public boolean containsValue(int value)
      Returns true if the specified value is in the map. Note this traverses the entire map and compares every value, which may be an expensive operation.
    • containsKey

      public boolean containsKey(Object key)
    • findKey

      public Enum<?> findKey(int value)
      Returns the key for the specified value, or null if it is not in the map. Note this traverses the entire map and compares every value, which may be an expensive operation.
      Returns:
      the corresponding Enum if the value was found, or null otherwise
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(String entrySeparator)
      Delegates to toString(String, boolean) with the given entrySeparator and without braces. This is different from toString(), which includes braces by default.
      Parameters:
      entrySeparator - how to separate entries, such as ", "
      Returns:
      a new String representing this map
    • toString

      public String toString(String entrySeparator, boolean braces)
    • toString

      public String toString(String entrySeparator, String keyValueSeparator, boolean braces, Appender<Enum<?>> keyAppender, IntAppender valueAppender)
      Makes a String from the contents of this ObjectObjectMap, but uses the given Appender and IntAppender to convert each key and each value to a customizable representation and append them to a temporary StringBuilder. To use the default toString representation, you can use Appender::append as an appender, or to use the readable Enum Enum.name(), use Appender.ENUM_NAME_APPENDER. Use IntAppender.DEFAULT or IntAppender.READABLE for human-readable or source-code-readable results, respectively.
      Parameters:
      entrySeparator - how to separate entries, such as ", "
      keyValueSeparator - how to separate each key from its value, such as "=" or ":"
      braces - true to wrap the output in curly braces, or false to omit them
      keyAppender - a function that takes a StringBuilder and an Enum, and returns the modified StringBuilder
      valueAppender - a function that takes a StringBuilder and a int, and returns the modified StringBuilder
      Returns:
      a new String representing this map
    • appendTo

      public StringBuilder appendTo(StringBuilder sb, String entrySeparator, boolean braces)
    • appendTo

      public StringBuilder appendTo(StringBuilder sb, String entrySeparator, String keyValueSeparator, boolean braces, Appender<Enum<?>> keyAppender, IntAppender valueAppender)
      Appends to a StringBuilder from the contents of this ObjectObjectMap, but uses the given Appender and Appender to convert each key and each value to a customizable representation and append them to a StringBuilder. To use the default toString representation, you can use Appender::append as an appender, or to use the readable Enum Enum.name(), use Appender.ENUM_NAME_APPENDER. Use IntAppender.DEFAULT or IntAppender.READABLE for human-readable or source-code-readable results, respectively.
      Parameters:
      sb - a StringBuilder that this can append to
      entrySeparator - how to separate entries, such as ", "
      keyValueSeparator - how to separate each key from its value, such as "=" or ":"
      braces - true to wrap the output in curly braces, or false to omit them
      keyAppender - a function that takes a StringBuilder and an Enum, and returns the modified StringBuilder
      valueAppender - a function that takes a StringBuilder and a int, and returns the modified StringBuilder
      Returns:
      sb, with the appended keys and values of this map
    • replace

      public int replace(Enum<?> key, int value)
    • computeIfAbsent

      public int computeIfAbsent(Enum<?> key, com.github.tommyettinger.function.ObjToIntFunction<? super Enum<?>> mappingFunction)
    • remove

      public boolean remove(Object key, int value)
    • combine

      public int combine(Enum<?> key, int value, com.github.tommyettinger.function.IntIntToIntBiFunction remappingFunction)
      Just like Map's merge() default method, but this doesn't use Java 8 APIs (so it should work on RoboVM), this uses primitive values, and this won't remove entries if the remappingFunction returns null (because that isn't possible with primitive types). This uses a functional interface from Funderby.
      Parameters:
      key - key with which the resulting value is to be associated
      value - the value to be merged with the existing value associated with the key or, if no existing value is associated with the key, to be associated with the key
      remappingFunction - given a int from this and the int value, this should return what int to use
      Returns:
      the value now associated with key
    • combine

      public void combine(EnumIntMap other, com.github.tommyettinger.function.IntIntToIntBiFunction remappingFunction)
      Simply calls combine(Enum, int, IntIntToIntBiFunction) on this map using every key-value pair in other. If other isn't empty, calling this will probably modify this map, though this depends on the remappingFunction.
      Parameters:
      other - a non-null ObjectIntMap (or subclass) with a compatible key type
      remappingFunction - given a int value from this and a value from other, this should return what int to use
    • iterator

      public EnumIntMap.EntryIterator iterator()
      Reuses the iterator of the reused EnumIntMap.Entries produced by entrySet(); does not permit nested iteration. Iterate over Entries(EnumIntMap) if you need nested or multithreaded iteration. You can remove an Entry from this EnumIntMap using this Iterator.
      Specified by:
      iterator in interface Iterable<EnumIntMap.Entry>
      Returns:
      an Iterator over Map.Entry key-value pairs; remove is supported.
    • keySet

      public EnumIntMap.Keys keySet()
      Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

      Note that the same Collection instance is returned each time this method is called. Use the EnumIntMap.Keys constructor for nested or multithreaded iteration.

      Returns:
      a set view of the keys contained in this map
    • values

      public EnumIntMap.Values values()
      Returns a PrimitiveCollection of the values in the map. Remove is supported. Note that the same PrimitiveCollection instance is returned each time this method is called. Use the EnumIntMap.Values constructor for nested or multithreaded iteration.
      Returns:
      a PrimitiveCollection of int values
    • entrySet

      public EnumIntMap.Entries entrySet()
      Returns a Set of Map.Entry, containing the entries in the map. Remove is supported by the Set's iterator. Note that the same iterator instance is returned each time this method is called. Use the EnumIntMap.Entries constructor for nested or multithreaded iteration.
      Returns:
      a Set of EnumIntMap.Entry key-value pairs
    • with

      public static EnumIntMap with()
      Constructs an empty map. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.
      Returns:
      a new map containing nothing
    • with

      public static EnumIntMap with(Enum<?> key0, Number value0)
      Constructs a single-entry map given one key and one value. This is mostly useful as an optimization for with(Enum, Number, Object...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number value to a primitive int, regardless of which Number type was used.
      Parameters:
      key0 - the first and only Enum key
      value0 - the first and only value; will be converted to primitive int
      Returns:
      a new map containing just the entry mapping key0 to value0
    • with

      public static EnumIntMap with(Enum<?> key0, Number value0, Enum<?> key1, Number value1)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Enum, Number, Object...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number values to primitive ints, regardless of which Number type was used.
      Parameters:
      key0 - an Enum key
      value0 - a Number for a value; will be converted to primitive int
      key1 - an Enum key
      value1 - a Number for a value; will be converted to primitive int
      Returns:
      a new map containing the given key-value pairs
    • with

      public static EnumIntMap with(Enum<?> key0, Number value0, Enum<?> key1, Number value1, Enum<?> key2, Number value2)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Enum, Number, Object...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number values to primitive ints, regardless of which Number type was used.
      Parameters:
      key0 - an Enum key
      value0 - a Number for a value; will be converted to primitive int
      key1 - an Enum key
      value1 - a Number for a value; will be converted to primitive int
      key2 - an Enum key
      value2 - a Number for a value; will be converted to primitive int
      Returns:
      a new map containing the given key-value pairs
    • with

      public static EnumIntMap with(Enum<?> key0, Number value0, Enum<?> key1, Number value1, Enum<?> key2, Number value2, Enum<?> key3, Number value3)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Enum, Number, Object...) when there's no "rest" of the keys or values. Like the more-argument with(), this will convert its Number values to primitive ints, regardless of which Number type was used.
      Parameters:
      key0 - an Enum key
      value0 - a Number for a value; will be converted to primitive int
      key1 - an Enum key
      value1 - a Number for a value; will be converted to primitive int
      key2 - an Enum key
      value2 - a Number for a value; will be converted to primitive int
      key3 - an Enum key
      value3 - a Number for a value; will be converted to primitive int
      Returns:
      a new map containing the given key-value pairs
    • with

      public static EnumIntMap with(Enum<?> key0, Number value0, Object... rest)
      Constructs a map given alternating keys and values. This can be useful in some code-generation scenarios, or when you want to make a map conveniently by-hand and have it populated at the start. You can also use EnumIntMap(Enum[], int[]), which takes all keys and then all values. This needs all keys to be Enum constants. All values must be some type of boxed Number, such as Integer or Double, and will be converted to primitive ints. Any keys that don't have Enum as their type or values that aren't Numbers have that entry skipped.
      Parameters:
      key0 - the first Enum key
      value0 - the first value; will be converted to primitive int
      rest - an array or varargs of alternating Enum, Number, Enum, Number... elements
      Returns:
      a new map containing the given keys and values
    • putPairs

      public void putPairs(Object... pairs)
      Attempts to put alternating key-value pairs into this map, drawing a key, then a value from pairs, then another key, another value, and so on until another pair cannot be drawn. All values must be some type of boxed Number, such as Integer or Double, and will be converted to primitive ints. Any keys that don't have some Enum as their type or values that aren't Numbers have that entry skipped.
      If any item in pairs cannot be cast to the Enum or Number type for its position in the arguments, that pair is ignored and neither that key nor value is put into the map. If any key is null, that pair is ignored, as well. If pairs is an Object array that is null, the entire call to putPairs() is ignored. If the length of pairs is odd, the last item (which will be unpaired) is ignored.
      Parameters:
      pairs - an array or varargs of alternating Enum, Number, Enum, Number... elements
    • putLegible

      public void putLegible(String str, PartialParser<Enum<?>> keyParser)
      Adds items to this map drawn from the result of toString(String) or appendTo(StringBuilder, String, boolean). Every key-value pair should be separated by ", ", and every key should be followed by "=" before the value (which toString() does). A PartialParser will be used to parse keys from sections of str, and values are parsed with Base.readInt(CharSequence, int, int). Usually, keyParser is produced by PartialParser.enumParser(ObjToObjFunction). Any brackets inside the given range of characters will ruin the parsing, so increase offset by 1 and reduce length by 2 if the original String had brackets added to it.
      Parameters:
      str - a String containing parseable text
      keyParser - a PartialParser that returns a Enum<?> key from a section of str, typically produced by PartialParser.enumParser(ObjToObjFunction)
    • putLegible

      public void putLegible(String str, String entrySeparator, PartialParser<Enum<?>> keyParser)
      Adds items to this map drawn from the result of toString(String) or appendTo(StringBuilder, String, boolean). Every key-value pair should be separated by entrySeparator, and every key should be followed by "=" before the value (which toString(String) does). A PartialParser will be used to parse keys from sections of str, and values are parsed with Base.readInt(CharSequence, int, int). Usually, keyParser is produced by PartialParser.enumParser(ObjToObjFunction). Any brackets inside the given range of characters will ruin the parsing, so increase offset by 1 and reduce length by 2 if the original String had brackets added to it.
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyParser - a PartialParser that returns a Enum<?> key from a section of str, typically produced by PartialParser.enumParser(ObjToObjFunction)
    • putLegible

      public void putLegible(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> keyParser)
      Adds items to this map drawn from the result of toString(String) or appendTo(StringBuilder, String, String, boolean, Appender, IntAppender). A PartialParser will be used to parse keys from sections of str, and values are parsed with Base.readInt(CharSequence, int, int). Usually, keyParser is produced by PartialParser.enumParser(ObjToObjFunction). Any brackets inside the given range of characters will ruin the parsing, so increase offset by 1 and reduce length by 2 if the original String had brackets added to it.
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      keyParser - a PartialParser that returns a Enum<?> key from a section of str, typically produced by PartialParser.enumParser(ObjToObjFunction)
    • putLegible

      public void putLegible(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> keyParser, int offset, int length)
      Puts key-value pairs into this map drawn from the result of toString(String) or appendTo(StringBuilder, String, String, boolean, Appender, IntAppender). A PartialParser will be used to parse keys from sections of str, and values are parsed with Base.readInt(CharSequence, int, int). Usually, keyParser is produced by PartialParser.enumParser(ObjToObjFunction). Any brackets inside the given range of characters will ruin the parsing, so increase offset by 1 and reduce length by 2 if the original String had brackets added to it.
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      keyParser - a PartialParser that returns a Enum<?> key from a section of str, typically produced by PartialParser.enumParser(ObjToObjFunction)
      offset - the first position to read parseable text from in str
      length - how many chars to read; -1 is treated as maximum length
    • parse

      public static EnumIntMap parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> keyParser)
      Creates a new map by parsing all of str with the given PartialParser for keys, with entries separated by entrySeparator, such as ", " and the keys separated from values by keyValueSeparator, such as "=".
      The keyParser is often produced by PartialParser.enumParser(ObjToObjFunction).
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      keyParser - a PartialParser that returns an Enum key from a section of str
    • parse

      public static EnumIntMap parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> keyParser, boolean brackets)
      Creates a new map by parsing all of str (or if brackets is true, all but the first and last chars) with the given PartialParser for keys, with entries separated by entrySeparator, such as ", " and the keys separated from values by keyValueSeparator, such as "=".
      The keyParser is often produced by PartialParser.enumParser(ObjToObjFunction).
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      keyParser - a PartialParser that returns an Enum key from a section of str
      brackets - if true, the first and last chars in str will be ignored
    • parse

      public static EnumIntMap parse(String str, String entrySeparator, String keyValueSeparator, PartialParser<Enum<?>> keyParser, int offset, int length)
      Creates a new map by parsing the given subrange of str with the given PartialParser for keys, with entries separated by entrySeparator, such as ", " and the keys separated from values by keyValueSeparator, such as "=".
      The keyParser is often produced by PartialParser.enumParser(ObjToObjFunction).
      Parameters:
      str - a String containing parseable text
      entrySeparator - the String separating every key-value pair
      keyValueSeparator - the String separating every key from its corresponding value
      keyParser - a PartialParser that returns an Enum key from a section of str
      offset - the first position to read parseable text from in str
      length - how many chars to read; -1 is treated as maximum length
    • withPrimitive

      public static EnumIntMap withPrimitive()
      Constructs an empty map. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.
      Returns:
      a new map containing nothing
    • withPrimitive

      public static EnumIntMap withPrimitive(Enum<?> key0, int value0)
      Constructs a single-entry map given one key and one value. This is mostly useful as an optimization for with(Enum, Number, Object...) when there's no "rest" of the keys or values. Unlike with(), this takes unboxed int as its value type, and will not box it.
      Parameters:
      key0 - an Enum for a key
      value0 - a int for a value
      Returns:
      a new map containing just the entry mapping key0 to value0
    • withPrimitive

      public static EnumIntMap withPrimitive(Enum<?> key0, int value0, Enum<?> key1, int value1)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Enum, Number, Object...) when there's no "rest" of the keys or values. Unlike with(), this takes unboxed int as its value type, and will not box it.
      Parameters:
      key0 - an Enum key
      value0 - a int for a value
      key1 - an Enum key
      value1 - a int for a value
      Returns:
      a new map containing the given key-value pairs
    • withPrimitive

      public static EnumIntMap withPrimitive(Enum<?> key0, int value0, Enum<?> key1, int value1, Enum<?> key2, int value2)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Enum, Number, Object...) when there's no "rest" of the keys or values. Unlike with(), this takes unboxed int as its value type, and will not box it.
      Parameters:
      key0 - an Enum key
      value0 - a int for a value
      key1 - an Enum key
      value1 - a int for a value
      key2 - an Enum key
      value2 - a int for a value
      Returns:
      a new map containing the given key-value pairs
    • withPrimitive

      public static EnumIntMap withPrimitive(Enum<?> key0, int value0, Enum<?> key1, int value1, Enum<?> key2, int value2, Enum<?> key3, int value3)
      Constructs a map given alternating keys and values. This is mostly useful as an optimization for with(Enum, Number, Object...) when there's no "rest" of the keys or values. Unlike with(), this takes unboxed int as its value type, and will not box it.
      Parameters:
      key0 - an Enum key
      value0 - a int for a value
      key1 - an Enum key
      value1 - a int for a value
      key2 - an Enum key
      value2 - a int for a value
      key3 - an Enum key
      value3 - a int for a value
      Returns:
      a new map containing the given key-value pairs
    • of

      public static EnumIntMap of()
      Constructs an empty map. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have. This is an alias for with().
      Returns:
      a new map containing nothing
    • of

      public static EnumIntMap of(Enum<?> key0, Number value0, Object... rest)
      Constructs a map given alternating keys and values. This can be useful in some code-generation scenarios, or when you want to make a map conveniently by-hand and have it populated at the start. You can also use EnumIntMap(Enum[], int[]), which takes all keys and then all values. This needs all keys to be Enum constants. All values must be some type of boxed Number, such as Integer or Double, and will be converted to primitive ints. Any keys that don't have Enum as their type or values that aren't Numbers have that entry skipped. This is an alias for with(Enum, Number, Object...).
      Parameters:
      key0 - the first Enum key
      value0 - the first value; will be converted to primitive int
      rest - an array or varargs of alternating Enum, Number, Enum, Number... elements
      Returns:
      a new map containing the given keys and values