I have this method that calls multiple APIs and aggregates all the results and returns them in a Map where each set of results is matched to the target API so that they can be grouped and displayed properly in a service.
public AggregateResults search(QueryParams params) {
Stream<APITarget> targets = params.getTargets().parallelStream();
Function<APITarget, APIResults> func = (APITarget target) -> {
if (params.length() >= MIN_LENGTH) {
switch (target) {
case api1:
return searchAPI1(params);
case api2:
return searchAPI2(params);
.
.
case apin:
return searchAPIn(params);
}
}
return new APIResults.Builder().build();
};
Map<APITarget, APIResults> results = targets.collect(Collectors.toMap(t -> t, func));
return AggregateResults;
}
I have now have to refactor this code so that for example API 1 - 3 can be called in one function and then API 4 - N can be called from another function. The functions will be called from different methods so I need to move this function out of this method and then pass in the QueryParams object in as another parameter to the function but then I run into the issue of the Function not being able to accept more than one parameter. E.g.
Function<APITarget, APIResults> exampleFunc = (APITarget target, QueryParams params) -> {
if (params.length() >= MIN_LENGTH) {
switch (target) {
case api1:
return searchAPI1(params);
case api2:
return searchAPI2(params);
}
}
return new APIResults.Builder().build();
};
I have seen something similar mentioned here: Can a java lambda have more than 1 parameter? But the examples of BiFunctions only show them being used as lambdas and so wont work when being called by an external method, from what I have been able to find. Also when it talks about creating a new FunctionalInterface I'm not sure what exactly is needed to be able to use the new interface in my required functions.
Any ideas would be great.