Class EnumUtil

java.lang.Object
me.deecaad.core.utils.EnumUtil

public final class EnumUtil extends Object
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 Details

    • parseEnums

      public static <T extends Enum<T>> List<T> parseEnums(Class<T> clazz, String input)
      Returns an immutable list of enum values that match the input. If the input starts 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 an Enum of the given Class type as an Optional. The resulting Optional will be empty if no Enum with the name exists.
      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)
      Returns an Enum of the given Class, or returns null.
      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 every Enum associated 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 the WeakReference to every Enum associated 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: