Optional Support for the build method
StefanSchrass opened this issue · 0 comments
StefanSchrass commented
Often, if not always I am implementing the Builder like this:
public static final class Builder implements ValidatingBuilder {
private Thing a;
private Thing b;
@Override
public Boolean isValid() { return null != a && null != b; }
public Builder withA(Thing a) {
this.a = a;
return this;
}
public Builder withB(Thing b) {
this.b = b;
return this;
}
public Optional<MyObject> build() {
Optional<MyObject> myObject = Optional.empty();
if (isValid()) {
myObject = Optional.of(new MyObject(this));
}
return myObject;
}
}
The idea behind this is, that a resulting Object can only be instantiated if all mandatory fields are set (and further more with valid values). So I've made a very simple interface for this, but the build()
method is not covered by it (the interface just lets me use [ctrl]-[i] to get the method stub).
I use the innerbuilder regularly and therefore issueing the request for supporting Optionals for the build method, because for me this would be a great feature.