LANG-667 Add a Null-safe compare() method to ObjectUtils

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1056124 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Niall Pemberton 2011-01-06 23:49:09 +00:00
parent 4fb355a838
commit f4f07d5ace
2 changed files with 59 additions and 8 deletions

View File

@ -288,11 +288,7 @@ public class ObjectUtils {
* </ul>
*/
public static <T extends Comparable<? super T>> 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 {
* </ul>
*/
public static <T extends Comparable<? super T>> T max(T c1, T c2) {
if (c1 != null && c2 != null) {
return c1.compareTo(c2) >= 0 ? c1 : c2;
} else {
return c1 != null ? 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 <T extends Comparable<? super T>> 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 <code>null</code> is considered greater
* than a Non-<code>null</code> value or if false <code>null</code> is
* considered less than a Non-<code>null</code> 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 <T extends Comparable<? super T>> 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 {
result = c1.compareTo(c2);
}
return result;
}
/**

View File

@ -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.
*/