Jordi
Jordi

Reputation: 23277

apache commons: FailableFunction and Function inside same FailableStream

Is there any way to use both FailableFunction and Function lambdas into a single .map chaining stream?

Function<String, Organization> hook = (id) -> this.organizationRepository.findById(id).get();
FailableFunction<Organization, Organization, MpiException> failableSave = (r) -> this.organizationRepository.save(r);

List<String> ids;

Failable.stream(ids)
   .map(hook)
   .map(failableSave)
   .collect(Collectors.toList());

I'm getting:

The method map(FailableFunction<String,R,?>) in the type Streams.FailableStream is not applicable for the arguments (Function<String,Organization>)

Problem here is that sometimes I need to use Function and other times I need to use FailableFunction.

Any ideas about how to use them into same stream mapping chaining?

Upvotes: 1

Views: 1094

Answers (1)

Adrian Diemer
Adrian Diemer

Reputation: 127

Maybe not the most elegant solution, but since nobody else came up with something:

You could use a method reference to implicitly convert a Function into a FailableFunction:

Failable.stream(ids)
   .map(hook::apply)
   .map(failableSave)
   .collect(Collectors.toList());

Upvotes: 2

Related Questions