0
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()
FaraBara
  • 115
  • 7

1 Answers1

0

This looks like you want a simple null check. What about

return dateOfInjury==null?Optional.empty():Optional.of("No date of injury");
f1sh
  • 11,489
  • 3
  • 25
  • 51