I'm trying to build an object, which uses different methods based on different data types. I.e. it differs from withBooleanValue
,withStringValue
and withDateValue
depending on the data type. What I have is a string, and depending on what that string is (boolean, string or date), I need to build this object. Below is how I went about it.
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;
}
Is there a better way to do this in Java8? Somehow the ifs, and try-catch statements make it look very complicated and I'd like to reduce the lines and complexity with a better way. Any advice would be much appreciated.
In terms of just refactoring the code, I would start with something like this (it doesn't compile since I don't have all the classes, so may have some errors):
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();
}
Then something like this. Also it would require having a global date, which may be questionable. Also instead of passing question around, it's possible to pass question.value() instead.
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;
}
}