QuestionEverything
QuestionEverything

Reputation: 5097

How to check any two parameters of a function are null

Suppose I have a function that takes three parameters. Of course I could check that any two parameters of this function is null in this way.

returnType function(p1, p2, p3){

  if((p1 == null && p2 == null) || (p2 == null && p3 == null) || (p3== null && p1 == null)){
       return;    
   }

}

But this is rather cumbersome and wouldn't scale for larger number of parameters.

What would be the elegant way to do this?

Thanks in advance.

Upvotes: 2

Views: 2080

Answers (7)

Gyanendra Dwivedi
Gyanendra Dwivedi

Reputation: 5538

Pass your parameter as public int someMethod(Object... param){// your code }

You will get the param as array of Object, then iterate through array to check nulls. As soon as your count reaches 2, break the loop and declare a failure.

static int check(Object... param)
{
   int count= 0;
   for (int i = 0; i < param.length; i++)
        // check param[i] and increase count
   if(param[i] == null){
      count++;
   }
   // return count, this is the number of null parameters to the function.
   return count;
}

Edit 1:

If you are new to variable arguments in java, I would recommend to read https://today.java.net/pub/a/today/2004/04/19/varargs.html

Upvotes: 2

upog
upog

Reputation: 5531

try this just another way of checking,

boolean isAtleastTwoNull = (p1==null )? (p2== null || p3==null): (p2==null && p3==null);

Upvotes: 0

MPavesi
MPavesi

Reputation: 971

A smart solution with Java 5 is a varargs validation function.

You can create an utility validation function which takes varargs Object arguments, and check if they are all not null.

Then you can iterate on the array and stop the cycle when you find more than two parameters are null.

boolean validate(int maxNull, Object... params) {
    int count = 0;
    if (params == null) {
        return true;
    }
    for (Object param : params) {
        if (param == null) {
            count++;
        }
        if (count >= maxNull) {
            return false;
        }
    }
    return true;
}

Using this utility function you can check all the parameters and also change the strategy of the test when you'll find a more efficient one.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

But this is rather cumbersome and wouldn't scale for larger number of parameters.

Moreover, this wouldn't compile. You need to add comparison with null for each parameter, like this:

if((p1 == null && p2 == null )
|| (p2 == null && p3 == null)
|| (p3 == null && p1 == null)) ...

If you need to scale to a larger number of parameters, do this:

int count = 0;
if (p1 == null) count++;
if (p2 == null) count++;
if (p3 == null) count++;
// ... and so on
if (count >= 2) {
    ...
}

Upvotes: 4

Oleg Pyzhcov
Oleg Pyzhcov

Reputation: 7353

returnType function(p1, p2, p3){
   Object[] args = {p1, p2, p3};
   if(Collections.frequency(Arrays.asList(args), null) >= 2) {
        return;
   }
}

Upvotes: 8

DevZer0
DevZer0

Reputation: 13535

You can always conduct your code in a try block and catch NullPointerException

String foo(Object p1, Object p2, Object p3){
  String bar;
  try {
       bar = p1.toString() + p2.toString();
       return bar;
   } catch (NullPointerException e) {
       return null;
   }
}

Upvotes: 0

Mike Q
Mike Q

Reputation: 23219

Maybe restructure the method to pass a List<Object> or similar and then iterate the list to check the number of nulls.

Upvotes: 3

Related Questions