Passionate developer in Java and Scala. And sometimes, something else. A few months per year, someone calls me "professor". CoFounder of Scala By The Lagoon @scalagoon
Interesting tool. It's a pain to write these things by hand.
However, a note (or you may call it a feature request): with these templates, there is no check done by the builder that all or a mandatory subset of parameters have been given a value. You could use Optional<> to encode arguments that may not be necessary to construct the object; after all, one of the use cases of the Builder pattern is managing construction-time validation of the constructor arguments.
Another idea: there is at least another implementation style for the pattern; the one in which each step is encoded in a Class (or better, an Interface) so that the user must follow a precise call sequence in building the object. I.e:
A software developer. I'm interested in learning new technologies and core language features. I love to dive into legacy code writing tests and refactoring as I go.
I like the idea of using Optional but I thought it caused problems when declaring a field as Optional. Was this something in Effective Java? I'm not so sure it is an issue but it is possible to wrap the object in the getter instead.
I added @NotNull annotation to specify required properties and not required ones.
They are used for null checks in build methods.
What do you think?
The tool works with @Required and @NonNull as aliases for @NotNull as well.
Your "Another idea" is also interesting as a design that forces the coder to set the required parameters in order.
Best.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Interesting tool. It's a pain to write these things by hand.
However, a note (or you may call it a feature request): with these templates, there is no check done by the builder that all or a mandatory subset of parameters have been given a value. You could use
Optional<>
to encode arguments that may not be necessary to construct the object; after all, one of the use cases of the Builder pattern is managing construction-time validation of the constructor arguments.Another idea: there is at least another implementation style for the pattern; the one in which each step is encoded in a Class (or better, an Interface) so that the user must follow a precise call sequence in building the object. I.e:
The
Point
class can only be instantiated in this way:This style may be useful when you want to thightly constrain how an object is created.
I like the idea of using Optional but I thought it caused problems when declaring a field as Optional. Was this something in Effective Java? I'm not so sure it is an issue but it is possible to wrap the object in the getter instead.
Thanks for your helpful comments.
I added @NotNull annotation to specify required properties and not required ones.
They are used for null checks in build methods.
What do you think?
The tool works with @Required and @NonNull as aliases for @NotNull as well.
Your "Another idea" is also interesting as a design that forces the coder to set the required parameters in order.
Best.