我怎样才能更好地重写以下Java代码?

Knowledgeseeker001:

我有一小段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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我怎样才能更好地使用 purrr 在 R 中重构以下代码

我怎样才能更好地抽象?

Ruby - 我怎样才能更好地写这行?

我怎样才能更好地转换我的代码

VBA 脚本,我怎样才能更好地编写此代码?运行时错误 13,类型不匹配

我怎样才能看到Scala编译器在什么[Java / Scala?]代码中重写了原始的Scala代码

我怎样才能加快我的代码?

我怎样才能让它更好地使用这个 json 检查器功能?PYTHON

我有以下测试代码。我怎样才能使这个条件为真

我怎样才能使这个代码更好?因为它现在不起作用

我怎样才能更均匀地调整我的导航栏,我怎样才能减少一些代码?(感谢您的格式帮助)

我怎样才能使这个gsub更好?

我怎样才能将此CSS重写为XPath

我怎样才能用Java 8个流API转换我的代码?

我怎样才能更优雅地进行_.has检查?

我怎样才能最好地实现这个 React Transition?

我怎样才能真正地执行这个?

我怎样才能清楚地猜猜游戏?

我怎样才能完美地翻转那个图像?

我怎样才能让我的代码满足这个要求

我怎样才能缩短这个替代密码的代码?

我怎样才能使这个 Javascript 代码减少重复?

我怎样才能改变这个MySQL代码为PDO?

我怎样才能加快这个python代码的速度?

我怎样才能简化这样的 oneliner javascript 代码

我怎样才能使这段代码更短?飞峰

我怎样才能“clipToBottomBounds”?

我怎样才能在谷歌Colab使用Java

我怎样才能在Windows上运行的Java?