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

org.jpmml.evaluator.FunctionRegistry Maven / Gradle / Ivy

There is a newer version: 1.6.11
Show newest version
/*
 * Copyright (c) 2014 Villu Ruusmann
 *
 * This file is part of JPMML-Evaluator
 *
 * JPMML-Evaluator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JPMML-Evaluator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with JPMML-Evaluator.  If not, see .
 */
package org.jpmml.evaluator;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 

* A registry of Java user-defined functions (Java UDFs). *

*/ public class FunctionRegistry { private FunctionRegistry(){ } /** *

* Gets a function for a name. *

* *

* First, if the name is registered with a singleton function instance, returns that instance. * After that, if the name represents a {@link Function} class on the application classpath, loads it, and returns a newly created instance. *

*/ static public Function getFunction(String name){ if(name == null){ return null; } // End if if(FunctionRegistry.functions.containsKey(name)){ Function function = FunctionRegistry.functions.get(name); return function; } Class functionClazz; if(FunctionRegistry.functionClazzes.containsKey(name)){ functionClazz = FunctionRegistry.functionClazzes.get(name); } else { functionClazz = loadFunctionClass(name); FunctionRegistry.functionClazzes.put(name, functionClazz); } // End if if(functionClazz != null){ Function function; try { function = (Function)functionClazz.newInstance(); } catch(IllegalAccessException | InstantiationException | ExceptionInInitializerError e){ throw (EvaluationException)new EvaluationException() .initCause(e); } return function; } return null; } /** *

* Registers a function by its default name. *

*/ static public void putFunction(Function function){ putFunction(function.getName(), function); } /** *

* Registers a function by a name other than its default name. *

*/ static public void putFunction(String name, Function function){ FunctionRegistry.functions.put(Objects.requireNonNull(name), function); } /** *

* Registers a function class. *

*/ static public void putFunction(String name, Class functionClazz){ FunctionRegistry.functionClazzes.put(Objects.requireNonNull(name), checkClass(functionClazz)); } static public void removeFunction(String name){ FunctionRegistry.functions.remove(name); FunctionRegistry.functionClazzes.remove(name); } static private Class loadFunctionClass(String name){ Class clazz; try { ClassLoader classLoader = (Thread.currentThread()).getContextClassLoader(); if(classLoader == null){ classLoader = (FunctionRegistry.class).getClassLoader(); } clazz = classLoader.loadClass(name); } catch(ClassNotFoundException cnfe){ return null; } return checkClass(clazz); } static private Class checkClass(Class clazz){ if(!(Function.class).isAssignableFrom(clazz)){ throw new TypeCheckException(Function.class, clazz); } return clazz; } private static final Map functions = new LinkedHashMap<>(); private static final Map> functionClazzes = new LinkedHashMap<>(); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy