You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pa...@apache.org on 2016/05/26 18:16:38 UTC
[1/2] [lang] LANG-781: Added methods to ObjectUtils class to check
for null elements in the array (closes #108)
Repository: commons-lang
Updated Branches:
refs/heads/master 2433cd393 -> 5d2728f65
LANG-781: Added methods to ObjectUtils class to check for null elements in the array (closes #108)
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/d190655a
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/d190655a
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/d190655a
Branch: refs/heads/master
Commit: d190655a97c33c886997b9570697f432e7d3a21a
Parents: 2433cd3
Author: Krzysztof Wolny <k....@samrtrecruiters.com>
Authored: Tue Jul 28 14:54:19 2015 +0200
Committer: pascalschumacher <pa...@gmx.net>
Committed: Thu May 26 19:35:58 2016 +0200
----------------------------------------------------------------------
.../org/apache/commons/lang3/ObjectUtils.java | 39 ++++++++++++++++++++
.../apache/commons/lang3/ObjectUtilsTest.java | 32 ++++++++++++++++
2 files changed, 71 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/d190655a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index 5c5fc04..e840031 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 class ObjectUtils {
return null;
}
+ /**
+ * <p></p>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.</p>
+ *
+ * @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;
+ }
+
+ /**
+ * <p></p>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.</p>
+ *
+ * @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
//-----------------------------------------------------------------------
/**
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/d190655a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
index acf30e7..0832647 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 class ObjectUtilsTest {
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() {
[2/2] [lang] LANG-781: add javadoc examples and since tags;
add changes.xml entry
Posted by pa...@apache.org.
LANG-781: add javadoc examples and since tags; add changes.xml entry
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/5d2728f6
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/5d2728f6
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/5d2728f6
Branch: refs/heads/master
Commit: 5d2728f655abea3e64c9e7e247cff71ee0285a66
Parents: d190655
Author: pascalschumacher <pa...@gmx.net>
Authored: Thu May 26 20:16:27 2016 +0200
Committer: pascalschumacher <pa...@gmx.net>
Committed: Thu May 26 20:16:27 2016 +0200
----------------------------------------------------------------------
src/changes/changes.xml | 1 +
.../org/apache/commons/lang3/ObjectUtils.java | 25 ++++++++++++++++++--
2 files changed, 24 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/5d2728f6/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index fe3c588..4f87fcc 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
+ <action issue="LANG-781" type="add" dev="pschumacher" due-to="Krzysztof Wolny">Add methods to ObjectUtils class to check for null elements in the array</action>
<action issue="LANG-1228" type="add" dev="pschumacher" due-to="Brad Hess">Prefer Throwable.getCause() in ExceptionUtils.getCause()</action>
<action issue="LANG-1233" type="add" dev="pschumacher" due-to="Nick Manley">DiffBuilder add method to allow appending from a DiffResult</action>
<action issue="LANG-1176" type="update" dev="pschumacher" due-to="Jeffery Yuan">Improve ArrayUtils removeElements time complexity to O(n)</action>
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/5d2728f6/src/main/java/org/apache/commons/lang3/ObjectUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
index e840031..8f1478f 100644
--- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java
@@ -129,29 +129,50 @@ public class ObjectUtils {
}
/**
- * <p></p>Checks if any value in the array is not {@code null}.
+ * <p>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.</p>
*
+ * <pre>
+ * ObjectUtils.anyNotNull(*) = true
+ * ObjectUtils.anyNotNull(*, null) = true
+ * ObjectUtils.anyNotNull(null, *) = true
+ * ObjectUtils.anyNotNull(null, null, *, *) = true
+ * ObjectUtils.anyNotNull(null) = false
+ * ObjectUtils.anyNotNull(null, null) = false
+ * </pre>
+ *
* @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.
+ * @since 3.5
*/
public static boolean anyNotNull(final Object... values) {
return firstNonNull(values) != null;
}
/**
- * <p></p>Checks if all values in the array are not {@code null}s.
+ * <p>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.</p>
+ *
+ * <pre>
+ * ObjectUtils.allNotNull(*) = true
+ * ObjectUtils.allNotNull(*, *) = true
+ * ObjectUtils.allNotNull(null) = false
+ * ObjectUtils.allNotNull(null, null) = false
+ * ObjectUtils.allNotNull(null, *) = false
+ * ObjectUtils.allNotNull(*, null) = false
+ * ObjectUtils.allNotNull(*, *, null, *) = false
+ * </pre>
*
* @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.
+ * @since 3.5
*/
public static boolean allNotNull(final Object... values) {
if (values == null) {