LANG-668 - change ObjectUtils min() & max() functions to use varargs rather than just two parameters

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1056134 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Niall Pemberton 2011-01-07 00:13:19 +00:00
parent 26afc6228d
commit 78e2ebfd9c
2 changed files with 40 additions and 16 deletions

View File

@ -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;
}
/**

View File

@ -180,18 +180,23 @@ public class ObjectUtilsTest extends TestCase {
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 TestCase {
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) );
}