Class EnumUtils

java.lang.Object
com.blackrook.gloop.opengl.struct.EnumUtils

public final class EnumUtils extends Object
Some utility methods for Enums.
Author:
Matthew Tropiano
  • Constructor Details

    • EnumUtils

      public EnumUtils()
  • Method Details

    • getEnumInstance

      public static <T extends Enum<T>> T getEnumInstance(String value, Class<T> enumClass)
      Returns the enum instance of a class given class and name, or null if not a valid name. If value is null, this returns null.
      Type Parameters:
      T - the Enum object type.
      Parameters:
      value - the value to search for.
      enumClass - the Enum class to inspect.
      Returns:
      the enum value or null if the target does not exist.
    • addToMap

      public static <K, E extends Enum<E>> void addToMap(Map<K, ? super E> map, Class<E> enumClass, BiFunction<Integer,E,K> keyProviderFunc)
      Adds all values in an enum to an existing ordinal map.
      Type Parameters:
      K - the key type.
      E - an Enum type.
      Parameters:
      map - the target map.
      enumClass - the Enum class.
      keyProviderFunc - the function that fetches the corresponding id to use for the provided enum value. First parameter is the ordinal from Enum.ordinal().
    • addToOrdinalMap

      public static <E extends Enum<E>> void addToOrdinalMap(Map<Integer, ? super E> map, Class<E> enumClass)
      Adds all values in an enum to an existing ordinal map.
      Type Parameters:
      E - an Enum type.
      Parameters:
      map - the target map.
      enumClass - the Enum class.
    • addToOrdinalMap

      public static <E extends Enum<E>> void addToOrdinalMap(Map<Integer, ? super E> map, Class<E> enumClass, int offset)
      Adds all values in an enum to an existing ordinal map.
      Type Parameters:
      E - an Enum type.
      Parameters:
      map - the target map.
      enumClass - the Enum class.
      offset - the offset to add to each ordinal.
    • addToNameMap

      public static <E extends Enum<E>> void addToNameMap(Map<String, ? super E> map, Class<E> enumClass)
      Adds all values in an enum to an existing name map.
      Type Parameters:
      E - an Enum type.
      Parameters:
      map - the target map.
      enumClass - the Enum class.
    • createMap

      public static <C extends Comparable<C>, E extends Enum<E>> SortedMap<C,E> createMap(Class<E> enumClass, BiFunction<Integer,E,C> keyProviderFunc)
      Turns a set of enums into a map of some kind of key to enum.
      Type Parameters:
      C - the key type; must be Comparable.
      E - an Enum type.
      Parameters:
      enumClass - the Enum class.
      keyProviderFunc - the function that fetches the corresponding id to use for the provided enum value. First parameter is the ordinal from Enum.ordinal().
      Returns:
      the resultant, unmodifiable map.
    • createIntegerMap

      public static <E extends Enum<E>> SortedMap<Integer,E> createIntegerMap(Class<E> enumClass, BiFunction<Integer,E,Integer> idProviderFunc)
      Turns a set of enums into a map of integer id to enum.
      Type Parameters:
      E - an Enum type.
      Parameters:
      enumClass - the Enum class.
      idProviderFunc - the function that fetches the corresponding id to use for the provided enum value. First parameter is the ordinal from Enum.ordinal().
      Returns:
      the resultant, unmodifiable map.
    • createOrdinalMap

      public static <E extends Enum<E>> SortedMap<Integer,E> createOrdinalMap(Class<E> enumClass)
      Turns a set of enums into a map of ordinal to enum value.
      Type Parameters:
      E - an Enum type.
      Parameters:
      enumClass - the Enum class.
      Returns:
      the resultant, unmodifiable map.
    • createOrdinalMap

      public static <E extends Enum<E>> SortedMap<Integer,E> createOrdinalMap(Class<E> enumClass, int offset)
      Turns a set of enums into a map of ordinal to enum value.

      Can optionally have an offset to add to them (if offset is 1, each enum maps to enum.ordinal() + 1).

      Type Parameters:
      E - an Enum type.
      Parameters:
      enumClass - the Enum class.
      offset - the offset to add to each ordinal.
      Returns:
      the resultant, unmodifiable map.
    • createNameMap

      public static <E extends Enum<E>> SortedMap<String,E> createNameMap(Class<E> enumClass)
      Turns a set of enums into a map of String to enum value. The Strings used are the enum's Enum.name().
      Type Parameters:
      E - an Enum type.
      Parameters:
      enumClass - the Enum class.
      Returns:
      the resultant, unmodifiable map.
    • createCaseInsensitiveNameMap

      public static <E extends Enum<E>> SortedMap<String,E> createCaseInsensitiveNameMap(Class<E> enumClass)
      Turns a set of enums into a map of case-insensitive-resolving Strings to enum value. The Strings used are the enum's Enum.name().
      Type Parameters:
      E - an Enum type.
      Parameters:
      enumClass - the Enum class.
      Returns:
      the resultant, unmodifiable map.
    • createCaseInsensitiveEnumMap

      public static <E extends Enum<E>> SortedMap<String,E> createCaseInsensitiveEnumMap(Class<E> enumClass, BiFunction<Integer,E,String> nameProviderFunc)
      Turns a set of enums into a map of case-insensitive-resolving Strings to enum value.
      Type Parameters:
      E - an Enum type.
      Parameters:
      enumClass - the Enum class.
      nameProviderFunc - the function that fetches the corresponding string to use for the provided enum value. First parameter is the ordinal from Enum.ordinal().
      Returns:
      the resultant, unmodifiable map.