【问题标题】:Alternative approach for checking data types to build an object [closed]检查数据类型以构建对象的替代方法[关闭]
【发布时间】:2020-11-28 09:52:09
【问题描述】:

我正在尝试构建一个对象,该对象根据不同的数据类型使用不同的方法。 IE。它与withBooleanValuewithStringValuewithDateValue 不同,具体取决于数据类型。我拥有的是一个字符串,根据该字符串是什么(布尔值、字符串或日期),我需要构建这个对象。以下是我的做法。

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 语句使它看起来非常复杂,我想用更好的方法来减少行数和复杂性。任何建议将不胜感激。

【问题讨论】:

    标签: java if-statement optimization types java-8


    【解决方案1】:

    就重构代码而言,我会从这样的事情开始(它不会编译,因为我没有所有的类,所以可能会出现一些错误):

        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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-06
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 2011-12-15
      • 2020-07-01
      • 2013-12-18
      相关资源
      最近更新 更多