Actually your code can be refactored pretty fine. Here are some general comments extracted from the code, and the code itself later.
- get from Java types (Array, util.List, etc) to fj counterparts as soon as you can
- never return Option<A> if the only way you make an option is Option.success(a)
- the above generalizes for other Contexts + lifting methods too
- also, never create manually a F<List<A>, List<B>> or any other F<C<A>, C<B>> for C in the fj contexts, rather use F<A, B>.mapContext, like .mapList, .mapArray, .mapOption which lift the F<A, B> to work in the given context
- if possible refactor anonymous functions to classes, bit nicer
The code is substantially cleaner this way.
void x() {
final Plan plan = new Plan();
final Section section = new Section();
Option.fromNull(plan.getOrderRequestArray())
.map(Array.<PlanOfCareType>wrap()) // get from Java raw type to fj type as soon as you can
.map(getGoals.mapArray())
.map(getGoalArray.mapOption().mapArray())
.foreach(addGoalsToSection(section));
}
// Lift using Option.some is to be avoided + map instead bind
private F<PlanOfCareType, Option<Goals>> getGoals =
new F<PlanOfCareType, Option<Goals>>() {
@Override
public Option<Goals> f(PlanOfCareType planOfCareType) {
return Option.fromNull(planOfCareType.getGoals());
}
};
// again don't use Option.some() just for fun
//
// dont write a new F<Array<A>, Array<B>>, instad use F<A, B>.mapArray
// dont write a new F<Option<A>, Option<B>>, instad use F<A, B>.mapOption
private F<Goals, GoalType[]> getGoalArray = new F<Goals, GoalType[]>() {
@Override
public GoalType[] f(Goals goals) {
return goals.getGoalArray();
}
};
private Effect<Array<Option<GoalType[]>>> addGoalsToSection(final Section section) {
return new Effect<Array<Option<GoalType[]>>>() {
@Override
public void e(Array<Option<GoalType[]>> optionalGoalTypeArray) {
for (Option<GoalType[]> maybeGoalTypes : optionalGoalTypeArray) {
for (GoalType[] someGoalTypes : maybeGoalTypes) {
for (GoalType goalType : someGoalTypes) {
section.addToMultilineDataCell(goalType.getDescription());
}
}
}
}
};
}
BR,
Robin