Add allNull() and anyNull() methods to ObjectUtils, as well as their associated tests. (#522)

This commit is contained in:
Gary Gregory 2020-07-10 11:56:01 -04:00 committed by GitHub
commit 1a77a547dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 0 deletions

View File

@ -107,6 +107,32 @@ private Object readResolve() {
*/
public static final Null NULL = new Null();
/**
* Checks if all values in the given array are {@code null}.
*
* <p>
* If all the values are {@code null} or the array is {@code null}
* or empty, then {@code true} is returned, otherwise {@code false} is returned.
* </p>
*
* <pre>
* ObjectUtils.allNull(*) = false
* ObjectUtils.allNull(*, null) = false
* ObjectUtils.allNull(null, *) = false
* ObjectUtils.allNull(null, null, *, *) = false
* ObjectUtils.allNull(null) = true
* ObjectUtils.allNull(null, null) = true
* </pre>
*
* @param values the values to test, may be {@code null} or empty
* @return {@code true} if all values in the array are {@code null}s,
* {@code false} if there is at least one non-null value in the array.
* @since 3.11
*/
public static boolean allNull(final Object... values) {
return !anyNotNull(values);
}
/**
* Checks if all values in the array are not {@code nulls}.
*
@ -146,6 +172,34 @@ public static boolean allNotNull(final Object... values) {
return true;
}
/**
* Checks if any value in the given array is {@code null}.
*
* <p>
* If any of the values are {@code null} or the array is {@code null},
* then {@code true} is returned, otherwise {@code false} is returned.
* </p>
*
* <pre>
* ObjectUtils.anyNull(*) = false
* ObjectUtils.anyNull(*, *) = false
* ObjectUtils.anyNull(null) = true
* ObjectUtils.anyNull(null, null) = true
* ObjectUtils.anyNull(null, *) = true
* ObjectUtils.anyNull(*, null) = true
* ObjectUtils.anyNull(*, *, null, *) = true
* </pre>
*
* @param values the values to test, may be {@code null} or empty
* @return {@code true} if there is at least one {@code null} value in the array,
* {@code false} if all the values are non-null.
* If the array is {@code null} or empty, {@code true} is also returned.
* @since 3.11
*/
public static boolean anyNull(final Object... values) {
return !allNotNull(values);
}
/**
* Checks if any value in the given array is not {@code null}.
*

View File

@ -172,6 +172,22 @@ public void testGetFirstNonNull() {
assertEquals(Boolean.TRUE, ObjectUtils.getFirstNonNull(() -> null, () -> Boolean.TRUE));
}
/**
* Tests {@link ObjectUtils#anyNull(Object...)}.
*/
@Test
public void testAnyNull() {
assertTrue(ObjectUtils.anyNull((Object) null));
assertTrue(ObjectUtils.anyNull(null, null, null));
assertTrue(ObjectUtils.anyNull(null, FOO, BAR));
assertTrue(ObjectUtils.anyNull(FOO, BAR, null));
assertTrue(ObjectUtils.anyNull(FOO, BAR, null, FOO, BAR));
assertFalse(ObjectUtils.anyNull());
assertFalse(ObjectUtils.anyNull(FOO));
assertFalse(ObjectUtils.anyNull(FOO, BAR, 1, Boolean.TRUE, new Object(), new Object[]{}));
}
/**
* Tests {@link ObjectUtils#anyNotNull(Object...)}.
*/
@ -187,6 +203,21 @@ public void testAnyNotNull() {
assertTrue(ObjectUtils.anyNotNull(null, null, null, null, FOO, BAR));
}
/**
* Tests {@link ObjectUtils#allNull(Object...)}.
*/
@Test
public void testAllNull() {
assertTrue(ObjectUtils.allNull());
assertTrue(ObjectUtils.allNull((Object) null));
assertTrue(ObjectUtils.allNull((Object[]) null));
assertTrue(ObjectUtils.allNull(null, null, null));
assertFalse(ObjectUtils.allNull(FOO));
assertFalse(ObjectUtils.allNull(null, FOO, null));
assertFalse(ObjectUtils.allNull(null, null, null, null, FOO, BAR));
}
/**
* Tests {@link ObjectUtils#allNotNull(Object...)}.
*/