Open bijialin opened 4 years ago
This feature probably wouldn't help much; the amount of code is about the same. Actually, in your example, the condition doesn't matter as mobile
is null
before the assignment.
In general, it could help to keep the fluent style.... however, I doubt it's worth it. Oftentimes, you could replace it by something like .mobile(Strings.nullToEmpty(mobile))
or .mobile(MoreObjects.firstNonNull(mobile, defaultMobile))
(both utils taken from Guava).
Disclaimer: I'm not a project owner.
seems quite a specific use-case. consider instead
Optional.ofNullable(mobile).ifPresent(builder::setMobile)
seems quite a specific use-case. consider instead
Optional.ofNullable(mobile).ifPresent(builder::setMobile)
Good point. I searched issues to suggest a feature that a builder's invocation chain may use an Optional
value.
Note that, using an Optional
as a method parameter is not a good move, yet I see,
public UriComponentsBuilder#queryParamIfPresent(String name, Optional value)
which keeps invocation chaining available.
Can we generate additional setter methods take Optional
which invoke original setter method when present?
public B userId(String userId) {
this.userId = userId;
return this.self();
}
public B userIdIfPresent(Optional<String> userId) {
userId.ifPresent(this::userId);
return this.self();
}
var userName = getUserName();
var userId = getUserId(); // Optional<String>
builder()
.userName(userName)
.userIdIfPresent(userId)
.build();
problem
we use
@Builder
set entity field,sometimes we need set it with some condition, just like objectnot null
,stringnot empty
..., if lombok can add a annotation to solve thatcurrent example
solution
if lombok can create
@Builder.Condition
annotation , to auto create method like this.when we use
@Accessors
set field value, it also same applies