From acb16f7348707f13228389bb05a6d1c899bb88b4 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Thu, 5 Nov 2009 07:25:54 +0000 Subject: [PATCH] =?UTF-8?q?Applying=20J=C3=B6rg=20Gottschling's=20patch=20?= =?UTF-8?q?from=20LANG-358=20with=20Thomas=20Menzel's=20naming=20from=20LA?= =?UTF-8?q?NG-550=20and=20some=20of=20his=20tests.=20This=20adds=20a=20fir?= =?UTF-8?q?stNonNull=20method=20to=20ObjectUtils.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@833016 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/ObjectUtils.java | 32 +++++++++++++++++++ .../apache/commons/lang/ObjectUtilsTest.java | 15 +++++++++ 2 files changed, 47 insertions(+) diff --git a/src/java/org/apache/commons/lang/ObjectUtils.java b/src/java/org/apache/commons/lang/ObjectUtils.java index 44d3a9ac7..2bdcb1c30 100644 --- a/src/java/org/apache/commons/lang/ObjectUtils.java +++ b/src/java/org/apache/commons/lang/ObjectUtils.java @@ -318,4 +318,36 @@ public class ObjectUtils { } } + /** + *

Returns the first passed value which is not null or + * null otherwise.

+ * + *
+     * ObjectUtils.firstNonNull(null, null)      = null
+     * ObjectUtils.firstNonNull(null, "")        = ""
+     * ObjectUtils.firstNonNull(null, null, "")  = ""
+     * ObjectUtils.firstNonNull(null, "zz")      = "zz"
+     * ObjectUtils.firstNonNull("abc", *)        = "abc"
+     * ObjectUtils.firstNonNull(null, "xyz", *)  = "xyz"
+     * ObjectUtils.firstNonNull(Boolean.TRUE, *) = Boolean.TRUE
+     * ObjectUtils.firstNonNull()                = null
+     * 
+ * + * @param values the values to test, may be null or empty + * + * @return The first value from values which is not + * null or null otherwise. + */ + public static T firstNonNull(T... values) { + // This is a trivial implementation. There may be faster ones. + // According to the JRE Libraries, overloading the method with fixed + // parameter sizes may be faster. + for (T val : values) { + if (val != null && val != ObjectUtils.NULL) { + return val; + } + } + return null; + } + } diff --git a/src/test/org/apache/commons/lang/ObjectUtilsTest.java b/src/test/org/apache/commons/lang/ObjectUtilsTest.java index c92b819ce..20ec1450d 100644 --- a/src/test/org/apache/commons/lang/ObjectUtilsTest.java +++ b/src/test/org/apache/commons/lang/ObjectUtilsTest.java @@ -216,4 +216,19 @@ public class ObjectUtilsTest extends TestCase { assertNull( ObjectUtils.min((String)null, (String)null) ); } + + public void testFirstNonNull() { + assertEquals(null, ObjectUtils.firstNonNull(null, null)); + assertEquals("", ObjectUtils.firstNonNull(null, "")); + assertEquals("123", ObjectUtils.firstNonNull(null, null, "123", "456")); + assertEquals("123", ObjectUtils.firstNonNull("123", null, "456", null)); + assertEquals(null, ObjectUtils.firstNonNull()); + assertSame(Boolean.TRUE, ObjectUtils.firstNonNull(Boolean.TRUE)); + assertNull(ObjectUtils.firstNonNull()); + assertNull(ObjectUtils.firstNonNull(null, null)); + assertSame("123", ObjectUtils.firstNonNull(null, ObjectUtils.NULL, "123", "456")); + assertSame("456", ObjectUtils.firstNonNull(ObjectUtils.NULL, "456", "123", null)); + assertNull(ObjectUtils.firstNonNull(null, null, ObjectUtils.NULL)); + } + }