com.softicar.platform.common.core.utils.CompareUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.core.utils;
import com.softicar.platform.common.core.exceptions.SofticarDeveloperException;
import java.util.Comparator;
/**
* Utility methods for comparison.
*
* @author Oliver Richers
*/
public class CompareUtils {
public static int compareNullFirst(T left, T right, Comparator comparator) {
if (left == right) {
return 0;
} else if (left == null) {
return -1;
} else if (right == null) {
return 1;
} else {
return comparator.compare(left, right);
}
}
public static > int compareNullFirst(T left, T right) {
return compareNullFirst(left, right, (a, b) -> a.compareTo(b));
}
/**
* Forces the comparison of two objects.
*
* This method expects that the first object implements the
* {@link Comparable} interface and that the second object is an instance of
* the same class or derived class of the first object.
*
* @param a
* first object
* @param b
* second object
* @return the comparison result as defined by {@link Comparable}
* @throws SofticarDeveloperException
* if the first element does not implement {@link Comparable} or
* if the second object is not of the same or a derived class.
*/
public static int compareTo(Object a, Object b) {
if (a instanceof Comparable && a.getClass().isInstance(b)) {
Comparable