commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r833016 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/ObjectUtils.java test/org/apache/commons/lang/ObjectUtilsTest.java
Date Thu, 05 Nov 2009 07:25:54 GMT
Author: bayard
Date: Thu Nov  5 07:25:54 2009
New Revision: 833016

URL: http://svn.apache.org/viewvc?rev=833016&view=rev
Log:
Applying Jörg Gottschling's patch from LANG-358 with Thomas Menzel's naming from LANG-550
and some of his tests. This adds a firstNonNull method to ObjectUtils. 

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java?rev=833016&r1=833015&r2=833016&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java Thu Nov  5
07:25:54 2009
@@ -318,4 +318,36 @@
         }
     }
 
+    /**
+     * <p>Returns the first passed value which is not <code>null</code>
or
+     * <code>null</code> otherwise.</p>
+     * 
+     * <pre>
+     * 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
+     * </pre>
+     *
+     * @param values  the values to test, may be <code>null</code> or empty
+     * 
+     * @return The first value from <code>values</code> which is not
+     *         <code>null</code> or <code>null</code> otherwise.
+     */
+    public static <T> 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;
+    }
+
 }

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java?rev=833016&r1=833015&r2=833016&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java Thu Nov
 5 07:25:54 2009
@@ -216,4 +216,19 @@
 
         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));
+    }
+
 }



Mime
View raw message