commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r475113 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/ObjectUtils.java test/org/apache/commons/lang/ObjectUtilsTest.java
Date Wed, 15 Nov 2006 04:14:42 GMT
Author: bayard
Date: Tue Nov 14 20:14:42 2006
New Revision: 475113

URL: http://svn.apache.org/viewvc?view=rev&rev=475113
Log:
Applying max/min for Comparables as supplied by David Karlsen in LANG-291

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

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java?view=diff&rev=475113&r1=475112&r2=475113
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ObjectUtils.java Tue
Nov 14 20:14:42 2006
@@ -31,6 +31,7 @@
  * @author Stephen Colebourne
  * @author Gary Gregory
  * @author Mario Winterer
+ * @author <a href="mailto:david@davidkarlsen.com">David J. M. Karlsen</a>
  * @since 1.0
  * @version $Id$
  */
@@ -276,5 +277,52 @@
             return ObjectUtils.NULL;
         }
     }
+    
+    
+    /**
+     * Null safe comparison of Comparables.
+     * 
+     * @param c1
+     * @param c2
+     * @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.
+     *  </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;
+        }                              
+    }
+    
+    /**
+     * Null safe comparison of Comparables.
+     * 
+     * @param c1
+     * @param c2
+     * @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.
+     *  </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;
+        }                              
+    }
+    
+    
     
 }

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java?view=diff&rev=475113&r1=475112&r2=475113
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ObjectUtilsTest.java
Tue Nov 14 20:14:42 2006
@@ -18,6 +18,8 @@
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
+import java.util.Calendar;
+import java.util.Date;
 
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -175,5 +177,40 @@
         assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null);
         assertSame(ObjectUtils.NULL, SerializationUtils.clone(ObjectUtils.NULL));
     }
-
+    
+    
+    
+    public void testMax() {
+        Calendar calendar = Calendar.getInstance();
+        Comparable nonNullComparable1 = calendar.getTime();
+        Comparable nonNullComparable2 = calendar.getTime();
+        
+        calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) -1 );
+        Comparable minComparable = calendar.getTime();
+        
+        assertNotSame( nonNullComparable1, nonNullComparable2 );
+        
+        assertSame( nonNullComparable1, ObjectUtils.max( null, nonNullComparable1 ) );
+        assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, null ) );
+        assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, nonNullComparable2
) );
+        assertSame( nonNullComparable1, ObjectUtils.max( nonNullComparable1, minComparable
) );
+        assertSame( nonNullComparable1, ObjectUtils.max( minComparable, nonNullComparable1
) );
+    }
+    
+    public void testMin() {
+        Calendar calendar = Calendar.getInstance();
+        Comparable nonNullComparable1 = calendar.getTime();
+        Comparable nonNullComparable2 = calendar.getTime();
+        
+        calendar.set( Calendar.YEAR, calendar.get( Calendar.YEAR ) -1 );
+        Comparable minComparable = calendar.getTime();
+        
+        assertNotSame( nonNullComparable1, nonNullComparable2 );
+        
+        assertSame( nonNullComparable1, ObjectUtils.min( null, nonNullComparable1 ) );
+        assertSame( nonNullComparable1, ObjectUtils.min( nonNullComparable1, null ) );
+        assertSame( nonNullComparable1, ObjectUtils.min( nonNullComparable1, nonNullComparable2
) );
+        assertSame( minComparable, ObjectUtils.min( nonNullComparable1, minComparable ) );
+        assertSame( minComparable, ObjectUtils.min( minComparable, nonNullComparable1 ) );
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message