[英]How can i rewrite the following java code better way?
我有一小段 java 代码。 我想以更具建设性和优雅的方式重写以下代码。 我怎样才能实现它。?
boolean ifJobTypeIsProfileAndAccountTypeIsNotInternal(PrimecastAccount primecastAccount,Job job) {
if( job.getProfile() != null && job.getContactList() == null && job.getParameterisedContactList() == null) {
return true;
} else if (job.getProfile() == null && job.getContactList() != null && job.getParameterisedContactList() == null) {
return false;
} else if (job.getProfile() == null && job.getContactList() == null && job.getParameterisedContactList() != null) {
return false;
} else if (job.getProfile() == null && job.getContactList() == null && job.getParameterisedContactList() == null) {
log.error("Either a contact list, parameterised contact list or profile not found for the flight : {}", job );
throw new RuntimeException("Either a contact list, parameterised contact list or profile not found for the flight");
} else {
log.error("Found both contact list/parameterised list and profile for the flight : {}", job );
throw new RuntimeException("Found both contact list/parameterised list and profile for the flight");
}
return true;
}
非常感谢您的帮助
如果您使用的是 Java 8 或更高版本,您可以使用流、 Objects#nonNull
和Objects#isNull
public boolean isProfileTypeJobWithNotInternalAccountType(Job job){
var profile = job.getProfile();
var contList = job.getContactList();
var paCoList = job.getParameterisedContactList();
var allNull = Stream.of(profile,contList,paCoList).allMatch(Objects::isNull);
var twoNotNull = Stream.of(profile,contList,paCoList).filter(Objects::nonNull).count() > 1;
if(allNull){
log.error("Either a contact list, parameterised contact list or profile not found for the flight : {}", job );
throw new RuntimeException("Either a contact list, parameterised contact list or profile not found for the flight");
}
if(twoNotNull){
log.error("Found both contact list/parameterised list and profile for the flight : {}", job );
throw new RuntimeException("Found both contact list/parameterised list and profile for the flight");
}
return Objects.nonNull(profile);
}
首先处理可能的错误。 之后,剩下的情况在这里很容易清理。
boolean ifJobTypeIsProfileAndAccountTypeIsNotInternal(PrimecastAccount primecastAccount,Job job) {
if (job.getProfile() == null && job.getContactList() == null && job.getParameterisedContactList() == null) {
log.error("Neither a contact list, parameterised contact list, nor profile found for the flight : {}", job );
throw new RuntimeException("Neither a contact list, parameterised contact list, nor profile found for the flight");
}
if (job.getProfile() != null && (job.getContactList() != null || job.getParameterisedContactList() != null)){
log.error("Found both contact list/parameterised list and profile for the flight : {}", job );
throw new RuntimeException("Found both contact list/parameterised list and profile for the flight");
}
return job.getProfile() != null;
}
遗憾的是,您将无法绕过错误条件。
static boolean if2(Job job) {
boolean eitherContactPresent = job.getContactList() == null || job.getParameterisedContactList() == null;
boolean bothContactAbsent = job.getContactList() == null && job.getParameterisedContactList() == null;
if (job.getProfile() != null) {
if (bothContactAbsent) return true;
else if (!eitherContactPresent) throw new RuntimeException("ALL present");
}
if (bothContactAbsent) throw new RuntimeException("None present");
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.