Java 带有多个条件的长if-else语句-如何替换它?

Java 带有多个条件的长if-else语句-如何替换它?,java,Java,我有一个很长的问题,如果否则,但我真的不知道如何更换它 List<Comment> commentList; if (EnumUtils.isValidEnum(Comment.State.class, state) && ordered && petUuid == null) { commentList = commentRepository.findByStateOrderByCreatedDesc(Comment.State.valueO

我有一个很长的问题,如果否则,但我真的不知道如何更换它

List<Comment> commentList;
if (EnumUtils.isValidEnum(Comment.State.class, state) && ordered && petUuid == null) {
    commentList = commentRepository.findByStateOrderByCreatedDesc(Comment.State.valueOf(state));
} else if (EnumUtils.isValidEnum(Comment.State.class, state) && !ordered && petUuid == null) {
    commentList = commentRepository.findByState(Comment.State.valueOf(state));
} else if (state == null && ordered && petUuid == null) {
    commentList = commentRepository.findAllByOrderByCreatedDesc();
} else if (state == null && !ordered && petUuid == null) {
    commentList = commentRepository.findAll();
} else if (EnumUtils.isValidEnum(Comment.State.class, state) && !ordered && petUuid != null) {
    commentList = commentRepository.findByPetUuidAndByState(petUuid, Comment.State.valueOf(state));
} else if (EnumUtils.isValidEnum(Comment.State.class, state) && ordered && petUuid != null) {
    commentList = commentRepository.findByPetUuidAndByStateOrderByCreatedDesc(petUuid, Comment.State.valueOf(state));
} else if (state == null && !ordered && petUuid != null) {
    commentList = commentRepository.findByPetUuid(petUuid);
} else if (state == null && ordered && petUuid != null) {
    commentList = commentRepository.findByPetUuidOrderByCreatedDesc(petUuid);
} else {
    throw new WrongEnumValueException(Comment.State.class);
}
列表注释列表;
if(EnumUtils.isValidEnum(Comment.State.class,State)&&ordered&&petUuid==null){
commentList=commentRepository.findByStateOrderByCreatedDesc(Comment.State.valueOf(State));
}else if(EnumUtils.isValidEnum(Comment.State.class,State)&&&!ordered&&petUuid==null){
commentList=commentRepository.findByState(Comment.State.valueOf(State));
}else if(state==null&&ordered&&petUuid==null){
commentList=commentRepository.findAllByOrderByCreatedDesc();
}else if(state==null&&!ordered&&petUuid==null){
commentList=commentRepository.findAll();
}else if(EnumUtils.isValidEnum(Comment.State.class,State)&&&!ordered&&petUuid!=null){
commentList=commentRepository.FindBypetuidByState(petUuid,Comment.State.valueOf(State));
}else if(EnumUtils.isValidEnum(Comment.State.class,State)&&ordered&&petUuid!=null){
commentList=commentRepository.FindBypetuidByStateOrderByCreatedDesc(petUuid,Comment.State.valueOf(State));
}else if(state==null&&!ordered&&petUuid!=null){
commentList=commentRepository.findBypetuid(petUuid);
}else if(state==null&&ordered&&petUuid!=null){
commentList=commentRepository.findBypetuidorderbyCreatedDesc(petUuid);
}否则{
抛出新的错误EnumValueException(Comment.State.class);
}

我在谷歌上读到“适用于多if-is-switch”语句,但我这里有多个条件,所以我不知道如何解决这个问题:/我需要比这个if-else长语句更好的解决方案,因为它太难看了。

switch/Case不会让你的语句更简单。研究布尔代数以及如何简化它,例如使用卡诺图


由于代码中有大量重复(state==null),您可能会得到一些嵌套的
if语句,这些语句至少会使代码更具可读性

看起来您正在尝试创建一个超级上帝方法,它应该处理所有可能的参数组合?这很难理解和支持。您应该重构代码以避免使用此类方法