I have an java POJO class as below.
public class EmployeeVo {
private String firstName;
private String middleName;
private String lastName;
private String suffix;
private String addressline1;
private String addressline2;
private String city;
private String state;
private String zipCode;
private String country;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
.// Setters and getters for other fields.
.
.
}
Is there any way to check all the fields of an object at once for null , rather than checking each field for null?
While retrieving these values from EmployeeVo object
public static boolean isEmpty(String string){
if(null==string || string.trim().length()==0){
return true;
}
return false;
}
I have to manually null check each field before using it some where . As the above Object has more than 10 fields it would obviously increase the Cyclomatic Complexity for that method. Is there any optimal solution for it?
if(CommonUtil.isNotEmpty(employeeVO.getCity())){
postalAddress.setCity(employeeVO.getCity());
}
if(CommonUtil.isNotEmpty(employeeVO.getCity())){
postalAddress.setState(employeeVO.getState());
}
if(CommonUtil.isNotEmpty(employeeVO.getZipCode())){
postalAddress.setPostalCode(employeeVO.getZipCode());
}
if(CommonUtil.isNotEmpty(employeeVO.getCountry())){
postalAddress.setCountryCode(employeeVO.getCountry());
}
Can we use
String country=employeeVO.getCountry();
postalAddress.setCountryCode( (country!=null) ? country: "");
Just to reduce the complexity? Is this according to standards?