Refactor multiple If' statements in Java-8

  • A+
Category:Languages

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<Integer, Optional<?>> 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<count; 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]))); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: