class Model {@NonNull A a;B b;C c;ModelBuilderWithoutA builder() { ... }}interface ModelBuilder {ModelBuilder setB(B b)ModelBuilder setC(C c)Model build()}interface ModelBuilderWithoutA {ModelBuilder setA(A a)ModelBuilderWithoutA setB(B b)ModelBuilderWithoutA setC(C c)}
--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I've seen the pattern where multiple builders are created to enforce required fields. (...)
If someone wanted to work on this, where would they start?
@Data
@Builder(builderMethodName = "privateBuilder")
public class Person {
@NonNull
private String name;
@NonNull
private String surname;
private int age;//optional
//disable public builder
private static PersonBuilder privateBuilder(){
return Person.privateBuilder();
}
public static Url safeBuilder() {
return new Builder();
}
interface Url {
Surname name(String name);
}
interface Surname {
Build surname(String surname);
}
interface Build {
Build age(int age);
Person build();
}
public static class Builder implements Url, Surname, Build {
PersonBuilder pb = Person.privateBuilder();
@Override
public Surname name(String name) {
pb.name(name);
return this;
}
@Override
public Build surname(String surname) {
pb.surname(surname);
return this;
}
@Override
public Build age(int age) {
pb.age(age);
return this;
}
@Override
public Person build() {
return pb.build();
}
}
}
I've seen the pattern where multiple builders are created to enforce required fields. For example:class Model {@NonNull A a;B b;C c;ModelBuilderWithoutA builder() { ... }}interface ModelBuilder {ModelBuilder setB(B b)ModelBuilder setC(C c)Model build()}interface ModelBuilderWithoutA {ModelBuilder setA(A a)ModelBuilderWithoutA setB(B b)ModelBuilderWithoutA setC(C c)}In the above example, you literally can't run build() until A has been set, because you would have had to call setA to get a ModelBuilder. You can also set B and C before or after A being set. This provides for a type-safe builder for required fields. As you can imagine, the combinatorial matrix of builders to maintain this setup is tedious, but something like Lombok could do the generation and hide all the messy details away.If someone wanted to work on this, where would they start?
Today, I've been discussing this topic with my team, because we ran into similar problems. We're glad you acknowledge the problem, however we do not share your opinion that an IDE plugin is a superior solution. We are curious why you believe that to be the case.
In the meantime, please consider our arguments in favor of a pure-code solution:
- A pure-code solution would cause a failing build in a build pipeline due to a compile error, while an IDE plugin cannot;
- Not every developer uses the same IDE, which means the plugin would have to be developed & maintained for multiple IDE's;
- It might be the case that not every developer will have this plugin installed, leaving possible inconsistencies in code quality.