static final Function<Bill,Optional<LocalDate>> toDateOfInjury = b -> toClaim.andThen(opt -> opt.map(Claim::getDatInjuryDate)).apply(b);
public static final Supplier<Optional<String>> emptyStrOpt = Optional::empty;
static final Function<Bill,Optional<String>> dateOfInjuryNotNull = b ->
toDateOfInjury.andThen(
opt -> opt.flatMap(doi -> emptyStrOpt.get())
.or(() -> Optional.of("No date of injury"))).apply(b);
In a unit test, this always returns the "No date of injury" optional. I assume it is because the empty result of flatMap triggers it. This is what I want:
- null dateOfInjury: Optional.of(".....")
- non-null dateOfInjury: Optional.empty()