Class EnumUtil

java.lang.Object
com.github.twitch4j.util.EnumUtil

public final class EnumUtil extends Object
  • Method Details

    • buildMapping

      public static <E extends Enum<E>, K, V> Map<K,V> buildMapping(@NotNull E[] values, @NotNull @NotNull Function<E,K> keySelector, @NotNull @NotNull Function<E,V> valueMapper)
      Transforms enum values to a key-value map based on the specified functions.
      Type Parameters:
      E - the type of enum
      K - the type of keys
      V - the type of values
      Parameters:
      values - the values within an enum to be transformed
      keySelector - a function that derives a key associated to an enum entry
      valueMapper - a function that yields a value for a given enum entry
      Returns:
      a Map of key-value associations from transforming the specified enum entries
      API Note:
      The key derivation function should not yield collisions (even though we do not throw an exception upon a duplicate key).
      Implementation Requirements:
      The returned map is unmodifiable. The underlying map is HashMap so that null keys (and values) are supported.
      Implementation Note:
      Enum#values is used instead of Class.getEnumConstants() to avoid reflection overhead.
    • buildMapping

      public static <E extends Enum<E>, K> Map<K,E> buildMapping(@NotNull E[] values, @NotNull @NotNull Function<E,K> keySelector)
      Creates a mapping of enum values via an arbitrary key derivation function.
      Type Parameters:
      E - the type of enum
      K - the type of keys
      Parameters:
      values - the values within an enum to be mapped
      keySelector - a function that derives a key associated to an enum value
      Returns:
      the mapping of enum values by the derived keys
      See Also:
    • buildMapping

      public static <E extends Enum<E>> Map<String,E> buildMapping(@NotNull E[] values)
      Creates a mapping of enum values by their string representation.
      Type Parameters:
      E - the type of enum
      Parameters:
      values - the values within an enum to be mapped
      Returns:
      a mapping of Enum.toString() to the enum value itself
      See Also: