这可能看起来像一个原始问题,或者可以通过我不知道的简单实用程序库方法来完成。
目的是检查嵌套在两个对象下的布尔字段的值。
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;
}
Run Code Online (Sandbox Code Playgroud)
我知道这可以一次完成if()。if为了可读性,我在这里添加了多个。
有没有一种方法可以简化上面的if语句,并有一个简单的实用工具类返回Boolean source父对象是否为null的值?