Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check which combinations of parameters are null in Java

I am new to Java. I am facing an issue now in which I couldn't find the easiest and cleanest way of solving it.

Suppose I have 3 parameters(string) passed to a function(could be a Hashmap too).I want to check if individual variable or combination of variables is not Null and act accordingly.

For example one way to do this is using if-else this way

if(a!=null && b == null && c == null) { 
      //doSomething   
}
 else if(a==null && b!= null && c == null ) { 
     //doSomething 
}
 else if(a==null && b0= null && c != null) { 
     //doSomething 
}
  ......
    //Similarly combination of two variables
 if(a!=null && b != null && c == null) {
     //doSomething 
}
 else if(a!=null && b== null && c != null) { 
    //doSomething
}
 else if(a==null && b!= null && c != null) { 
    //doSomething 
} 
  ......
    //and so on 
    //Similarly combination of three variables
 if(a!=null && b != null && c != null) {
    //doSomething 
}

   ....

How to achieve this kind of situation. I found similar question, but didn't make the code clean. Any help will be appreciated

like image 979
AJ007 Avatar asked Nov 23 '18 10:11

AJ007


2 Answers

Write these utility functions and you can compare n terms easily.

public static boolean areAllNull(Object... objects) {
     return Stream.of(objects).allMatch(Objects::isNull);
}

public static boolean areAllNotNull(Object... objects) {
     return Stream.of(objects).allMatch(Objects::nonNull);
}

you can use these functions for n comparisons.

if(areAllNotNull(a) && areAllNull(b,c)) { 
      //doSomething   
}
 else if(areAllNotNull(b) && areAllNull(a,c)) { 
     //doSomething 
}
 else if(areAllNotNull(c) && areAllNull(b,a)) { 
     //doSomething 
}
like image 173
Khalid Shah Avatar answered Oct 12 '22 15:10

Khalid Shah


This is my solution. Note, that you have multiple if...else in one single method. And then you add doSomething. This is going to be terrible to ready and later to realize.

What about to move one single condition into separate method and name it with relative name. Then, lets encapsulate it into Consumer and all of it into a predefined list. Later, if your doSomething will be huge, then you can move from single method to single class, not modifying client code.

This is class, to collect required variable for conditions:

final class Data {

    private final String a;
    private final String b;
    private final String c;
}

Then define one Consumer per on if statement:

Consumer<Data> wonderfulConsumer = data -> {
    if (a != null && b == null && c == null) {
        // do something for wonderful consumer
    }
};

Consumer<Data> badLuckConsumer = data -> {
    if (a == null && b != null && c == null) {
        // do something for bad luck consumer
    }
};

Note, all these consumers could be modified separately (even be in the different classes).

Then in the client code, define list of all known consumers: List<Consumer<Data>> consumers = Arrays.asList(wonderfulConsumer, badLuckConsumer).

And finally your method will be like this and you do not need to change it when you decide to modify or add consumers.

Data data = new Data(a, b, c);
consumers.forEach(consumer -> consumer.accept(data));
like image 34
oleg.cherednik Avatar answered Oct 12 '22 15:10

oleg.cherednik