You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement @EnumHelpers annotation and the corresponsing annotation processor to generate boilerplate enum methods.
Problem
While Java's enum is a powerful mechanism for describing varios constructions, it has a number of negative aspects which make it not that helpful.
The biggest problem is static E @NotNull [] values() method which is required to always return a fresh copy of the array due to array mutability although it's usually needed for iteration.
Another serious design problem (especially considering padla's philosophy) is inability to do try-match operation, i.e. to try to find an enum constant by a string name alternatively fallin back to null or default value etc.
This PR addresses this by provifing a @EnumHelpers annotation causing the generation of ES utility class for enum E.
Goals
Add @EnumHelper allowing generation of:
@NotNull Stream<@NotNull E> stream()
@NotNull @Unmodifiable List<@NotNull E> values()
@Nullable E match(final @NotNull String name) and similar ones
implement annotation-processor to generate the helper class
Example
Given
@EnumHelperspublicenumFoo {
A, B, C
}
the following should be generated:
@GeneratedpublicfinalclassFoos {
privatestaticfinal@NotNullFoo@NotNull [] VALUES_ARRAY
= Foo.values(); // may be used for faster iterationsprivatestaticfinal@NotNullList<@NotNullFoo> VALUE_LIST
= Collections.unmodifiableList(Arrays.asList(VALUES_ARRAY));
privateFoos() {
thrownewAssertionError("Foos is an utility class and thus should never be instantiated");
}
publicstatic@NotNullStream<@NotNullFoo> stream() {
returnArrays.stream(VALUES_ARRAY);
}
publicstatic@NotNull@UnmodifiableList<@NotNullFoo> values() {
returnVALUES_LIST;
}
publicstatic@NullableFoomatch(final@NotNullStringname) {
if (name == null) thrownewNullPointerException("name is null");
switch (name) {
case"A": returnA;
case"B": returnB;
case"C": returnC;
default: returnnull;
}
}
publicstaticFoomatch(final@NotNullStringname, finalFoodefaultValue) {
if (name == null) thrownewNullPointerException("name is null");
switch (name) {
case"A": returnA;
case"B": returnB;
case"C": returnC;
default: returndefaultValue;
}
}
// note: possibly worth specialized lambdapublicstatic@NullableFoomatch(final@NotNullStringname, final@NotNullSupplier<Foo> defaultValueSupplier) {
if (name == null) thrownewNullPointerException("name is null");
if (defaultValueSupplier== null) thrownewNullPointerException("defaultValueSupplieris null");
switch (foo) {
case"A": returnA;
case"B": returnB;
case"C": returnC;
default: returndefaultValueSupplier.get();
}
}
// ...
}
Notes
Generated class should net rely on Lombok as it may be not used by annotation users.
Subjects to discuss:
Documenting annotation should be generated if possible(?) but it is not clear whether some eaxct one (org.jetbrains) should be used or custom (via an annotation parameter).
The text was updated successfully, but these errors were encountered:
use following naming: Foos.stream(), Foos.list(), Foos.array()
use List.of() instead of Collections.unmodifiableList(Arrays.asList()) for Java 9+
make Foos.match generation optional
add additional annotation to generate match-like methods for finding by field:
@RequiredArgsConstructor@GetterenumFoo {
FOO_BAR("foo-bar"),
BAR_BAZ("bar-baz");
@EnumHelpers.FinderprivatefinalStringvalue;
}
classFoos {
publicstaticFoofromValue(Stringvalue) {
// some optimal finding logic// for example: // 1) use HashMap for big enums and array-foreach for small enums// 2) switch-case if enum field is compile-time constant
}
}
add additional annotation to generate enum name constants and field constants:
Description
Implement
@EnumHelpers
annotation and the corresponsing annotation processor to generate boilerplate enum methods.Problem
While Java's
enum
is a powerful mechanism for describing varios constructions, it has a number of negative aspects which make it not that helpful.The biggest problem is
static E @NotNull [] values()
method which is required to always return a fresh copy of the array due to array mutability although it's usually needed for iteration.Another serious design problem (especially considering padla's philosophy) is inability to do
try-match
operation, i.e. to try to find an enum constant by a string name alternatively fallin back tonull
ordefault
value etc.This PR addresses this by provifing a
@EnumHelpers
annotation causing the generation ofES
utility class for enumE
.Goals
@EnumHelper
allowing generation of:@NotNull Stream<@NotNull E> stream()
@NotNull @Unmodifiable List<@NotNull E> values()
@Nullable E match(final @NotNull String name)
and similar onesExample
Given
the following should be generated:
Notes
Generated class should net rely on
Lombok
as it may be not used by annotation users.Subjects to discuss:
org.jetbrains
) should be used or custom (via an annotation parameter).The text was updated successfully, but these errors were encountered: