Skip to content

Commit

Permalink
NumberComparator: Extract helper methods to simplify if conditions.
Browse files Browse the repository at this point in the history
  • Loading branch information
kingjon3377 committed Mar 30, 2024
1 parent 07f30f3 commit ae1b75b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lovelace-util/src/main/java/lovelace/util/NumberComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ public class NumberComparator implements Comparator<Number>, Serializable {
@Serial
private static final long serialVersionUID = 0L;

private static boolean isIntegral(final Number number) {
return number instanceof Integer || number instanceof Long;
}

private static boolean isFloatingPoint(final Number number) {
return number instanceof Float || number instanceof Double;
}
/**
* Compare two numbers. If they are the same type, delegate to their
* built-in comparison function; if not, convert both to doubles and
* return the result of comparing those.
*/
@SuppressWarnings({"ChainOfInstanceofChecks", "QuestionableName"})
@SuppressWarnings({"QuestionableName"})
public static int compareNumbers(final Number one, final Number two) {
if ((one instanceof Integer || one instanceof Long) &&
(two instanceof Integer || two instanceof Long)) {
if (isIntegral(one) && isIntegral(two)) {
return Long.compare(one.longValue(), two.longValue());
} else if ((one instanceof Float || one instanceof Double) &&
(two instanceof Float || two instanceof Double)) {
} else if (isFloatingPoint(one) && isFloatingPoint(two)) {
return Double.compare(one.doubleValue(), two.doubleValue());
} else if (one instanceof BigDecimal && two instanceof BigDecimal) {
return ((BigDecimal) one).compareTo((BigDecimal) two);
Expand Down

0 comments on commit ae1b75b

Please sign in to comment.