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

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

Go to download

The engine library for the Clinical Quality Language Java reference implementation

There is a newer version: 3.18.0
Show newest version
package org.opencds.cqf.cql.engine.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 + "].");
    }
}