Class EnumHelpers


  • public final class EnumHelpers
    extends java.lang.Object
    General helpers on Enum types.
    • Constructor Summary

      Constructors 
      Constructor Description
      EnumHelpers()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T extends java.lang.Enum<T>>
      T[]
      indexById​(java.lang.Class<T> enumClass, java.util.function.ToIntFunction<T> getId)
      Indexes the values of the given Enum by a specified Id function.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • EnumHelpers

        public EnumHelpers()
    • Method Detail

      • indexById

        public static <T extends java.lang.Enum<T>> T[] indexById​(java.lang.Class<T> enumClass,
                                                                  java.util.function.ToIntFunction<T> getId)
        Indexes the values of the given Enum by a specified Id function. This method maps getId on an integer interval from 0 to max()+1, and fills in the slots where getId returns a value. Empty slots will contain null.

        Notes:

        • This method will create an array of size max(getId(all-enum-values)), so it could get very large. It is not recommended for sparse Enums (in which case a Map would be better suited).
        • This method has undefined behavior if getId does not return unique values. It does not check, so Enum values that have the same result from getId may not be indexed correctly.
        Type Parameters:
        T - Type of the enum.
        Parameters:
        enumClass - The Enum Class.
        getId - A function that returns an Id for each enum value.
        Returns:
        An array of type T with Enum values indexed by the result of the getId function.