diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index dc5060ed4..d210cc3ab 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -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 * */ - public static > T min(T c1, T c2) { - return compare(c1, c2, true) <= 0 ? c1 : c2; + public static > 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 * */ - public static > T max(T c1, T c2) { - return compare(c1, c2, false) >= 0 ? c1 : c2; + public static > T max(T... values) { + T result = null; + if (values != null) { + for (T value : values) { + if (compare(value, result, false) > 0) { + result = value; + } + } + } + return result; } /** diff --git a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java index 8b5e5640e..01ba19721 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -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) ); }