问题描述
我有一种方法可以获取用户输入,检查某些值是否存在,然后基于该方法构建我自己的自定义输入对象,用于在数据库中进行搜索。搜索方法的代码如下。
public SearchDocumentResult searchData(EmployeeInput employeeInput) {
EmployeeInput getEmployeeInputForSearch = buildApplicationInputForSearch(employeeInput);
if (employeeInput != null) {
return super.searchForExactData(getEmployeeInputForSearch);
} else {
return super.searchForCloseMatchData(getTestPersonInput);
}
}
对输入进行多个 if 检查的方法如下。下面的方法和上面的方法都存在于同一个类中。
private Application buildApplicationInputForSearch(Application applicationInput) {
Application.Builder applicationForSearch = Application.builder();
String jobIdFromInput = applicationInput.getJobId();
applicationForSearch.withIsTagged(applicationInput.isTagged());
if (!StringUtils.isEmpty(jobIdFromInput)) {
applicationForSearch.withJobId(jobIdFromInput);
}
FormSection formSectionInput = applicationInput.getFormSection();
if (formSectionInput != null) {
this.buildFormSectionInputForSearch(formSectionInput);
}
return applicationForSearch.build();
}
private FormSection buildFormSectionInputForSearch(FormSection formSectionInput) {
FormSection.Builder formSectionForSearch = FormSection.builder();
String formCountry = formSectionInput.getCountry();
Map<String,String> employeeQuestions = formSectionInput.getEmployeeQuestions();
Map<String,String> applicationQuestions = formSectionInput.getApplicationQuestions();
List<String> formNames = formSectionInput.getNames();
if (!StringUtils.isEmpty(formCountry)) {
formSectionForSearch.withCountry(formCountry);
}
if (formNames.size() > 0) {
formSectionForSearch.withNames(formNames);
}
if (employeeQuestions.size() > 0) {
formSectionForSearch.withEmployeeQuestions(employeeQuestions);
}
if (applicationQuestions.size() > 0) {
formSectionForSearch.withApplicationQuestions(applicationQuestions);
}
return formSectionForSearch.build();
}
EmployeeInput
是一个通过库生成的模型类,因此对于可能存在或可能不存在的字段,我无法使用 Java Optional。照原样使用这个 EmployeeInput
对象,我怎样才能使这段代码更具可读性,并减少 if 条件?任何帮助将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)