- 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.
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 TypeMethodDescriptiondefault int
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
name()
Returns the name of the given enum-like constant.default int
ordinal()
Returns the ordinal of the given enum-like constant.static <T extends EnumLike<T>>
TReturns 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.Gets the values all enum-like values of the given enum-like class.
-
Method Details
-
isConstant
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 nullClassCastException
- If the 'VALUES' field in the given enum-like class is not a list of the given enum-like classIllegalStateException
- 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 classname
- The name of the constant- Returns:
- The constant with the given name
- Throws:
NullPointerException
- If the given enum-like class is nullIllegalStateException
- If no constant with the given name exists
-
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
- Specified by:
compareTo
in interfaceComparable<T extends EnumLike<T>>
-