Author: niallp
Date: Fri Jan  7 00:13:19 2011
New Revision: 1056134

URL: http://svn.apache.org/viewvc?rev=1056134&view=rev
Log:
LANG-668 - change ObjectUtils min() & max() functions to use varargs rather 
than just two parameters

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=1056134&r1=1056133&r2=1056134&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
 Fri Jan  7 00:13:19 2011
@@ -277,35 +277,49 @@ public class ObjectUtils {
     /**
      * Null safe comparison of Comparables.
      * 
-     * @param c1  the first comparable, may be null
-     * @param c2  the second comparable, may be null
+     * @param values the set of comparable values, may be null
      * @return
      *  <ul>
-     *   <li>If both objects are non-null and unequal, the lesser object.
-     *   <li>If both objects are non-null and equal, c1.
-     *   <li>If one of the comparables is null, the non-null object.
-     *   <li>If both the comparables are null, null is returned.
+     *   <li>If any objects are non-null and unequal, the lesser object.
+     *   <li>If all objects are non-null and equal, the first.
+     *   <li>If any of the comparables are null, the lesser of the non-null 
object.
+     *   <li>If all the comparables are null, null is returned.
      *  </ul>
      */
-    public static <T extends Comparable<? super T>> T min(T c1, T c2) {
-        return compare(c1, c2, true) <= 0 ? c1 : c2;
+    public static <T extends Comparable<? super T>> T min(T... values) {
+        T result = null;
+        if (values != null) {
+            for (T value : values) {
+                if (compare(value, result, true) < 0) {
+                    result = value;
+                }
+            }
+        }
+        return result;
     }
 
     /**
      * Null safe comparison of Comparables.
      * 
-     * @param c1  the first comparable, may be null
-     * @param c2  the second comparable, may be null
+     * @param values the set of comparable values, may be null
      * @return
      *  <ul>
-     *   <li>If both objects are non-null and unequal, the greater object.
-     *   <li>If both objects are non-null and equal, c1.
-     *   <li>If one of the comparables is null, the non-null object.
-     *   <li>If both the comparables are null, null is returned.
+     *   <li>If any objects are non-null and unequal, the greater object.
+     *   <li>If all objects are non-null and equal, the first.
+     *   <li>If any of the comparables are null, the greater of the non-null 
object.
+     *   <li>If all the comparables are null, null is returned.
      *  </ul>
      */
-    public static <T extends Comparable<? super T>> T max(T c1, T c2) {
-        return compare(c1, c2, false) >= 0 ? c1 : c2;
+    public static <T extends Comparable<? super T>> T max(T... values) {
+        T result = null;
+        if (values != null) {
+            for (T value : values) {
+                if (compare(value, result, false) > 0) {
+                    result = value;
+                }
+            }
+        }
+        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=1056134&r1=1056133&r2=1056134&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
 Fri Jan  7 00:13:19 2011
@@ -180,18 +180,23 @@ public class ObjectUtilsTest extends Tes
         Calendar calendar = Calendar.getInstance();
         Date nonNullComparable1 = calendar.getTime();
         Date nonNullComparable2 = calendar.getTime();
+        String[] nullAray = null;
         
         calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) -1 );
         Date minComparable = calendar.getTime();
         
         assertNotSame( nonNullComparable1, nonNullComparable2 );
         
+        assertNull(ObjectUtils.max( (String) null ) );
+        assertNull(ObjectUtils.max( nullAray ) );
         assertSame( nonNullComparable1, ObjectUtils.max( null, 
nonNullComparable1 ) );
         assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, 
null ) );
+        assertSame( nonNullComparable1, ObjectUtils.max( null, 
nonNullComparable1, null ) );
         assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, 
nonNullComparable2 ) );
         assertSame( nonNullComparable2, ObjectUtils.max( nonNullComparable2, 
nonNullComparable1 ) );
         assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, 
minComparable ) );
         assertSame( nonNullComparable1, ObjectUtils.max( minComparable, 
nonNullComparable1 ) );
+        assertSame( nonNullComparable1, ObjectUtils.max( null, minComparable, 
null, nonNullComparable1 ) );
 
         assertNull( ObjectUtils.max((String)null, (String)null) );
     }
@@ -200,18 +205,23 @@ public class ObjectUtilsTest extends Tes
         Calendar calendar = Calendar.getInstance();
         Date nonNullComparable1 = calendar.getTime();
         Date nonNullComparable2 = calendar.getTime();
+        String[] nullAray = null;
         
         calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) -1 );
         Date minComparable = calendar.getTime();
         
         assertNotSame( nonNullComparable1, nonNullComparable2 );
         
+        assertNull(ObjectUtils.min( (String) null ) );
+        assertNull(ObjectUtils.min( nullAray ) );
         assertSame( nonNullComparable1, ObjectUtils.min( null, 
nonNullComparable1 ) );
         assertSame( nonNullComparable1, ObjectUtils.min( nonNullComparable1, 
null ) );
+        assertSame( nonNullComparable1, ObjectUtils.min( null, 
nonNullComparable1, null ) );
         assertSame( nonNullComparable1, ObjectUtils.min( nonNullComparable1, 
nonNullComparable2 ) );
         assertSame( nonNullComparable2, ObjectUtils.min( nonNullComparable2, 
nonNullComparable1 ) );
         assertSame( minComparable, ObjectUtils.min( nonNullComparable1, 
minComparable ) );
         assertSame( minComparable, ObjectUtils.min( minComparable, 
nonNullComparable1 ) );
+        assertSame( minComparable, ObjectUtils.min( null, nonNullComparable1, 
null, minComparable ) );
 
         assertNull( ObjectUtils.min((String)null, (String)null) );
     }


Reply via email to