forked from fanruan/finekit
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.9 KiB
93 lines
2.9 KiB
package com.fanruan.api.function; |
|
|
|
import com.fanruan.api.function.shell.FineFunc; |
|
import com.fr.design.formula.FunctionDefNAD; |
|
import com.fr.design.formula.NameAndDescription; |
|
import com.fr.file.FunctionManager; |
|
import com.fr.file.FunctionManagerProvider; |
|
import com.fr.script.Calculator; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.stable.fun.FunctionHelper; |
|
import com.fr.stable.script.FunctionDef; |
|
|
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
/** |
|
* @author zack |
|
* @date 2019/8/23 |
|
* @version 10.0 |
|
* 函数相关工具类 |
|
*/ |
|
public class FunctionKit { |
|
/** |
|
* 新增一个自定义函数 |
|
* |
|
* @param fineFunc 自定义函数对象 |
|
*/ |
|
public static void addFunc(FineFunc fineFunc) { |
|
if (fineFunc == null) { |
|
return; |
|
} |
|
if (Calculator.createCalculator().resolveMethod(fineFunc.getName()) == null) { |
|
FunctionManager.getInstance().addFunctionDef(new FunctionDef(fineFunc.getName(), fineFunc.getDescription(), fineFunc.getClassName())); |
|
} |
|
} |
|
|
|
/** |
|
* 根据自定义函数类路径移除一个自定义函数 |
|
* |
|
* @param className 自定义函数类路径 |
|
*/ |
|
public static void removeFunc(String className) { |
|
if (StringUtils.isEmpty(className)) { |
|
return; |
|
} |
|
int count = FunctionManager.getInstance().getFunctionDefCount(); |
|
List<FunctionDef> functionDefList = new ArrayList<FunctionDef>(count); |
|
for (int i = 0; i < count; i++) { |
|
FunctionDef functionDef = FunctionManager.getInstance().getFunctionDef(i); |
|
if (!className.equalsIgnoreCase(functionDef.getClassName())) { |
|
functionDefList.add(functionDef); |
|
} |
|
} |
|
if (functionDefList.size() == count) { |
|
return; |
|
} |
|
FunctionManager.getInstance().clearAllFunctionDef(); |
|
for (FunctionDef fun : functionDefList) { |
|
FunctionManager.getInstance().addFunctionDef(fun); |
|
} |
|
} |
|
|
|
/** |
|
* 得到描述函数管理器中所有的函数的名称描述 |
|
* @return 函数的名称描述数组 |
|
*/ |
|
public static NameAndDescription[] getDescription() { |
|
FunctionManagerProvider funtionManager = FunctionKit.getInstance(); |
|
if (funtionManager != null) { |
|
int functionDefCount = funtionManager.getFunctionDefCount(); |
|
FunctionDefNAD[] nads = new FunctionDefNAD[functionDefCount]; |
|
for (int i = 0; i < functionDefCount; i++) { |
|
nads[i] = new FunctionDefNAD(funtionManager.getFunctionDef(i)); |
|
} |
|
|
|
return nads; |
|
} |
|
|
|
return new NameAndDescription[0]; |
|
} |
|
/** |
|
* 得到函数管理器实例 |
|
* @return 函数管理器 |
|
*/ |
|
public static com.fr.file.FunctionManager getInstance() { |
|
return com.fr.file.FunctionManager.getInstance(); |
|
} |
|
|
|
public static int generateFunctionID(String var0) { |
|
return FunctionHelper.generateFunctionID(var0); |
|
} |
|
|
|
} |