final List hasElectionWithGIPlanCode = getEnrollmentDocumentResponse
.getDocument()
.getEnrollmentData()
.getElections()
.stream()
.map(election -> GI_PLAN_CODES.contains(election.getPlanCode()) ? election.getPlanCode() : null)
.toList();
// If current election has a GI plan, Make the corresponding GI election nonEditable,
// and remove other GI plans from BenefitPlans.
getEnrollmentDocumentResponse
.getDocument()
.getEligibleBenefitPlansData()
.getBenefitPlans()
.removeIf(benefitPlan -> {
if (GI_PLAN_CODES.contains(benefitPlan.getPlanCode())) {
if (hasElectionWithGIPlanCode.contains(benefitPlan.getPlanCode())) {
benefitPlan.setNonEditable(true);
return false;
}
return true;
}
return false;
});
Can you simplify removeif logic
Assistant
Sure, let's work on simplifying the removeIf logic. We'll break down the steps to clarify the conditions and actions based on whether a plan code is in GI_PLAN_CODES and whether it has been elected.
Here is the original logic:
If the plan code is in , check if there is an election with that plan code ( contains it).
GI_PLAN_CODES
hasElectionWithGIPlanCode
If there is such an election, mark the benefit plan as non-editable and do not remove it (return false).
If there is no such election, remove the benefit plan (return true).
We can simplify this by handling the marking of the plan as non-editable separately from the filtering of the benefit plans. Note that the original code removes plans that are in GI_PLAN_CODES but not in hasElectionWithGIPlanCode.
Simplified logic:
// Mark GI plan elections as nonEditable.
getEnrollmentDocumentResponse
.getDocument()
.getEligibleBenefitPlansData()
.getBenefitPlans()
.forEach(benefitPlan -> {
if (GI_PLAN_CODES.contains(benefitPlan.getPlanCode())
&& hasElectionWithGIPlanCode.contains(benefitPlan.getPlanCode())) {
benefitPlan.setNonEditable(true);
}
});
// Remove GI plans that were not elected.
getEnrollmentDocumentResponse
.getDocument()
.getEligibleBenefitPlansData()
.getBenefitPlans()
.removeIf(benefitPlan ->
GI_PLAN_CODES.contains(benefitPlan.getPlanCode())
&& !hasElectionWithGIPlanCode.contains(benefitPlan.getPlanCode())
);
In this simplified version, we first iterate over every benefitPlan in getBenefitPlans() to mark the relevant ones as non-editable. We avoid removing anything in this step to make it clear that we are only updating the editability status of certain plans.
Next, we use removeIf to filter out the benefitPlans that should be removed. We remove a plan if it is inside GI_PLAN_CODES but not in hasElectionWithGIPlanCode, meaning it's a non-elected GI plan.
This approach accomplishes the same thing but separates the two concerns for clarity: (1) marking non-editable and (2) removing ineligible plans.