All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencds.cqf.cql.data.SystemExternalFunctionProvider Maven / Gradle / Ivy

package org.opencds.cqf.cql.data;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class SystemExternalFunctionProvider implements ExternalFunctionProvider {

    private List staticFunctions;

    public SystemExternalFunctionProvider(List staticFunctions) {
        this.staticFunctions = staticFunctions;
    }

    // TODO: Support adding more functions to an existing provider object.

    @Override
    public Object evaluate(String staticFunctionName, List arguments)
    {
        for (Method staticFunction : staticFunctions) {
            if (staticFunction.getName().equals(staticFunctionName)) {
                try {
                    return staticFunction.invoke(staticFunction.getDeclaringClass(), arguments.toArray());
                }
                catch (InvocationTargetException | IllegalAccessException e) {
                    throw new IllegalArgumentException("Unable to invoke function ["+staticFunctionName+"]: " + e.getMessage());
                }
                catch (Exception e) {
                    throw new RuntimeException("Error when executing function ["+staticFunctionName+"]: \n" + e.toString());
                }
            }
        }
        throw new IllegalArgumentException("Unable to find function ["+staticFunctionName+"].");
    }
}