static <R> Function<Optional<R>,Function<R, Optional<String>>> checkPresence(String err){
return ro -> r -> ro.map(x -> emptyStrOpt.get())
.orElseGet(() -> Optional.of(err));
}
//toDateOfInjury is Function<Bill,LocalDate>
static final Function<Bill,Optional<String>> dateOfInjuryNotNull = b ->
toDateOfInjury.andThen(
o -> checkPresence("No date of injury").apply(o).apply(b));
IntelliJ complains that for o, it expects Optional<Object> but it's receiving Optional<LocalDate>.
Also, is there any way to declare a function using generics without returning it from a method?
EDIT: for the sake of completeness, let's just say that toDateOfInjury is
Function<Bill,Optional<LocalDate>> toDateOfInjury = b -> Optional.ofNullable(LocalDate.now()); //though it map to empty optional too of course
EDIT2: After getting past the first problem, I fixed it like so:
static final Function<Bill,Optional<String>> dateOfInjuryNotNull2 = b ->
toDateOfInjury.andThen(
o -> EventValidationUtils.<LocalDate>checkPresence("No date of injury").apply(o)).apply(b);
static <R> Function<Optional<R>,Optional<String>> checkPresence(String err){
return ro -> ro.map(x -> emptyStrOpt.get())
.orElseGet(() -> Optional.of(err));
}
CodePudding user response:
See this answer: Calling static generic methods
You need to specify generic type (here is LocaleDate) when calling a generic method. In your method call it cannot specify method type so inherit it as Object. So you need to specify it like below (Suppose Test is your class name):
static final Function<Bill,Optional<String>> dateOfInjuryNotNull = b ->
toDateOfInjury.andThen(
o -> Test.<LocalDate>checkPresence("No date of injury").apply(o).apply(b));
But you face other problems need to solve.
