Package me.deecaad.core.utils
Class EnumUtil
java.lang.Object
me.deecaad.core.utils.EnumUtil
This final utility class outlines static methods that operate on or return enums. This class
keeps a cache of enums to save CPU processes at the expense of increase memory usage.
The methods in this class are designed to be threadsafe.
-
Method Summary
Modifier and TypeMethodDescriptiongetIfPresent(@NotNull Class<T> clazz, @NotNull String name) getOptions(@NotNull Class<T> clazz) Returns an immutable set of the name of everyEnumassociated with the given enum class.static <T extends Enum<T>>
TReturns an immutable collection of theWeakReferenceto everyEnumassociated with the given enum class.parseEnums(Class<T> clazz, String input) Returns an immutable list of enum values that match the input.
-
Method Details
-
parseEnums
Returns an immutable list of enum values that match the input. If theinputstarts with a$, all enum values that contain the input are added to the list.Otherwise, this method returns an immutable list of 0 or 1 enums values.
- Parameters:
input- The input matcher. If the input starts with a $, it matches multiple enum values.- Returns:
- A non-null, immutable list of all parsed enum values.
- See Also:
-
getIfPresent
@NotNull public static <T extends Enum<T>> @NotNull Optional<T> getIfPresent(@NotNull @NotNull Class<T> clazz, @NotNull @NotNull String name) Returns anEnumof the givenClasstype as anOptional. The resultingOptionalwill be empty if noEnumwith thenameexists.- Type Parameters:
T- The generic type of the enum.- Parameters:
clazz- The enum's non-null class type.name- The name of the enum to grab. This string should be trimmed (String.trim()) and uppercase (String.toUpperCase()).- Returns:
- An optional of the enum found, or an empty optional.
-
getOrNull
@Nullable public static <T extends Enum<T>> T getOrNull(@NotNull @NotNull Class<T> clazz, @NotNull @NotNull String name) - Type Parameters:
T- The generic type of the enum.- Parameters:
clazz- The enum's non-null class type.name- The name of the enum to grab. This string should be trimmed (String.trim()) and uppercase (String.toUpperCase()).- Returns:
- The enum found, or null.
-
getOptions
@NotNull public static <T extends Enum<T>> @NotNull Set<String> getOptions(@NotNull @NotNull Class<T> clazz) Returns an immutable set of the name of everyEnumassociated with the given enum class.- Type Parameters:
T- The enum type.- Parameters:
clazz- Class to grab the enum from.- Returns:
- immutable set of all enums.
- See Also:
-
getValues
@NotNull public static <T extends Enum<T>> @NotNull List<T> getValues(@NotNull @NotNull Class<T> clazz) Returns an immutable collection of theWeakReferenceto everyEnumassociated with the given enum class. The references will only return null if garbage collections has unloaded the enums.- Type Parameters:
T- The enum type.- Parameters:
clazz- Class to grab the enum from.- Returns:
- An immutable set of all enums.
- See Also:
-