0
我有一個很長的問題,但是我真的不知道如何替換它。Long if else具有多個條件 - 如何替換它?
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 else語句,因爲更好的解決方案這只是醜陋的。
看起來你想創建一個應該處理所有可能的參數組合的超級神方法嗎?這是非常難以閱讀和支持的。你應該重構你的代碼以避免使用這種方法 –