Package com.helger.base.lang
Class EnumHelper
java.lang.Object
com.helger.base.lang.EnumHelper
Some enum utility methods.
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull EChangeClear the internal int ID enum cache.static <ENUMTYPE extends Enum<ENUMTYPE>>
@Nullable ENUMTYPEFind the first enum constant matching the provided filter.static <ENUMTYPE extends Enum<ENUMTYPE>>
@Nullable ENUMTYPEfindFirst(@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.Get all enum constants matching the provided filter.static @NonNull StringGet the unique name of the passed enum entry.getFromIDCaseInsensitiveOrDefault(@NonNull Class<ENUMTYPE> aClass, @Nullable String sID, @Nullable ENUMTYPE eDefault) Get the enum value with the passed string ID case insensitivegetFromIDCaseInsensitiveOrNull(@NonNull Class<ENUMTYPE> aClass, @Nullable String sID) Get the enum value with the passed string ID case insensitivegetFromIDCaseInsensitiveOrThrow(@NonNull Class<ENUMTYPE> aClass, @Nullable String sID) Get the enum value with the passed string ID (case insensitive).getFromIDOrDefault(@NonNull Class<ENUMTYPE> aClass, int nID, @Nullable ENUMTYPE eDefault) Get the enum value with the passed IDgetFromIDOrDefault(@NonNull Class<ENUMTYPE> aClass, @Nullable KEYTYPE aID, @Nullable ENUMTYPE eDefault) Get the enum value with the passed IDgetFromIDOrNull(@NonNull Class<ENUMTYPE> aClass, int nID) Get the enum value with the passed IDgetFromIDOrNull(@NonNull Class<ENUMTYPE> aClass, @Nullable KEYTYPE aID) Get the enum value with the passed IDgetFromIDOrThrow(@NonNull Class<ENUMTYPE> aClass, int nID) Get the enum value with the passed ID.getFromIDOrThrow(@NonNull Class<ENUMTYPE> aClass, @Nullable KEYTYPE aID) Get the enum value with the passed ID.getFromNameCaseInsensitiveOrDefault(@NonNull Class<ENUMTYPE> aClass, @Nullable String sName, @Nullable ENUMTYPE eDefault) Get the enum value with the passed name case insensitivegetFromNameCaseInsensitiveOrNull(@NonNull Class<ENUMTYPE> aClass, @Nullable String sName) Get the enum value with the passed name case insensitivegetFromNameCaseInsensitiveOrThrow(@NonNull Class<ENUMTYPE> aClass, @Nullable String sName) Get the enum value with the passed name (case insensitive).getFromNameOrDefault(@NonNull Class<ENUMTYPE> aClass, @Nullable String sName, @Nullable ENUMTYPE eDefault) Get the enum value with the passed namegetFromNameOrNull(@NonNull Class<ENUMTYPE> aClass, @Nullable String sName) Get the enum value with the passed namegetFromNameOrThrow(@NonNull Class<ENUMTYPE> aClass, @Nullable String sName) Get the enum value with the passed name.
-
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 benull.aFilter- The filter to apply. May benullto match any element.- Returns:
nullif 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 benull.aFilter- The filter to apply. May benullto match any element.eDefault- The default value to return if no matching enum constant is found. May benull.- 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 benull.aFilter- The filter to apply. May benullto 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 typeENUMTYPE- The enum type- Parameters:
aClass- The enum classaID- The ID to search- Returns:
nullif 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 typeENUMTYPE- The enum type- Parameters:
aClass- The enum classaID- The ID to searcheDefault- 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, anIllegalArgumentExceptionis thrown.- Type Parameters:
KEYTYPE- The ID typeENUMTYPE- The enum type- Parameters:
aClass- The enum classaID- 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 classsID- The ID to search- Returns:
nullif 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 classsID- The ID to searcheDefault- 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, anIllegalArgumentExceptionis thrown.- Type Parameters:
ENUMTYPE- The enum type- Parameters:
aClass- The enum classsID- 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 classnID- The ID to search- Returns:
nullif 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 classnID- The ID to searcheDefault- 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, anIllegalArgumentExceptionis thrown.- Type Parameters:
ENUMTYPE- The enum type- Parameters:
aClass- The enum classnID- 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 classsName- The name to search- Returns:
nullif 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 classsName- The name to searcheDefault- 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, anIllegalArgumentExceptionis thrown.- Type Parameters:
ENUMTYPE- The enum type- Parameters:
aClass- The enum classsName- 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 classsName- The name to search- Returns:
nullif 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 classsName- The name to searcheDefault- 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, anIllegalArgumentExceptionis thrown.- Type Parameters:
ENUMTYPE- The enum type- Parameters:
aClass- The enum classsName- 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
Get the unique name of the passed enum entry.- Parameters:
aEnum- The enum to use. May not benull.- Returns:
- The unique ID as a combination of the class name and the enum entry name. Never
null.
-
clearCache
Clear the internal int ID enum cache.- Returns:
EChange.CHANGEDif the cache was not empty and was cleared,EChange.UNCHANGEDif the cache was already empty.
-