java 多if的解决方案,在Java-8中重构多个If语句

I need to validate mandatory fields in my class

For example, 9 fields must not be null.

I need to check if they are all null but I am using multiple if statements for this now as below:

StringBuilder mandatoryExcessFields = new StringBuilder(MANDATORY_EXCESS_FIELDS.length);

if(Objects.isNull(excess.getAsOfDate())){

mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[0]);

}

if(StringUtils.isEmpty(excess.getStatus())) {

mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[1]);

}

if(Objects.isNull(excess.getLimit())) {

mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[2]);

}

if(!Objects.isNull(excess.getLimit()) && Objects.isNull(excess.getLimit().getId())) {

mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[3]);

}

if(!Objects.isNull(excess.getLimit()) && Objects.isNull(excess.getLimit().getAsOfDate())) {

mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[4]);

}

if(Objects.isNull(excess.getExposure())) {

mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[5]);

}

if(!Objects.isNull(excess.getExposure()) && Objects.isNull(excess.getExposure().getCoordinates())) {

mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[6]);

}

if(!Objects.isNull(excess.getExposure()) && Objects.isNull(excess.getExposure().getValue())) {

mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[7]);

}

if(StringUtils.isEmpty(excess.getLimitValue())) {

mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[8]);

}

Do we have a better approach to reduce this boilerplate code or any design pattern or any new feature from Java-8 which I can leverage?

解决方案

All the Object.isNull might be replaced with Optional object and its methods. Let's take example the line:

if (!Objects.isNull(excess.getLimit()) && Objects.isNull(excess.getLimit().getId())) {

mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[3]);

}

Would be simplified to (and squeezed on 1 line remains readable):

Optional.ofNullable(excess.getLimit()) // check the Limit

.map(limit -> limit.getId()) // if not null, getId

.ifPresent(i -> builder.append(MANDATORY_EXCESS_FIELDS[3])); // Append if present

And for the String.isEmpty(s) check, you have to create Optional in this way:

Optional.ofNullable(excess.getStatus()).filter(s -> !StringUtils.isEmpty(s))

A short way would be to pass those Optional object into the map and use the index to iterate through them and perform an action. int count is a number of checkings:

Map> map = new HashMap<>();

map.put(...);

map.put(1, Optional.ofNullable(excess.getStatus()).filter(s -> !StringUtils.isEmpty(s)));

map.put(...);

map.put(3, Optional.ofNullable(excess.getLimit()).map(limit -> limit.getId()));

map.put(...);

for (int index=0; index

map.get(index).ifPresent(any -> mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[index]));

}

And the for-cycle might be simplified as well:

IntStream.range(0, count).forEach(index ->

map.get(index)

.ifPresent(any -> mandatoryExcessFields.append(MANDATORY_EXCESS_FIELDS[index])));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值