I am dirtying my hands with the fantastic library of vavr (0.9.2).
Here's a code snippet, that aims to collect an Either:
Either<Tuple2<Enum<ReportByTeamExecutionErrors>,String>, List<MayurDAO>> payloadPreparationResult =
new ReportByTeamCriteriaSchemaChecker("./json/schema/REQ_ReportByTeam_Schema.json")
.validateSchema(payLoad) // payload is JSON and is a paramter to this holding function
.map(JsonPOJOBidirectionalConverter::pojofyCriteriaAllFieldsTeam)
.map((e) -> {
CriteriaTeamAllFieldsValidator validator = new CriteriaTeamAllFieldsValidator(e.right().get());
return(validator.validate());
})
.map((e) -> retrieveRecordsAsPerCriteria(e.right().get())) // compiler doesn't approve of this line!!
;
The retrieveRecordsAsPerCriteria method is defined this way:
private Either<Tuple2<Enum<ReportByTeamExecutionErrors>,String>, List<MayurDAO>>
retrieveRecordsAsPerCriteria(CriteriaAllFieldsTeam criteriaAllFieldsTeam) {
Optional<List<MayurDAO>> retrievedRecords = new MayurModel().retrieveRecords(criteriaAllFieldsTeam);
return( (retrievedRecords.isPresent())
? (Either.right(retrievedRecords.get()))
: Either.left(
Tuple.of(
ReportByTeamExecutionErrors.NO_RECORDS_FOUND_TO_MATCH_CRITERIA,
"No records have been found, which match the criteria passed"
)
)
);
}
The compiler is complaining:
./com/myApp/application/ReportByTeamResource.java:58: error: incompatible types: cannot infer type-variable(s) L,R .map((e) -> retrieveRecordsAsPerCriteria(e.right().get())); ^ (actual and formal argument lists differ in length) where L,R are type-variables: L extends Object declared in method right(R) R extends Object declared in method right(R) 1 error
The List is from java.util.List.
I am at my wit's end to understand the source of the problem. The types should easily inferred, or so I think.
IntelliJ seems to be OK with the transformations till this objectionable line! In that line, however, the type parameter U is not decipherable. Could someone please nudge me to the path I am unable to see, for some reason?
Probably you might want to use flatMap
instead of map
. The advantage of flatMap
is that you don't get the nested eithers which you then need to unwrap in some way (like you are currently doing with .right().get()
in your current code).
So the code would look something like this then:
Either<Tuple2<Enum<ReportByTeamExecutionErrors>,String>, List<MayurDAO>> payloadPreparationResult =
new ReportByTeamCriteriaSchemaChecker("./json/schema/REQ_ReportByTeam_Schema.json")
.validateSchema(payLoad)
.flatMap(JsonPOJOBidirectionalConverter::pojofyCriteriaAllFieldsTeam)
.flatMap(e -> {
CriteriaTeamAllFieldsValidator validator = new CriteriaTeamAllFieldsValidator(e);
return (validator.validate());
})
.flatMap(this::retrieveRecordsAsPerCriteria)