user3145373 ツ
user3145373 ツ

Reputation: 8156

Is it Possible to Check Received fields are Empty or Null Without If

I am receiving list of fields. Near About to 60 fields.

From that I have to check 50 fields that are they null or empty, if not then I ll have to add them also in DB table.

Right now I am doing it manually using if condition. I am just thinking to do so, not implemented still yet.

Is there any better option then it ?

My Code :

if(ValidateData.checkIsNullOrEmpty(command.getSubscriptionStartYear())){

}
if(ValidateData.checkIsNullOrEmpty(command.getSubscriptionPeriod())){

}
if(ValidateData.checkIsNullOrEmpty(command.getExpectedArrivalTimeOfIssues())){

}
.....
.....

if(ValidateData.checkIsNullOrEmpty(command.getMaxNoOfClaims())){

}

Here command is class which receives Data from source.

Here ValidateData is a class :

It's method definition :

public static boolean checkIsNullOrEmpty(Integer arg){
    if(arg != null) return true;
    return false;
}

public static boolean checkIsNullOrEmpty(String arg){
     if(!arg.trim().equals("") || !arg.trim().equals(" ") || arg.trim() != null) return true;
     return false;
}

If anyone guide me or suggest me that is there any better option available ??

Upvotes: 1

Views: 3137

Answers (3)

No Idea For Name
No Idea For Name

Reputation: 11607

create a function like this:

public static bool AllNull(object... something)
{
    for(var v :something)
       if(v!=null){
         if(v instanceof Integer)
           // do integer validation
       }else
         //Err msg
}

Then you could call it like this:

if (AllNull(obj1, obj2, obj3, obj4, obj5, obj6))
{
    // ...
}

if you want to be specific, separate strings and integers and make separate function like this one for each type you need

Edit

as i understod from your comment, u don't know varargs

varargs are useful for any method that needs to deal with an indeterminate number of objects. One good example is String.format.

Upvotes: 1

Manh Trinh
Manh Trinh

Reputation: 56

I think best solution for your problem is using Java Reflect. Here is sample code to validate all field of an instance by Java Reflect. Example I have one instance(pojo) of object PojoObj.

PojoObj pojo = new PojoObj("one1", 2, null, 4, "five", "Six");

Validate all fields by Java Reflect.

Class<PojoObj>  aClass = PojoObj.class;
      Field[] fields = aClass.getDeclaredFields();
      for(Field field : fields) {
          Object value = field.get(pojo);
          Object type = field.getType();
          if(value == null) {
              System.out.println(field.getName() + " is null");
          } else {
              System.out.println(field.getName() + " is instanceof " + type + " and value = " + value);
          }
      }

Output:

fieldOne is instanceof class java.lang.String and value = one1
fieldTwo is instanceof long and value = 2
fieldThree is null
fieldFour is instanceof int and value = 4
fieldFive is instanceof class java.lang.String and value = five
fieldSix is instanceof class java.lang.String and value = Six

Upvotes: 0

linuxlsx
linuxlsx

Reputation: 138

if you can edit command, you can mark each field that you want to check null with @NotNull, then use java reflect api to get all fields marked with @NotNull, and check whether some fields null or not

Upvotes: 0

Related Questions