Class EnumHelper

java.lang.Object
com.helger.base.lang.EnumHelper

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

    • findFirst

      public static <ENUMTYPE extends Enum<ENUMTYPE>> @Nullable ENUMTYPE findFirst(@NonNull Class<ENUMTYPE> aClass, @Nullable Predicate<? super ENUMTYPE> aFilter)
      Find the first enum constant matching the provided filter.
      Type Parameters:
      ENUMTYPE - The enum type
      Parameters:
      aClass - The enum class. May not be null.
      aFilter - The filter to apply. May be null to match any element.
      Returns:
      null if no matching enum constant is found.
    • findFirst

      public static <ENUMTYPE extends Enum<ENUMTYPE>> @Nullable ENUMTYPE findFirst(@NonNull Class<ENUMTYPE> aClass, @Nullable Predicate<? super ENUMTYPE> aFilter, @Nullable ENUMTYPE eDefault)
      Find the first enum constant matching the provided filter, returning a default value if none matches.
      Type Parameters:
      ENUMTYPE - The enum type
      Parameters:
      aClass - The enum class. May not be null.
      aFilter - The filter to apply. May be null to match any element.
      eDefault - The default value to return if no matching enum constant is found. May be null.
      Returns:
      The first matching enum constant or the default value.
    • getAll

      @ReturnsMutableCopy public static <ENUMTYPE extends Enum<ENUMTYPE>> @NonNull List<ENUMTYPE> getAll(@NonNull Class<ENUMTYPE> aClass, @Nullable Predicate<? super ENUMTYPE> aFilter)
      Get all enum constants matching the provided filter.
      Type Parameters:
      ENUMTYPE - The enum type
      Parameters:
      aClass - The enum class. May not be null.
      aFilter - The filter to apply. May be null to return all enum constants.
      Returns:
      A mutable copy of the matching enum constants. Never null.
    • getFromIDOrNull

      public static <KEYTYPE, ENUMTYPE extends Enum<ENUMTYPE> & IHasID<KEYTYPE>> @Nullable 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

      public static <KEYTYPE, ENUMTYPE extends Enum<ENUMTYPE> & IHasID<KEYTYPE>> @Nullable 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

      public static <KEYTYPE, ENUMTYPE extends Enum<ENUMTYPE> & IHasID<KEYTYPE>> @NonNull 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasID<String>> @Nullable 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasID<String>> @Nullable 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasID<String>> @NonNull 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasIntID> @Nullable 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasIntID> @Nullable 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasIntID> @NonNull 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> @Nullable 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> @Nullable 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> @NonNull 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> @Nullable 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> @Nullable 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

      public static <ENUMTYPE extends Enum<ENUMTYPE> & IHasName> @NonNull 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

      public static @NonNull 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.
    • clearCache

      public static @NonNull EChange clearCache()
      Clear the internal int ID enum cache.
      Returns:
      EChange.CHANGED if the cache was not empty and was cleared, EChange.UNCHANGED if the cache was already empty.