-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lombok.enum #1545
base: master
Are you sure you want to change the base?
Lombok.enum #1545
Conversation
Some random comments (I only skimmed over the code):
|
Thanks for the comments. |
1., 2. Agreed
According to stats I've seen, there are still 30-40% versions older than Java 8 in 2017, so it's a hard question. I'd prefer the supplier version as it's very general and forces you to think about the miss. But with my below proposal, this dilemma vanishes.
Either this or require uniqueness (which is what I usually do by adding a static For the scalar return, there are the following variants:
This could be controlled by the Disclaimer: I'm just a random commenter here, so whatever I say, doesn't matter. ;) I'd await some feedback from the project owners (which may take long 😢). |
@rspilker is going to work on fleshing this out. He has a few modifications in mind. |
In case you don't know about it, you might want to draw inspiration from lombok-pg's @EnumId. |
What about
This is a well-established name for such methods. |
The findByX names came from my original thought of "searching the 'database' of existing enum values'. I read "Effective Java 3rd ed." during my holidays so I quote Joshua Bloch (Item 1, p.9) here for common names:
For the here intented behaviour I think:
So I would prefer the fromBy{AttributeName}(param) over the suggested of{AttributeName}(param). Generating the options-stuff additionally is an idea ...
which generates something like
|
In the enums which I have written so far and which I would like to have generated by Lombok, I also use |
The foreach iterator's performance is lower than get from a map, especially a immutable map.And somehow a list of value with the same type is also meet the need.
` |
fbf8c41
to
650e037
Compare
Sometime ago i was part of huge project, it was easy to mess all around so they used strict terminology for methods for getting data: Since that time i'm following that everywhere and it works to me. My enums look like this, in most cases they have only
|
As a minor point, from a performance perspective calling values() is slow
due to it cloning the internal values array under the covers to guarantee
immutability.
That's why the static map version outperforms the simple version in the
general case.
As for naming, Enum should imply getters for any fields*, so I prefer
find() for the actual lookup methods. And yes, I prefer them to return
Optional**.
*) Enums should have fields. Most projects only use the names but that is
by far the least interesting use of an enum. And there is almost guaranteed
to be a switch or series of if statements somewhere else that could be
replaced with a field..
**) Honestly, projects running on 10 year+ old java versions can only get
away with that only because projects try to cater to them. At some point it
should start to break down for the simple reason that the maintenance
burden grows exponentially with the number of supported java versions -
staying on java 5 forever is a choice, and doing that is in my view
antisocial. Dropping support for those at some point is entirely
justifiable.
…On Sat, Aug 29, 2020, 23:22 Volodymyr ***@***.***> wrote:
Sometime ago i was part of huge project, it was easy to mess all around so
they used strict terminology for methods for getting data:
find - when you may get object back and may not and both are valid cases
- it should return optional
get - when you most likely expect to get object back - it should return
object itself
Since that time i'm following that everywhere and it works to me. My enums
look like this, in most cases they have only find method, get methods
added rather as example and using 1.6 java may be implemented without
optional:
public enum Color
{
RED, GREEN, BLLUE,
;
public static final Map<String, Color> MAP_BY_NAME = Stream.of(values())
.collect(Collectors.toMap(Color::name, Function.identity()));
public static Optional<Color> find(String name)
{
return Optional.ofNullable(name).map(String::toUpperCase).map(MAP_BY_NAME::get);
}
public static Color get(String name)
{
return get(name, null);
}
public static Color get(String name, Color defaultValue)
{
return find(name).orElse(defaultValue);
}
}
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#1545 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABIERP5OUKTSPR247IF2S3SDFWPRANCNFSM4EJMBDOQ>
.
|
Hi, I think this is a very useful feature, do you have a release plan? |
Implemented a draft of a handler for @lombok.Enum, adding a finder method for each field of an enum.
Have checked that manually in Eclipse Oxygen (4.7.0) and Oracle JDK 1.8.
A command line "ant test" went wrong with unrelated errors like "RunTestsViaDelombok cannot be resolved to a type".
Maybe some more docs are required (where?)
Before merging the rebase had to be done (by me) - but I want to discuss before ;-)