Example usage for org.apache.poi.ss.util NumberComparer compare

List of usage examples for org.apache.poi.ss.util NumberComparer compare

Introduction

In this page you can find the example usage for org.apache.poi.ss.util NumberComparer compare.

Prototype

public static int compare(double a, double b) 

Source Link

Document

This class attempts to reproduce Excel's behaviour for comparing numbers.

Usage

From source file:nl.eur.ese.spreadsheettest.DStarRunner.java

License:Apache License

/**
 * Test whether a value matches a numeric condition.
 * @param valueEval Value to check./*from w  w  w  .  ja  va 2 s. c  om*/
 * @param op Comparator to use.
 * @param condition Value to check against.
 * @return whether the condition holds.
 * @throws EvaluationException If it's impossible to turn the condition into a number.
 */
private static boolean testNumericCondition(ValueEval valueEval, operator op, String condition)
        throws EvaluationException {
    // Construct double from ValueEval.
    if (!(valueEval instanceof NumericValueEval))
        return false;
    double value = ((NumericValueEval) valueEval).getNumberValue();

    // Construct double from condition.
    double conditionValue = 0.0;
    try {
        int intValue = Integer.parseInt(condition);
        conditionValue = intValue;
    } catch (NumberFormatException e) { // It's not an int.
        try {
            conditionValue = Double.parseDouble(condition);
        } catch (NumberFormatException e2) { // It's not a double.
            throw new EvaluationException(ErrorEval.VALUE_INVALID);
        }
    }

    int result = NumberComparer.compare(value, conditionValue);
    switch (op) {
    case largerThan:
        return result > 0;
    case largerEqualThan:
        return result >= 0;
    case smallerThan:
        return result < 0;
    case smallerEqualThan:
        return result <= 0;
    case equal:
        return result == 0;
    }
    return false; // Can not be reached.
}