1

I have fields like

variable1,variable2,variable3....etc

and my conditions are

if(variable1 != null && variable1 != 0 )
  myobject.setFirstValue(variable1);

if(variable2 != null && variable2 != 0 )
 myobject.setSecondValue(variable2);

if(variable3 != null && variable3 != 0 )
 myobject.setThirdValue(variable3);

Like this I have nearly 15 to consitions. Is there any way to write all these conditions in a simple way.The variable names are not exactly same what I posted . They are different from what I posted.

12
  • 6
    Do you really need 15 variables rather than an array? And do you really want to call setFirstValue multiple times if multiple variables are non-null and non-zero?
    – Jon Skeet
    Apr 10, 2013 at 11:35
  • what is the nature of your program ? what exactly are you doing with those variables ?
    – giorashc
    Apr 10, 2013 at 11:36
  • i am sending like state ,city ,street ,name values.I need to set those values if not null
    – PSR
    Apr 10, 2013 at 11:37
  • @ Jon Skeet if i put in list or array how i know which variable i need to set which setter method
    – PSR
    Apr 10, 2013 at 11:41
  • You can use reflection but this can turn to be a bit nasty if not used properly. Highly depends on the real variable names and setter names
    – giorashc
    Apr 10, 2013 at 11:42

6 Answers 6

5

You could start with ...

if (notNullOrZer0(variable1)) {
   ...
}

private boolean notNullOrZero(Integer x) {
    return x != null && x != 0;
}

Then maybe put all these variables in a List<Integer> ... it depends on how the rest of your code is structured ...

4
  • i updated my question.Please see it once.I posted some mistake
    – PSR
    Apr 10, 2013 at 11:40
  • if i put in list how i know which variable i need to set which setter method
    – PSR
    Apr 10, 2013 at 11:41
  • Are all your variables the same type?
    – blank
    Apr 10, 2013 at 11:42
  • So you have a few variables which you want to construct an object out of? Or are you updating an object? How many variables?
    – blank
    Apr 10, 2013 at 11:43
5

you can create a method isNotNullorequalsZero which checks not null and not equals zero

private boolean isNotNullorequalsZero(Integer value){
     return value != null && value != 0;
}

and you use that -

if(isNotNullorequalsZero(variable1) )
  myobject.setFirstValue(variable1);

if(isNotNullorequalsZero(variable2) )
 myobject.setFirstValue(variable2);

if(isNotNullorequalsZero(variable3) )
 myobject.setFirstValue(variable3);
2
  • 1
    Why the boilerplate code return true..false. you could change your code by: return value != null && value != 0;;)
    – Mik378
    Apr 10, 2013 at 11:40
  • +1 for your answer.It is also correct.But i can accept only one answer
    – PSR
    Apr 10, 2013 at 11:52
0

You could make a method called something like copyIfNonZero, and it would probably look like this:

public void copyIfNonZero(MyObject myObject, Integer value) {
    if (value != null && value != 0) {
        myObject.setFirstValue(value);
    }
}

And current code would be reduced to

copyIfNonZero(myobject, variable1);
copyIfNonZero(myobject, variable2);
copyIfNonZero(myobject, variable3);
...

Or even put the variables in a Collection and iterate over that:

Collection<Integer> values = .....
for(Integer value : values) {
    copyIfNonZero(myObject, value);
}
2
  • As it was in the first version of the question... :) But indeed, with the updated question, this approach isn't particularly useful.
    – mthmulders
    Apr 10, 2013 at 11:59
  • oh, didn't see the first version :) Apr 10, 2013 at 12:04
0

either modify setFirstValue method as

void setFirstValue(Integer var){
if(var !=null && var !=0) {
//current logic of setFirstValue
}
}

or if not possible to modify it, create a wrapper call as (return type boolean to notify user if call fails)

boolean checkAndSetFirstValue(Integer var) {
    if(var !=null && var !=0){
        setFirstValue(var);
        return true;
    }
    return false;
}
2
  • But you are hiding the fact that if something is wrong the caller will not be notified about it. You should throw an exception in such case
    – giorashc
    Apr 10, 2013 at 11:38
  • yes thnx for pointer, but i would rather prefer alternate approach as i edit the code.
    – Ankit
    Apr 10, 2013 at 11:43
0

You can put all the variables in an array and use a for loop to check the condition and set the values in myobject.setFirstValue(array[index]);

1
  • but my setter methods are not same.How i can set
    – PSR
    Apr 10, 2013 at 11:45
0

If this is something that really occurs frequently in your code and you want to encapsulate this behaviour and avoid to repeat it, I would do something like that :

In a tools or utils class, create a static method :

public static void setPropertyIfNonNullNorZero(Object target, String propertyName, Integer value){

    //use some reflection tooling here, or write the whole thing yourself... I am  using commons.beanutils here

    if(value != null && value != 0){
        BeanUtils.setProperty(target, propertyName, value);
    }

}

This code assumes that your target object (myObject) respects the bean conventions (basically consitent getters and setters naming) and the the value is always an Integer.

Then use it like this :

Tools.setPropertyIfNonNullNorZero(myobject, "thirdValue", variable3);
2
  • what happens when you refactor a property name?
    – blank
    Apr 10, 2013 at 14:15
  • Of course, if you rename a property you have to change the property name manually in the method call(s), unless your refactoring tool is smart enough to detect these indirect references... It is the same as many other cases where beans properties are referenced by their name and not direct calls to the getter method by java code (e.g. HQL / JPAQL, JSP/JSTL, etc.) Apr 11, 2013 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.