| 31 | import java.util.regex.Matcher; |
| 32 | |
| 33 | public class Functions { |
| 34 | |
| 35 | /** |
| 36 | * All static methods in {@link Math} and {@link Functions} |
| 37 | * that returns double and matches arguments will wrap a {@link MathFunction} |
| 38 | * by {@link MathFunction#wrap(Method)} and import here so |
| 39 | * the MathParser can recognize the functions as a built-in function. |
| 40 | */ |
| 41 | private static final List<MathFunction> functions = new ArrayList<>(); |
| 42 | |
| 43 | static { |
| 44 | functions.add(new LogFunction()); |
| 45 | functions.add(new RadicalFunction()); |
| 46 | try { |
| 47 | functions.add(MathFunction.wrap(Functions.class.getMethod("integral", MathParser.class, |
| 48 | String.class, String.class, double.class, double.class), "∫")); |
| 49 | functions.add(MathFunction.wrap(Functions.class.getMethod("integral", MathParser.class, |
| 50 | String.class, String.class, double.class, double.class, double.class), "∫")); |
| 51 | functions.add(MathFunction.wrap(Functions.class.getMethod("radical", |
| 52 | double.class), "√")); |
| 53 | functions.add(MathFunction.wrap(Functions.class.getMethod("sigma", MathParser.class, String.class, |
| 54 | String.class, double.class, double.class), "Σ")); |
| 55 | functions.add(MathFunction.wrap(Functions.class.getMethod("sigma", MathParser.class, String.class, |
| 56 | String.class, double.class, double.class, double.class), "Σ")); |
| 57 | } catch (NoSuchMethodException e) { |
| 58 | e.printStackTrace(); |
| 59 | } |
| 60 | |
| 61 | ArrayList<Method> methods = new ArrayList<>(); |
| 62 | methods.addAll(Arrays.asList(Functions.class.getMethods())); |
| 63 | methods.addAll(Arrays.asList(Math.class.getMethods())); |
| 64 | addFunctions(methods, functions); |
| 65 | } |
| 66 | |
| 67 | public static List<MathFunction> getFunctions() { |
| 68 | return functions; |
| 69 | } |
| 70 | |
| 71 | static void addFunctions(Collection<Method> methods, List<MathFunction> functions) { |
| 72 | Math: |
| 73 | for (Method method : methods) { |
| 74 | if (method.getReturnType() == double.class) { |
| 75 | if (method.getParameterCount() != 1 || |
| 76 | (method.getParameterTypes()[0] != Object[].class && method.getParameterTypes()[0] != Double[].class)) { |
| 77 | int index = 0; |
| 78 | for (Class<?> cls : method.getParameterTypes()) { |
| 79 | if (cls != double.class && cls != String.class && !(index == 0 && cls == MathParser.class)) |
| 80 | continue Math; |
| 81 | index++; |
| 82 | } |
| 83 | } |
| 84 | functions.add(MathFunction.wrap(method)); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static MathFunction getFunction(String src, int index, String name, int count, List<MathFunction> innerFunctions) throws MathFunctionInvalidArgumentsException { |
| 90 | MathFunction function = null; |
nothing calls this directly
no test coverage detected