From d190655a97c33c886997b9570697f432e7d3a21a Mon Sep 17 00:00:00 2001
From: Krzysztof Wolny
Date: Tue, 28 Jul 2015 14:54:19 +0200
Subject: [PATCH] LANG-781: Added methods to ObjectUtils class to check for
null elements in the array (closes #108)
---
.../org/apache/commons/lang3/ObjectUtils.java | 39 +++++++++++++++++++
.../apache/commons/lang3/ObjectUtilsTest.java | 32 +++++++++++++++
2 files changed, 71 insertions(+)
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index 5c5fc04fb..e8400319f 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -128,6 +128,45 @@ public static T firstNonNull(final T... values) {
return null;
}
+ /**
+ * Checks if any value in the array is not {@code null}.
+ * If all the values are {@code null} or the array is {@code null}
+ * or empty then {@code false} is returned. Otherwise {@code true} is returned.
+ *
+ * @param values the values to test, may be {@code null} or empty
+ * @return {@code true} if there is at least one non-null value in the array,
+ * {@code false} if all values in the array are {@code null}s.
+ * If the array is {@code null} or empty {@code false} is also returned.
+ */
+ public static boolean anyNotNull(final Object... values) {
+ return firstNonNull(values) != null;
+ }
+
+ /**
+ * Checks if all values in the array are not {@code null}s.
+ * If any value is {@code null} or the array is {@code null}
+ * then {@code false} is returned.
+ * If all elements in array are not {@code null} or the array is empty (contains no elements)
+ * {@code true} is returned.
+ *
+ * @param values the values to test, may be {@code null} or empty
+ * @return {@code false} if there is at least one {@code null} value in the array or the array is {@code null},
+ * {@code true} if all values in the array are not {@code null}s or array contains no elements.
+ */
+ public static boolean allNotNull(final Object... values) {
+ if (values == null) {
+ return false;
+ }
+
+ for (final Object val : values) {
+ if (val == null) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
// Null-safe equals/hashCode
//-----------------------------------------------------------------------
/**
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index acf30e72e..083264780 100644
--- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
@@ -89,6 +89,38 @@ public void testFirstNonNull() {
assertNull(ObjectUtils.firstNonNull((Object[]) null));
}
+ /**
+ * Tests {@link ObjectUtils#anyNotNull(Object...)}.
+ */
+ @Test
+ public void testAnyNotNull() {
+ assertFalse(ObjectUtils.anyNotNull());
+ assertFalse(ObjectUtils.anyNotNull((Object) null));
+ assertFalse(ObjectUtils.anyNotNull((Object[]) null));
+ assertFalse(ObjectUtils.anyNotNull(null, null, null));
+
+ assertTrue(ObjectUtils.anyNotNull(FOO));
+ assertTrue(ObjectUtils.anyNotNull(null, FOO, null));
+ assertTrue(ObjectUtils.anyNotNull(null, null, null, null, FOO, BAR));
+ }
+
+ /**
+ * Tests {@link ObjectUtils#allNotNull(Object...)}.
+ */
+ @Test
+ public void testAllNotNull() {
+ assertFalse(ObjectUtils.allNotNull((Object) null));
+ assertFalse(ObjectUtils.allNotNull((Object[]) null));
+ assertFalse(ObjectUtils.allNotNull(null, null, null));
+ assertFalse(ObjectUtils.allNotNull(null, FOO, BAR));
+ assertFalse(ObjectUtils.allNotNull(FOO, BAR, null));
+ assertFalse(ObjectUtils.allNotNull(FOO, BAR, null, FOO, BAR));
+
+ assertTrue(ObjectUtils.allNotNull());
+ assertTrue(ObjectUtils.allNotNull(FOO));
+ assertTrue(ObjectUtils.allNotNull(FOO, BAR, 1, Boolean.TRUE, new Object(), new Object[]{}));
+ }
+
//-----------------------------------------------------------------------
@Test
public void testEquals() {