I have the following piece of code (the code for TrResponse
and TrNode
is irrelevant):
public TrResponse from(final TrNode b1, final TrNode b2, final TrNode b3) {
final TrResponse resp = new TrResponse();
Stream.of(b1, b2, b3)
.filter(Objects::nonNull)
.findFirst()
.ifPresent(it -> {
resp.m1 = it.m1;
resp.m2 = it.m2;
resp.m3 = it.m3;
});
// b1, b2 or b3 can be null
// normalize() returns a mutated TrNode
resp.b1 = (null != b1) ? b1.normalize() : null;
resp.b2 = (null != b2) ? b2.normalize() : null;
resp.b3 = (null != b3) ? b3.normalize() : null;
return resp;
}
I'm trying to figure it out how to execute the normalize
function for all of b1
, b2
, and b3
(that are eventually non-null
) within the same stream operation so I don't have to have those null
checks later on (as I would have now). I've tried .map(TrNode::normalize)
right before .findFirst()
, but it only applies it, eventually, to the first found instance (if any).
Any clues?