diff --git a/src/java/org/apache/commons/lang/ObjectUtils.java b/src/java/org/apache/commons/lang/ObjectUtils.java index 2bdcb1c30..34fff81f3 100644 --- a/src/java/org/apache/commons/lang/ObjectUtils.java +++ b/src/java/org/apache/commons/lang/ObjectUtils.java @@ -90,6 +90,37 @@ public static Object defaultIfNull(Object object, Object defaultValue) { return object != null ? object : defaultValue; } + /** + *

Returns the first value in the array which is not null. + * If all the values are null or the array is null + * or empty then null is returned.

+ * + *
+     * 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 if there are no non-null values + */ + public static T firstNonNull(T... values) { + for (T val : values) { + if (val != null && val != ObjectUtils.NULL) { + return val; + } + } + return null; + } + + // Null-safe equals/hashCode + //----------------------------------------------------------------------- /** *

Compares two objects for equality, where either one or both * objects may be null.

@@ -318,36 +349,4 @@ private Object readResolve() { } } - /** - *

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; - } - }