I have this aggregate converter:
public O convert() {
output = getOutput();
final List<GenericConverterBase> converters = getConverters();
for (GenericConverterBase converterBase : converters) {
output = (O) Optional.ofNullable(converterBase.getInput()).map(converterBase::apply).orElse(output);
}
return output;
}
Basically, the getConverters() return a list of converters that have a concrete implementation of the apply in with they fill a common kind of object (let's say OutputDTO).
This works, however I have 2 issues:
- Is it possibile to transform for each loop using a java 8 functional method?
Premise: Actually the output is filled with new properties and keep the old ones, i.e. the second time the loop call the converter.apply() the output will be a result of the second and the previous one.
- I needed to cast to (O) within the loop, otherwise java doesn't compile because it say "required O, found Object". By applying the cast (O) I solved the issue, btw compiler warn me of the "unchecked cast". Any chance to solve in other way ?
convertersto assign it(after mapping) to theoutputvariable? What is the iteration even required for? It would be worth sharing the implementations relevant to the question for the classesGenericConverterBaseandO(not assuming it to be a generic here).inputis nullnullinput has the same effect, but does much less work.