
com.github.apetrelli.gwtintegration.util.shared.ObjectUtils Maven / Gradle / Ivy
The newest version!
package com.github.apetrelli.gwtintegration.util.shared;
/**
* Copied from Apache Commons Lang 3.1.
*
* Operations on {@code Object}.
*
* This class tries to handle {@code null} input gracefully.
* An exception will generally not be thrown for a {@code null} input.
* Each method documents its behaviour in more detail.
*
* #ThreadSafe#
* @since 1.0
* @version $Id: ObjectUtils.java 1199894 2011-11-09 17:53:59Z ggregory $
*/
public class ObjectUtils {
private ObjectUtils() {}
/**
* Null safe comparison of Comparables.
* {@code null} is assumed to be less than a non-{@code null} value.
*
* @param type of the values processed by this method
* @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 type of the values processed by this method
* @param c1 the first comparable, may be null
* @param c2 the second comparable, may be null
* @param nullGreater if true {@code null} is considered greater
* than a non-{@code null} value or if false {@code null} is
* considered less than a Non-{@code null} value
* @return a negative value if c1 < c2, zero if c1 = c2
* and a positive value if c1 > c2
* @see java.util.Comparator#compare(Object, Object)
*/
public static > int compare(T c1, T c2, boolean nullGreater) {
if (c1 == c2) {
return 0;
} else if (c1 == null) {
return nullGreater ? 1 : -1;
} else if (c2 == null) {
return nullGreater ? -1 : 1;
}
return c1.compareTo(c2);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy