I have a class called Address
which looks like this:
@Value
class Address {
@NotNull String userId;
@NotNull String line1;
String line2;
private Address(Builder b) {
// copy everything from builder
}
// override getter for line2 so that it returns Optional<String>
public Optional<String> getLine2() {
return Optional.ofNullable(this.line2);
}
// and a Builder
public static class Builder {
// builder methods
}
}
Here I am forced to write Builder
and a Getter
because, if I want to return an Optional while using Lombok, I will have to declare line2
as Optional<String>
. And that will generate a builder method which accepts Optional<String>
!
Is there any other way to use lombok with Optional
?
Optional
as a replacement ofnull
. And that it will an over use ofOptional
if used in getters. But then there is another, more convenient, philosophy of Google Guava's developers, which allows you to use Optional to replace nullGuava's Optional So, I personally think thatlombok
shld stay neutral and provide the support for autoOptional, and leave whether to use it or not to the programmers – Gorizia