Search in sources :

Example 1 with NumberQueryResult

use of org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult in project nifi by apache.

the class MathEvaluator method evaluate.

@Override
public QueryResult<Number> evaluate(final Map<String, String> attributes) {
    final String methodNamedValue = methodName.evaluate(attributes).getValue();
    if (methodNamedValue == null) {
        return new NumberQueryResult(null);
    }
    final Number subjectValue;
    if (subject != null) {
        subjectValue = subject.evaluate(attributes).getValue();
        if (subjectValue == null) {
            return new NumberQueryResult(null);
        }
    } else {
        subjectValue = null;
    }
    final Number optionalArgValue;
    if (optionalArg != null) {
        optionalArgValue = optionalArg.evaluate(attributes).getValue();
        if (optionalArgValue == null) {
            return new NumberQueryResult(null);
        }
    } else {
        optionalArgValue = null;
    }
    try {
        Number executionValue = null;
        if (subjectValue == null) {
            Method method;
            try {
                method = Math.class.getMethod(methodNamedValue);
            } catch (NoSuchMethodException subjectlessNoMethodException) {
                throw new AttributeExpressionLanguageException("Cannot evaluate 'math' function because no subjectless method was found with the name:'" + methodNamedValue + "'", subjectlessNoMethodException);
            }
            if (method == null) {
                throw new AttributeExpressionLanguageException("Cannot evaluate 'math' function because no subjectless method was found with the name:'" + methodNamedValue + "'");
            }
            executionValue = (Number) method.invoke(null);
        } else if (optionalArg == null) {
            boolean subjectIsDecimal = subjectValue instanceof Double;
            Method method;
            try {
                method = Math.class.getMethod(methodNamedValue, subjectIsDecimal ? double.class : long.class);
            } catch (NoSuchMethodException noOptionalNoMethodException) {
                throw new AttributeExpressionLanguageException("Cannot evaluate 'math' function because no method was found matching the passed parameters:" + " name:'" + methodNamedValue + "', one argument of type: '" + (subjectIsDecimal ? "double" : "long") + "'", noOptionalNoMethodException);
            }
            if (method == null) {
                throw new AttributeExpressionLanguageException("Cannot evaluate 'math' function because no method was found matching the passed parameters:" + " name:'" + methodNamedValue + "', one argument of type: '" + (subjectIsDecimal ? "double" : "long") + "'");
            }
            if (subjectIsDecimal) {
                executionValue = (Number) method.invoke(null, subjectValue.doubleValue());
            } else {
                executionValue = (Number) method.invoke(null, subjectValue.longValue());
            }
        } else {
            boolean subjectIsDecimal = subjectValue instanceof Double;
            boolean optionalArgIsDecimal = optionalArgValue instanceof Double;
            Method method;
            boolean convertOptionalToInt = false;
            try {
                method = Math.class.getMethod(methodNamedValue, subjectIsDecimal ? double.class : long.class, optionalArgIsDecimal ? double.class : long.class);
            } catch (NoSuchMethodException withOptionalNoMethodException) {
                if (!optionalArgIsDecimal) {
                    try {
                        method = Math.class.getMethod(methodNamedValue, subjectIsDecimal ? double.class : long.class, int.class);
                    } catch (NoSuchMethodException withOptionalInnerNoMethodException) {
                        throw new AttributeExpressionLanguageException("Cannot evaluate 'math' function because no method was found matching the passed parameters: " + "name:'" + methodNamedValue + "', first argument type: '" + (subjectIsDecimal ? "double" : "long") + "', second argument type:  'long'", withOptionalInnerNoMethodException);
                    }
                    convertOptionalToInt = true;
                } else {
                    throw new AttributeExpressionLanguageException("Cannot evaluate 'math' function because no method was found matching the passed parameters: " + "name:'" + methodNamedValue + "', first argument type: '" + (subjectIsDecimal ? "double" : "long") + "', second argument type:  'double'", withOptionalNoMethodException);
                }
            }
            if (method == null) {
                throw new AttributeExpressionLanguageException("Cannot evaluate 'math' function because no method was found matching the passed parameters: " + "name:'" + methodNamedValue + "', first argument type: '" + (subjectIsDecimal ? "double" : "long") + "', second argument type:  '" + (optionalArgIsDecimal ? "double" : "long") + "'");
            }
            if (optionalArgIsDecimal) {
                executionValue = (Number) method.invoke(null, subjectValue, optionalArgValue.doubleValue());
            } else {
                if (convertOptionalToInt) {
                    executionValue = (Number) method.invoke(null, subjectValue, optionalArgValue.intValue());
                } else {
                    executionValue = (Number) method.invoke(null, subjectValue, optionalArgValue.longValue());
                }
            }
        }
        return new NumberQueryResult(executionValue);
    } catch (IllegalAccessException | InvocationTargetException e) {
        throw new AttributeExpressionLanguageException("Unable to calculate math function value", e);
    }
}
Also used : AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) NumberQueryResult(org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 NumberQueryResult (org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult)1 AttributeExpressionLanguageException (org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException)1