| 28 | import java.util.List; |
| 29 | |
| 30 | public interface MathFunction { |
| 31 | |
| 32 | /** |
| 33 | * @return the name of function |
| 34 | */ |
| 35 | String name(); |
| 36 | |
| 37 | /** |
| 38 | * @return True if name mentions this function |
| 39 | */ |
| 40 | boolean compareNames(String name); |
| 41 | |
| 42 | /** |
| 43 | * Calculates and returns the value |
| 44 | * Parameters are usually double values |
| 45 | */ |
| 46 | double calculate(Object... parameters) throws MathParserException; |
| 47 | |
| 48 | int getParameterCount(); |
| 49 | |
| 50 | /** |
| 51 | * @return True if the parameter at specified index won't be a double value (String otherwise) |
| 52 | */ |
| 53 | boolean isSpecialParameter(int index); |
| 54 | |
| 55 | /** |
| 56 | * Calls when this function just attached to a parser or it's parent variable called to calculate |
| 57 | */ |
| 58 | void attachToParser(MathParser parser); |
| 59 | |
| 60 | static MathFunction wrap(final Method method) { |
| 61 | return wrap(method, method.getName()); |
| 62 | } |
| 63 | |
| 64 | static MathFunction wrap(final Method method, final String name) { |
| 65 | return new MathFunction() { |
| 66 | |
| 67 | MathParser parser = null; |
| 68 | |
| 69 | @Override |
| 70 | public String name() { |
| 71 | return name; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean compareNames(String name) { |
| 76 | return name().trim().equalsIgnoreCase(name.trim()); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public double calculate(Object... parameters) throws MathParserException { |
| 81 | try { |
| 82 | List<Object> pars = new ArrayList<>(); |
| 83 | if (method.getParameterTypes()[0] == MathParser.class) |
| 84 | pars.add(parser); |
| 85 | if (getParameterCount() == -1) { |
| 86 | pars.add(parameters); |
| 87 | } else { |
no outgoing calls
no test coverage detected