Interface EnumLike<T extends EnumLike<T>>

Type Parameters:
T - The type of the enum-like class
All Superinterfaces:
Comparable<T>
All Known Implementing Classes:
Priority
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface EnumLike<T extends EnumLike<T>> extends Comparable<T>
An interface that provides utility methods for enum-like classes.

An enum-like class is a class with constants like an enum but with a public constructor.
The enum-like class contains predefined constants of its own type.
An enum-like class is a class that contains constants of its own type.

It is anticipated that constants are added at runtime.
The constants are not required to be known at compile time.

Class Implementation example, see below:
The implementation can also be a record.


 public class Example implements EnumLike<Example> {

   // Required field, must be exactly declared like this
   @ReflectiveUsage
   private static final List<Example> VALUES = Lists.newLinkedList();

   public static final Example EXAMPLE_1 = new Example("Example 1");
   public static final Example EXAMPLE_2 = new Example("Example 2");
   public static final Example EXAMPLE_3 = new Example("Example 3");

   private final String name;

   public Example(String name) {
     this.name = name;
     VALUES.add(this);
   }

   public @NotNull String name() {
     return this.name;
   }

  // other methods
 }
 

The 'VALUES' field is required and must be exactly declared like above.
All instances of the enum-like class must be added to the 'VALUES' list in the constructor or a factory method.

  • Method Summary

    Modifier and Type
    Method
    Description
    default int
    compareTo(T object)
     
    static <T extends EnumLike<T>>
    @Unmodifiable @NotNull List<EnumConstant<T>>
    getPredefinedConstants(@NotNull Class<T> enumType)
    Gets all predefined constants of the given enum-like class.
    A constant is a static final field of the given enum-like class.
    private static boolean
    isConstant(@NotNull Field field)
    Checks if the given field is a constant.
    A constant is a static final field.
    @NotNull String
    Returns the name of the given enum-like constant.
    default int
    Returns the ordinal of the given enum-like constant.
    static <T extends EnumLike<T>>
    T
    valueOf(@NotNull Class<T> enumType, @Nullable String name)
    Returns the constant of the given enum-like class with the specified name.
    If the given name is null or no constant with the given name (case-insensitive) exists, an exception is thrown.
    static <T extends EnumLike<T>>
    @Unmodifiable @NotNull List<T>
    values(@NotNull Class<T> enumType)
    Gets the values all enum-like values of the given enum-like class.
  • Method Details

    • isConstant

      private static boolean isConstant(@NotNull @NotNull Field field)
      Checks if the given field is a constant.
      A constant is a static final field.
      Parameters:
      field - The field to check
      Returns:
      True, if the given field is a constant, otherwise false
      Throws:
      NullPointerException - If the given field is null
    • getPredefinedConstants

      @NotNull static <T extends EnumLike<T>> @Unmodifiable @NotNull List<EnumConstant<T>> getPredefinedConstants(@NotNull @NotNull Class<T> enumType)
      Gets all predefined constants of the given enum-like class.
      A constant is a static final field of the given enum-like class.
      Type Parameters:
      T - The type of the enum-like class
      Parameters:
      enumType - The enum-like class
      Returns:
      An unmodifiable list of all predefined constants declared in the given enum-like class
      Throws:
      NullPointerException - If the given enum-like class is null
    • values

      @NotNull static <T extends EnumLike<T>> @Unmodifiable @NotNull List<T> values(@NotNull @NotNull Class<T> enumType)
      Gets the values all enum-like values of the given enum-like class.

      The values are gathered from the 'VALUES' field in the given enum-like class.
      The 'VALUES' field is required and must be exactly declared like this:

      
       private static final List<Example> VALUES = Lists.newLinkedList();
       
      Type Parameters:
      T - The type of the enum-like class
      Parameters:
      enumType - The enum-like class
      Returns:
      An unmodifiable list with the values of all constants declared in the given enum-like class
      Throws:
      NullPointerException - If the given enum-like class is null
      ClassCastException - If the 'VALUES' field in the given enum-like class is not a list of the given enum-like class
      IllegalStateException - If no 'VALUES' field in the given enum-like class exists
    • valueOf

      static <T extends EnumLike<T>> T valueOf(@NotNull @NotNull Class<T> enumType, @Nullable @Nullable String name)
      Returns the constant of the given enum-like class with the specified name.
      If the given name is null or no constant with the given name (case-insensitive) exists, an exception is thrown.
      Type Parameters:
      T - The type of the enum-like class
      Parameters:
      enumType - The enum-like class
      name - The name of the constant
      Returns:
      The constant with the given name
      Throws:
      NullPointerException - If the given enum-like class is null
      IllegalStateException - If no constant with the given name exists
    • name

      @NotNull @NotNull String name()
      Returns the name of the given enum-like constant.
      Returns:
      The name of the constant
    • ordinal

      default int ordinal()
      Returns the ordinal of the given enum-like constant.
      Returns:
      The ordinal of the constant
      Throws:
      IllegalStateException - If the constant was not correctly defined
    • compareTo

      default int compareTo(@NotNull T object)
      Specified by:
      compareTo in interface Comparable<T extends EnumLike<T>>