Search in sources :

Example 1 with CompareResult

use of org.apache.poi.ss.formula.functions.LookupUtils.CompareResult in project poi by apache.

the class Match method findIndexOfValue.

/**
	 * @return zero based index
	 */
private static int findIndexOfValue(ValueEval lookupValue, ValueVector lookupRange, boolean matchExact, boolean findLargestLessThanOrEqual) throws EvaluationException {
    LookupValueComparer lookupComparer = createLookupComparer(lookupValue, matchExact);
    int size = lookupRange.getSize();
    if (matchExact) {
        for (int i = 0; i < size; i++) {
            if (lookupComparer.compareTo(lookupRange.getItem(i)).isEqual()) {
                return i;
            }
        }
        throw new EvaluationException(ErrorEval.NA);
    }
    if (findLargestLessThanOrEqual) {
        // Note - backward iteration
        for (int i = size - 1; i >= 0; i--) {
            CompareResult cmp = lookupComparer.compareTo(lookupRange.getItem(i));
            if (cmp.isTypeMismatch()) {
                continue;
            }
            if (!cmp.isLessThan()) {
                return i;
            }
        }
        throw new EvaluationException(ErrorEval.NA);
    }
    // TODO - is binary search used for (match_type==+1) ?
    for (int i = 0; i < size; i++) {
        CompareResult cmp = lookupComparer.compareTo(lookupRange.getItem(i));
        if (cmp.isEqual()) {
            return i;
        }
        if (cmp.isGreaterThan()) {
            if (i < 1) {
                throw new EvaluationException(ErrorEval.NA);
            }
            return i - 1;
        }
    }
    return size - 1;
}
Also used : LookupValueComparer(org.apache.poi.ss.formula.functions.LookupUtils.LookupValueComparer) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) CompareResult(org.apache.poi.ss.formula.functions.LookupUtils.CompareResult)

Aggregations

EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)1 CompareResult (org.apache.poi.ss.formula.functions.LookupUtils.CompareResult)1 LookupValueComparer (org.apache.poi.ss.formula.functions.LookupUtils.LookupValueComparer)1