Class EnumHelper


  • @Immutable
    public final class EnumHelper
    extends Object
    Some enum utility methods.
    Author:
    Philip Helger
    • Method Detail

      • getFromIDOrNull

        @Nullable
        public static <KEYTYPE,​ENUMTYPE extends Enum<ENUMTYPE> & IHasID<KEYTYPE>> ENUMTYPE getFromIDOrNull​(@Nonnull
                                                                                                                 Class<ENUMTYPE> aClass,
                                                                                                                 @Nullable
                                                                                                                 KEYTYPE aID)
        Get the enum value with the passed ID
        Type Parameters:
        KEYTYPE - The ID type
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        aID - The ID to search
        Returns:
        null if no enum item with the given ID is present.
      • getFromIDOrDefault

        @Nullable
        public static <KEYTYPE,​ENUMTYPE extends Enum<ENUMTYPE> & IHasID<KEYTYPE>> ENUMTYPE getFromIDOrDefault​(@Nonnull
                                                                                                                    Class<ENUMTYPE> aClass,
                                                                                                                    @Nullable
                                                                                                                    KEYTYPE aID,
                                                                                                                    @Nullable
                                                                                                                    ENUMTYPE eDefault)
        Get the enum value with the passed ID
        Type Parameters:
        KEYTYPE - The ID type
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        aID - The ID to search
        eDefault - The default value to be returned, if the ID was not found.
        Returns:
        The default parameter if no enum item with the given ID is present.
      • getFromIDOrThrow

        @Nonnull
        public static <KEYTYPE,​ENUMTYPE extends Enum<ENUMTYPE> & IHasID<KEYTYPE>> ENUMTYPE getFromIDOrThrow​(@Nonnull
                                                                                                                  Class<ENUMTYPE> aClass,
                                                                                                                  @Nullable
                                                                                                                  KEYTYPE aID)
        Get the enum value with the passed ID. If no such ID is present, an IllegalArgumentException is thrown.
        Type Parameters:
        KEYTYPE - The ID type
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        aID - The ID to search
        Returns:
        The enum item with the given ID. Never null.
        Throws:
        IllegalArgumentException - if no enum item with the given ID is present
      • getFromIDCaseInsensitiveOrNull

        @Nullable
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasID<String>> ENUMTYPE getFromIDCaseInsensitiveOrNull​(@Nonnull
                                                                                                                 Class<ENUMTYPE> aClass,
                                                                                                                 @Nullable
                                                                                                                 String sID)
        Get the enum value with the passed string ID case insensitive
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        sID - The ID to search
        Returns:
        null if no enum item with the given ID is present.
      • getFromIDCaseInsensitiveOrDefault

        @Nullable
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasID<String>> ENUMTYPE getFromIDCaseInsensitiveOrDefault​(@Nonnull
                                                                                                                    Class<ENUMTYPE> aClass,
                                                                                                                    @Nullable
                                                                                                                    String sID,
                                                                                                                    @Nullable
                                                                                                                    ENUMTYPE eDefault)
        Get the enum value with the passed string ID case insensitive
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        sID - The ID to search
        eDefault - The default value to be returned, if the ID was not found.
        Returns:
        The default parameter if no enum item with the given ID is present.
      • getFromIDCaseInsensitiveOrThrow

        @Nonnull
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasID<String>> ENUMTYPE getFromIDCaseInsensitiveOrThrow​(@Nonnull
                                                                                                                  Class<ENUMTYPE> aClass,
                                                                                                                  @Nullable
                                                                                                                  String sID)
        Get the enum value with the passed string ID (case insensitive). If no such ID is present, an IllegalArgumentException is thrown.
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        sID - The ID to search
        Returns:
        The enum item with the given ID. Never null.
        Throws:
        IllegalArgumentException - if no enum item with the given ID is present
      • getFromIDOrNull

        @Nullable
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasIntID> ENUMTYPE getFromIDOrNull​(@Nonnull
                                                                                             Class<ENUMTYPE> aClass,
                                                                                             int nID)
        Get the enum value with the passed ID
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        nID - The ID to search
        Returns:
        null if no enum item with the given ID is present.
      • getFromIDOrDefault

        @Nullable
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasIntID> ENUMTYPE getFromIDOrDefault​(@Nonnull
                                                                                                Class<ENUMTYPE> aClass,
                                                                                                int nID,
                                                                                                @Nullable
                                                                                                ENUMTYPE eDefault)
        Get the enum value with the passed ID
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        nID - The ID to search
        eDefault - The default value to be returned, if the ID was not found.
        Returns:
        The default parameter if no enum item with the given ID is present.
      • getFromIDOrThrow

        @Nonnull
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasIntID> ENUMTYPE getFromIDOrThrow​(@Nonnull
                                                                                              Class<ENUMTYPE> aClass,
                                                                                              int nID)
        Get the enum value with the passed ID. If no such ID is present, an IllegalArgumentException is thrown.
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        nID - The ID to search
        Returns:
        The enum item with the given ID. Never null.
        Throws:
        IllegalArgumentException - if no enum item with the given ID is present
      • getFromNameOrNull

        @Nullable
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> ENUMTYPE getFromNameOrNull​(@Nonnull
                                                                                              Class<ENUMTYPE> aClass,
                                                                                              @Nullable
                                                                                              String sName)
        Get the enum value with the passed name
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        sName - The name to search
        Returns:
        null if no enum item with the given name is present.
      • getFromNameOrDefault

        @Nullable
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> ENUMTYPE getFromNameOrDefault​(@Nonnull
                                                                                                 Class<ENUMTYPE> aClass,
                                                                                                 @Nullable
                                                                                                 String sName,
                                                                                                 @Nullable
                                                                                                 ENUMTYPE eDefault)
        Get the enum value with the passed name
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        sName - The name to search
        eDefault - The default value to be returned, if the name was not found.
        Returns:
        The default parameter if no enum item with the given name is present.
      • getFromNameOrThrow

        @Nonnull
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> ENUMTYPE getFromNameOrThrow​(@Nonnull
                                                                                               Class<ENUMTYPE> aClass,
                                                                                               @Nullable
                                                                                               String sName)
        Get the enum value with the passed name. If no such name is present, an IllegalArgumentException is thrown.
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        sName - The name to search
        Returns:
        The enum item with the given name. Never null.
        Throws:
        IllegalArgumentException - if no enum item with the given name is present
      • getFromNameCaseInsensitiveOrNull

        @Nullable
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> ENUMTYPE getFromNameCaseInsensitiveOrNull​(@Nonnull
                                                                                                             Class<ENUMTYPE> aClass,
                                                                                                             @Nullable
                                                                                                             String sName)
        Get the enum value with the passed name case insensitive
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        sName - The name to search
        Returns:
        null if no enum item with the given ID is present.
      • getFromNameCaseInsensitiveOrDefault

        @Nullable
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> ENUMTYPE getFromNameCaseInsensitiveOrDefault​(@Nonnull
                                                                                                                Class<ENUMTYPE> aClass,
                                                                                                                @Nullable
                                                                                                                String sName,
                                                                                                                @Nullable
                                                                                                                ENUMTYPE eDefault)
        Get the enum value with the passed name case insensitive
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        sName - The name to search
        eDefault - The default value to be returned, if the name was not found.
        Returns:
        The default parameter if no enum item with the given name is present.
      • getFromNameCaseInsensitiveOrThrow

        @Nonnull
        public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> ENUMTYPE getFromNameCaseInsensitiveOrThrow​(@Nonnull
                                                                                                              Class<ENUMTYPE> aClass,
                                                                                                              @Nullable
                                                                                                              String sName)
        Get the enum value with the passed name (case insensitive). If no such name is present, an IllegalArgumentException is thrown.
        Type Parameters:
        ENUMTYPE - The enum type
        Parameters:
        aClass - The enum class
        sName - The name to search
        Returns:
        The enum item with the given name. Never null.
        Throws:
        IllegalArgumentException - if no enum item with the given name is present
      • getEnumID

        @Nonnull
        public static String getEnumID​(@Nonnull
                                       Enum<?> aEnum)
        Get the unique name of the passed enum entry.
        Parameters:
        aEnum - The enum to use. May not be null.
        Returns:
        The unique ID as a combination of the class name and the enum entry name. Never null.