[utilities COMMIT] in /java-support/trunk/src: main/java/net/shibboleth/utilities/java/support/primitive/ObjectSuppor...

noreply at shibboleth.net noreply at shibboleth.net
Fri May 22 16:46:07 EDT 2015


Author: putmanb
Date: Fri May 22 16:46:07 2015
New Revision: 796

URL: http://svn.shibboleth.net/view/utilities?rev=796&view=rev
Log:
Add object support method which returns the first non-null argument from a varargs list. 

Modified:
    java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/primitive/ObjectSupport.java
    java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/primitive/ObjectSupportTest.java

Modified: java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/primitive/ObjectSupport.java
URL: http://svn.shibboleth.net/view/utilities/java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/primitive/ObjectSupport.java?rev=796&r1=795&r2=796&view=diff
==============================================================================
--- java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/primitive/ObjectSupport.java	(original)
+++ java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/primitive/ObjectSupport.java	Fri May 22 16:46:07 2015
@@ -41,4 +41,34 @@
 
         return o.hashCode();
     }
+    
+    /**
+     * Return the first from a list of arguments that is non-null, or null if all arguments 
+     * are null.
+     * 
+     * <p>
+     * This is similar to Guava's {@link com.google.common.base.MoreObjects#firstNonNull(Object, Object)},
+     * except it takes more than 2 arguments, and also doesn't throw a null pointer exception if 
+     * all arguments are null.
+     * </p>
+     * 
+     * @param <T> the type of arguments being evaluated
+     * 
+     * @param objects the list of object references to evaluate
+     * 
+     * @return the first non-null argument, or null if all arguments are null
+     * 
+     */
+    @Nullable public static <T> T firstNonNull(@Nullable T ... objects) {
+        if (objects == null) {
+            return null;
+        } else {
+            for (T obj : objects) {
+                if (obj != null) {
+                    return obj;
+                }
+            }
+        }
+        return null;
+    }
 }

Modified: java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/primitive/ObjectSupportTest.java
URL: http://svn.shibboleth.net/view/utilities/java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/primitive/ObjectSupportTest.java?rev=796&r1=795&r2=796&view=diff
==============================================================================
--- java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/primitive/ObjectSupportTest.java	(original)
+++ java-support/trunk/src/test/java/net/shibboleth/utilities/java/support/primitive/ObjectSupportTest.java	Fri May 22 16:46:07 2015
@@ -26,8 +26,22 @@
  * Test for the remaining method in {@link ObjectSupport}
  */
 public class ObjectSupportTest {
+    
+    @Test public void testFirstNonNull() {
+       Object foo = new Object();
+       Object bar = new Object();
+       Object baz = new Object();
+       
+       Assert.assertSame(ObjectSupport.firstNonNull(foo, bar), foo);
+       Assert.assertSame(ObjectSupport.firstNonNull(foo, bar, baz), foo);
+       Assert.assertSame(ObjectSupport.firstNonNull(null, bar, baz), bar);
+       Assert.assertSame(ObjectSupport.firstNonNull(null, null, baz), baz);
+       
+       Assert.assertNull(ObjectSupport.firstNonNull(null, null, null, null));
+       Assert.assertNull(ObjectSupport.firstNonNull());
+    }
 
-    @Test public void testObjectSupport() {
+    @Test public void testHashCode() {
         ComplexClass c1 = new ComplexClass(new Integer(1), new Double(3.4), "String!");
         ComplexClass c2 = new ComplexClass("String3", new Integer(1243), new Boolean(false));
 



More information about the commits mailing list