I have an Optional Object named Form. This has 3 other optional properties to it. Reason being, if this form object is not present, I have to generate my own form data. If the form is present, users can still choose to leave their properties empty, requiring my method to generate them in that case. The way that I have implemented this logic goes as follows.
private FormSection buildForm(final Optional<Form> formOptional) {
Set<String> formNames = null;
Set<Question> formTypes = null;
Set<Question> formDetails = null;
if (formOptional.isPresent()) {
Form form = formOptional.get();
formNames = formSection.names()
.orElseGet(() -> getNamesFromDB());
formTypes = formSection.formTypes()
.orElseGet(() -> getFormTypesFromDB());
formDetails = formSection.formDetails()
.orElseGet(() -> getFormDetailsFromDB());
} else {
formNames = getNamesFromDB();
formTypes = getFormTypesFromDB();
formDetails = getFormDetailsFromDB();
}
return Form()
.names(formNames)
.formTypes(formTypes)
.formDetails(formDetails)
.build();
}
I feel like I'm redoing some checks and that this whole if-else statement could be optimized. I'm using Java 8 by the way.