Created
July 18, 2012 16:49
-
-
Save bmchild/3137404 to your computer and use it in GitHub Desktop.
Checks if any number of objects are null
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* checks through a bunch of objects for a null value | |
* | |
* @param o1 | |
* @param objects | |
* @return true if any object is null, false otherwise | |
*/ | |
public static boolean hasNullValue(Object o1, Object... objects) { | |
if(o1 == null) { | |
return true; | |
} | |
boolean isNull = false; | |
for(Object o : objects) { | |
if(o == null) { | |
isNull = true; | |
break; | |
} | |
} | |
return isNull; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testHasNullValue() { | |
Object one = null; | |
assertTrue(Util.hasNullValue(one)); | |
one = new Object(); | |
assertFalse(Util.hasNullValue(one)); | |
Object two = null; | |
assertTrue(Util.hasNullValue(one, two)); | |
two = new Object(); | |
assertFalse(Util.hasNullValue(one, two)); | |
Object three = null; | |
assertTrue(Util.hasNullValue(one, two, three)); | |
three = new Object(); | |
assertFalse(Util.hasNullValue(one, two, three)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment