我正在尝试建立一个对象,该对象根据不同的数据类型使用不同的方法。即,它不同于和withBooleanValue
,withStringValue
并withDateValue
取决于数据类型。我所拥有的是一个字符串,并且取决于该字符串是什么(布尔值,字符串或日期),我需要构建此对象。以下是我的处理方法。
private List<Answer> getAnswers(Set<Question> questions) {
List<Answer> answers = new ArrayList<>();
questions.forEach(question -> {
Answer.Builder answer = Answer.builder()
.withQuestion(question.question());
if (BooleanUtils.toBooleanObject(question.value()) != null) {
answer.withValue(AnswerValue.builder()
.withBooleanValue(BooleanUtils.toBoolean(question.value()))
.build());
} else {
try {
Date dateValue = DateUtils.parseDate(question.value(), new String[]{"dd-MM-YYYY"});
answer.withValue(AnswerValue.builder()
.withDateValue(dateValue)
.build());
} catch (ParseException e) {
answer.withValue(AnswerValue.builder()
.withStringValue(question.value())
.build());
}
}
answers.add(answer.build());
});
return answers;
}
在Java8中是否有更好的方法可以做到这一点?不知何故,ifs和try-catch语句使它看起来非常复杂,我想通过一种更好的方法来减少行数和复杂性。任何建议将不胜感激。
就重构代码而言,我将从这样的事情开始(由于我没有所有的类,所以它无法编译,因此可能会有一些错误):
private List<Answer> getAnswers(Set<Question> questions) {
List<Answer> answers = new ArrayList<>();
questions.forEach(question -> {
Answer.Builder answer = Answer.builder()
.withQuestion(question.question())
.withValue(parseAnswerValue(question));
answers.add(answer.build());
});
return answers;
}
private AnswerValue parseAnswerValue(Question question) {
Optional<AnswerValue> answerOptional = parseBoolean(question);
if (answerOptional.isEmpty()) {
answerOptional = parseDate(question);
}
return answerOptional.isEmpty() ? parseString(question) : answerOptional.get();
}
private Optional<AnswerValue> parseBoolean(Question question) {
if (BooleanUtils.toBooleanObject(question.value()) != null) {
return Optional.of(AnswerValue.builder()
.withBooleanValue(BooleanUtils.toBoolean(question.value()))
.build());
}
return Optional.empty();
}
private Optional<AnswerValue> parseDate(Question question) {
try {
Date dateValue = DateUtils.parseDate(question.value(), new String[]{"dd-MM-YYYY"});
return Optional.of(AnswerValue.builder()
.withDateValue(dateValue)
.build());
} catch (ParseException e) {
return Optional.empty();
}
}
private AnswerValue parseString(Question question) {
return AnswerValue.builder()
.withStringValue(question.value())
.build();
}
然后是这样的。同样,这将需要有一个全球日期,这可能是有问题的。同样,除了传递问题之外,还可以传递question.value()。
private List<Answer> getAnswers(Set<Question> questions) {
List<Answer> answers = new ArrayList<>();
questions.forEach(question -> {
Answer.Builder answer = Answer.builder()
.withQuestion(question.question())
.withValue(parseAnswerValue(question));
answers.add(answer.build());
});
return answers;
}
private AnswerValue parseAnswerValue(Question question) {
AnswerValue.Builder answerBuilder = AnswerValue.builder();
if (isBoolean(question)) {
answerBuilder.withBooleanValue(BooleanUtils.toBoolean(question.value()));
} else if (isDate(question)) {
answerBuilder.withDateValue(dateValue);
} else {
answerBuilder.withStringValue(question.value());
}
return answerBuilder.build();
}
private boolean isBoolean(Question question) {
return BooleanUtils.toBooleanObject(question.value()) != null;
}
private boolean isDate(Question question) {
try {
dateValue = DateUtils.parseDate(question.value(), new String[]{"dd-MM-YYYY"});
return true;
} catch (ParseException e) {
return false;
}
}