【发布时间】:2019-09-01 23:25:15
【问题描述】:
这可能看起来像一个原始问题,或者这可以通过一个我不知道的简单实用程序库方法来完成。
目标是检查嵌套在两个对象下的布尔字段的值。
private boolean sourceWebsite(Registration registration) {
Application application = registration.getApplication();
if (application == null) {
return true;
}
Metadata metadata = application.getMetadata();
if (metadata == null) {
return true;
}
Boolean source = metadata.getSource();
if (source == null) {
return true;
}
return !source;
}
我知道这可以在单个 if() 中完成。为了便于阅读,我在这里添加了多个ifs。
有没有一种方法可以简化上述if 语句,并有一个简单的实用程序类,如果父对象或不为空,则返回Boolean source 的值?
【问题讨论】:
-
从 apache utils 中尝试 StringUtils
标签: java if-statement conditional