Java 8 Alternative for validate data inside multiple nested loop

  • A+
Category:Languages

Not sure whether i should ask it here, tried Code Review section but not there, i have a question regarding validate data in a nested for loop.

public class Object1{   private String obj1Name;    private String obj1Desc;    private List<Object2> object2List;    //Setters and getters }   public class Object2{   private String obj2Name;    private String obj2Desc;    private List<Object3> object3List;    //Setters and getters }  public class Object3{   private String obj3Name;    private String obj3Desc;   //Setters and getters } 

I wish to validate both name and desc in all object, instead of using nested loop like following:

List<Object1> object1List = getObject1List();  for(Object1 object1 : object1List ){    if(object1.getObj1Name() == null){      //throw error    }     if(object1.getObj1Desc() == null){      //throw error    }     for(Object2 object2 : object1.getObject2List()){         if(object2.getObj2Name() == null){             //throw error         }          if(object2.getObj2Desc() == null){             //throw error         }          //loop Object 3 ...    } } 

Is there any ways to do it better?

 


EDIT: An idea to externalize the check, you need to create Functional Interface

ObjectValidatorInterface ov = new ObjectValidator(); if(!object1List.stream().allMatch(o -> ov.validate(o, Object1.class))) {         // throw error; } 

And the interface

@FunctionalInterface interface ObjectValidatorInterface {     boolean validate(Object object, Class clazz); }  class ObjectValidator implements ObjectValidatorInterface {      public boolean validate(Object object, Class clazz) {         boolean valid = false;          if(Object1.class.getName().equals(clazz.getName())) {             valid = validateObject1((Object1) object);          } else if(Object2.class.getName().equals(clazz.getName())) {             valid = validateObject2((Object2) object);          } else if(Object3.class.getName().equals(clazz.getName())) {             valid = validateObject3((Object3) object);         }          return valid;     }      private boolean validateObject1(Object1 o) {         boolean valid;         valid = o.getObj1Name() != null && o.getObj1Desc() != null;         if(!(o.getObject2List() == null || o.getObject2List().isEmpty())) {             valid = valid && o.getObject2List().stream().allMatch(o2 -> validate(o2, Object2.class));         }         return valid;     }      private boolean validateObject2(Object2 o) {         boolean valid;         valid = o.getObj2Name() != null && o.getObj2Desc() != null;         if(!(o.getObject3List() == null || o.getObject3List().isEmpty())) {             valid = valid && o.getObject3List().stream().allMatch(o3 -> validate(o3, Object3.class));         }         return valid;     }      private boolean validateObject3(Object3 o) {         return o.getObj3Name() != null && o.getObj3Desc() != null;     } } 

ORIGINAL ANSWER

You might be able to do it by delegating the validation to each object:

List<Object1> object1List = getObject1List();  if(!object1List.stream().allMatch(Object1::isValid)) {     //throw error } 

And add an isValid method to each object

public class Object1 {     private String obj1Name;     private String obj1Desc;     private List<Object2> object2List;      public boolean isValid() {         return obj1Name != null             && obj1Desc != null             && object2List.stream().allMatch(Object2::isValid);     } }  public class Object2 {     private String obj2Name;     private String obj2Desc;     private List<Object3> object3List;      public boolean isValid() {         return obj2Name != null             && obj2Desc != null             && object3List.stream().allMatch(Object3::isValid);     } }  public class Object3 {     private String obj3Name;     private String obj3Desc;      public boolean isValid() {         return obj3Name != null             && obj3Desc != null;     } } 

Comment

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