[英]How to avoid deeply if then statements to avoid warnings in pmd?
是这样的方式,以便我可以减少下面的代码的if语句
if (presenterManager != null) {
IFieldPresenter modeFieldPresenter = presenterManager.getFieldPresenter(ATTR_MODE);
if (modeFieldPresenter != null) {
String modeLV = ((ListBoxValue)modeFieldPresenter.getState().getSingleValue()).getValue();
String customerAccountPK = getContext().getRequestParam("parentPK");
String customerAccountId = toObjectId(customerAccountPK);
LOG.debug(" modeLV = "+modeLV);
LOG.debug( "customerAccountId = "+customerAccountId);
if(!LV_AUTOMATIC.equals(modeLV)) {
Window.open(CONFIGURE_URL_PREFIX + customerAccountId, "_blank", "");
return getInitialEvent();
}
}
}
我想深入避免if(!LV_AUTOMATIC.equals(modeLV))这个if语句。
在Java 8中,您可以执行此操作。
Optional<InitalEvent> event = Optional.ofNullable(presenterManager)
.map(p -> p.getFieldPresenter(ATTR_MODE))
.map(p -> (ListBoxValue)p.getState().getSingleValue()).getValue())
.filter(!LV_AUTOMATIC.equals(modeLV))
.map(modeLV -> {
String customerAccountId = toObjectId(getContext().getRequestParam("parentPK"));
LOG.debug(" modeLV = "+modeLV+", customerAccountId = "+customerAccountId);
Window.open(CONFIGURE_URL_PREFIX + customerAccountId, "_blank", "");
return getInitialEvent();
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.