commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r1057022 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/ObjectUtils.java test/java/org/apache/commons/lang/ObjectUtilsTest.java
Date Sun, 09 Jan 2011 20:38:34 GMT
Author: niallp
Date: Sun Jan  9 20:38:33 2011
New Revision: 1057022

URL: http://svn.apache.org/viewvc?rev=1057022&view=rev
Log:
Port LANG-667 to LANG 2.x Branch - Add a Null-safe compare() method to ObjectUtils

Modified:
    commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/ObjectUtils.java
    commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/ObjectUtilsTest.java

Modified: commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/ObjectUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/ObjectUtils.java?rev=1057022&r1=1057021&r2=1057022&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/ObjectUtils.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/ObjectUtils.java
Sun Jan  9 20:38:33 2011
@@ -287,11 +287,7 @@ public class ObjectUtils {
      *  </ul>
      */
     public static Object min(Comparable c1, Comparable c2) {
-        if (c1 != null && c2 != null) {
-            return c1.compareTo(c2) < 1 ? c1 : c2;
-        } else {
-            return c1 != null ? c1 : c2;
-        }                              
+        return (compare(c1, c2, true) <= 0 ? c1 : c2);
     }
 
     /**
@@ -308,11 +304,45 @@ public class ObjectUtils {
      *  </ul>
      */
     public static Object max(Comparable c1, Comparable c2) {
-        if (c1 != null && c2 != null) {
-            return c1.compareTo(c2) >= 0 ? c1 : c2;
-        } else {
-            return c1 != null ? c1 : c2;
+        return (compare(c1, c2, false) >= 0 ? c1 : c2);
+    }
+
+    /**
+     * Null safe comparison of Comparables.
+     * {@code null} is assumed to be less than a non-{@code null} value.
+     * 
+     * @param c1  the first comparable, may be null
+     * @param c2  the second comparable, may be null
+     * @return a negative value if c1 < c2, zero if c1 = c2
+     * and a positive value if c1 > c2
+     * @since 2.6
+     */
+    public static int compare(Comparable c1, Comparable c2) {
+        return compare(c1, c2, false);
+    }
+
+    /**
+     * Null safe comparison of Comparables.
+     * 
+     * @param c1  the first comparable, may be null
+     * @param c2  the second comparable, may be null
+     * @param nullGreater if true <code>null</code> is considered greater
+     * than a Non-<code>null</code> value or if false <code>null</code>
is
+     * considered less than a Non-<code>null</code> value
+     * @return a negative value if c1 < c2, zero if c1 = c2
+     * and a positive value if c1 > c2
+     * @see java.util.Comparator#compare(Object, Object)
+     * @since 2.6
+     */
+    public static int compare(Comparable c1, Comparable c2, boolean nullGreater) {
+        if (c1 == c2) {
+            return 0;
+        } else if (c1 == null) {
+            return (nullGreater ? 1 : -1);
+        } else if (c2 == null) {
+            return (nullGreater ? -1 : 1);
         }
+        return c1.compareTo(c2);
     }
     
     /**

Modified: commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/ObjectUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/ObjectUtilsTest.java?rev=1057022&r1=1057021&r2=1057022&view=diff
==============================================================================
--- commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/ObjectUtilsTest.java
(original)
+++ commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/ObjectUtilsTest.java
Sun Jan  9 20:38:33 2011
@@ -210,6 +210,27 @@ public class ObjectUtilsTest extends Tes
     }
 
     /**
+     * Tests {@link ObjectUtils#compare(Comparable, Comparable, boolean)}.
+     */
+    public void testCompare() {
+        Integer one = new Integer(1);
+        Integer two = new Integer(2);
+        Integer nullValue = null;
+
+        assertEquals("Null Null false", 0, ObjectUtils.compare(nullValue, nullValue));
+        assertEquals("Null Null true",  0, ObjectUtils.compare(nullValue, nullValue, true));
+
+        assertEquals("Null one false", -1, ObjectUtils.compare(nullValue, one));
+        assertEquals("Null one true",   1, ObjectUtils.compare(nullValue, one, true));
+        
+        assertEquals("one Null false", 1, ObjectUtils.compare(one, nullValue));
+        assertEquals("one Null true", -1, ObjectUtils.compare(one, nullValue, true));
+
+        assertEquals("one two false", -1, ObjectUtils.compare(one, two));
+        assertEquals("one two true",  -1, ObjectUtils.compare(one, two, true));
+    }
+
+    /**
      * Tests {@link ObjectUtils#clone(Object)} with a cloneable object.
      */
     public void testCloneOfCloneable() {



Mime
View raw message