private static Object compare()

in impl/maven-impl/src/main/java/org/apache/maven/impl/model/profile/ConditionParser.java [497:533]


    private static Object compare(Object left, String operator, Object right) {
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null) {
            if ("==".equals(operator)) {
                return false;
            } else if ("!=".equals(operator)) {
                return true;
            }
        }
        if (left instanceof Number leftNumber && right instanceof Number rightNumber) {
            double leftVal = leftNumber.doubleValue();
            double rightVal = rightNumber.doubleValue();
            return switch (operator) {
                case ">" -> leftVal > rightVal;
                case "<" -> leftVal < rightVal;
                case ">=" -> leftVal >= rightVal;
                case "<=" -> leftVal <= rightVal;
                case "==" -> Math.abs(leftVal - rightVal) < 1e-9;
                case "!=" -> Math.abs(leftVal - rightVal) >= 1e-9;
                default -> throw new IllegalStateException("Unknown operator: " + operator);
            };
        } else if (left instanceof String leftString && right instanceof String rightString) {
            int comparison = leftString.compareTo(rightString);
            return switch (operator) {
                case ">" -> comparison > 0;
                case "<" -> comparison < 0;
                case ">=" -> comparison >= 0;
                case "<=" -> comparison <= 0;
                case "==" -> comparison == 0;
                case "!=" -> comparison != 0;
                default -> throw new IllegalStateException("Unknown operator: " + operator);
            };
        }
        throw new RuntimeException("Cannot compare " + left + " and " + right + " with operator " + operator);
    }