commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r1056124 - in /commons/proper/lang/trunk/src: main/java/org/apache/commons/lang3/ObjectUtils.java test/java/org/apache/commons/lang3/ObjectUtilsTest.java
Date Thu, 06 Jan 2011 23:49:09 GMT
Author: niallp
Date: Thu Jan  6 23:49:09 2011
New Revision: 1056124

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

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

Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java?rev=1056124&r1=1056123&r2=1056124&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ObjectUtils.java Thu
Jan  6 23:49:09 2011
@@ -288,11 +288,7 @@ public class ObjectUtils {
      *  </ul>
      */
     public static <T extends Comparable<? super T>> T min(T c1, T 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;
     }
 
     /**
@@ -309,11 +305,45 @@ public class ObjectUtils {
      *  </ul>
      */
     public static <T extends Comparable<? super T>> T max(T c1, T c2) {
-        if (c1 != null && c2 != null) {
-            return c1.compareTo(c2) >= 0 ? c1 : c2;
+        return compare(c1, c2, false) >= 0 ? c1 : c2;
+    }
+
+    /**
+     * Null safe comparison of Comparables.
+     * 
+     * @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
+     */
+    public static <T extends Comparable<? super T>> int compare(T c1, T 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.lang.Comparator#compare(Object, Object)
+     */
+    public static <T extends Comparable<? super T>> int compare(T c1, T c2, boolean
nullGreater) {
+        int result = 0;
+        if ((c1 == null) || (c2 == null)) {
+            if (nullGreater) {
+                result = (c1 == null ? 1 : 0) - (c2 == null ? 1 : 0);
+            } else {
+                result = (c1 == null ? -1 : 0) - (c2 == null ? -1 : 0);
+            }
         } else {
-            return c1 != null ? c1 : c2;
+            result = c1.compareTo(c2);
         }
+        return result;
     }
     
     /**

Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java?rev=1056124&r1=1056123&r2=1056124&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
(original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java
Thu Jan  6 23:49:09 2011
@@ -217,6 +217,27 @@ public class ObjectUtilsTest extends Tes
     }
 
     /**
+     * Tests {@link ObjectUtils#compare(Comparable, Comparable, boolean)}.
+     */
+    public void testCompare() {
+        Integer one = Integer.valueOf(1);
+        Integer two = Integer.valueOf(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