diff --git a/src/main/java/org/apache/commons/lang3/ObjectUtils.java b/src/main/java/org/apache/commons/lang3/ObjectUtils.java index 89d56c214..b31332753 100644 --- a/src/main/java/org/apache/commons/lang3/ObjectUtils.java +++ b/src/main/java/org/apache/commons/lang3/ObjectUtils.java @@ -288,11 +288,7 @@ public class ObjectUtils { * */ public static > T min(T c1, T c2) { - if (c1 != null && c2 != null) { - return c1.compareTo(c2) < 1 ? c1 : c2; - } else { - return c1 != null ? c1 : c2; - } + return compare(c1, c2, true) <= 0 ? c1 : c2; } /** @@ -309,11 +305,45 @@ public class ObjectUtils { * */ public static > T max(T c1, T c2) { - if (c1 != null && c2 != null) { - return c1.compareTo(c2) >= 0 ? c1 : c2; + return compare(c1, c2, false) >= 0 ? c1 : c2; + } + + /** + * Null safe comparison of Comparables. + * + * @param c1 the first comparable, may be null + * @param c2 the second comparable, may be null + * @return a negative value if c1 < c2, zero if c1 = c2 + * and a positive value if c1 > c2 + */ + public static > int compare(T c1, T c2) { + return compare(c1, c2, false); + } + + /** + * Null safe comparison of Comparables. + * + * @param c1 the first comparable, may be null + * @param c2 the second comparable, may be null + * @param nullGreater if true null is considered greater + * than a Non-null value or if false null is + * considered less than a Non-null value + * @return a negative value if c1 < c2, zero if c1 = c2 + * and a positive value if c1 > c2 + * @see java.lang.Comparator#compare(Object, Object) + */ + public static > int compare(T c1, T c2, boolean nullGreater) { + int result = 0; + if ((c1 == null) || (c2 == null)) { + if (nullGreater) { + result = (c1 == null ? 1 : 0) - (c2 == null ? 1 : 0); + } else { + result = (c1 == null ? -1 : 0) - (c2 == null ? -1 : 0); + } } else { - return c1 != null ? c1 : c2; + result = c1.compareTo(c2); } + 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 97f00daed..8b5e5640e 100644 --- a/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ObjectUtilsTest.java @@ -216,6 +216,27 @@ public class ObjectUtilsTest extends TestCase { assertNull( ObjectUtils.min((String)null, (String)null) ); } + /** + * Tests {@link ObjectUtils#compare(Comparable, Comparable, boolean)}. + */ + public void testCompare() { + Integer one = Integer.valueOf(1); + Integer two = Integer.valueOf(2); + Integer nullValue = null; + + assertEquals("Null Null false", 0, ObjectUtils.compare(nullValue, nullValue)); + assertEquals("Null Null true", 0, ObjectUtils.compare(nullValue, nullValue, true)); + + assertEquals("Null one false", -1, ObjectUtils.compare(nullValue, one)); + assertEquals("Null one true", 1, ObjectUtils.compare(nullValue, one, true)); + + assertEquals("one Null false", 1, ObjectUtils.compare(one, nullValue)); + assertEquals("one Null true", -1, ObjectUtils.compare(one, nullValue, true)); + + assertEquals("one two false", -1, ObjectUtils.compare(one, two)); + assertEquals("one two true", -1, ObjectUtils.compare(one, two, true)); + } + /** * Tests {@link ObjectUtils#clone(Object)} with a cloneable object. */