我有一小段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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句