Applying max/min for Comparables as supplied by David Karlsen in LANG-291
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@475113 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
11a4975580
commit
012998b3d4
|
@ -31,6 +31,7 @@ import java.io.Serializable;
|
|||
* @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$
|
||||
*/
|
||||
|
@ -277,4 +278,51 @@ public class ObjectUtils {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@ package org.apache.commons.lang;
|
|||
|
||||
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 @@ public class ObjectUtilsTest extends TestCase {
|
|||
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 ) );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue