a fork of EvalEx by ezylang with a handful of breaking changes
at main 104 lines 3.6 kB view raw view rendered
1--- 2layout: default 3title: Operator Dictionary 4parent: Customization 5nav_order: 4 6--- 7 8## Function Dictionary 9 10By default, EvalEx has its own dictionary for the operators that can be used in an expression, 11the _MapBasedOperatorDictionary_. It stores and retrieves the operators in a case-insensitive 12_TreeMap_. 13 14You can easily define your own operator directory, by defining a class that implements the 15_FunctionDictionaryIfc_: 16 17```java 18public interface OperatorDictionaryIfc { 19 20 /** 21 * Allows to add an operator to the dictionary. Implementation is optional, if you have a fixed 22 * set of operators, this method can throw an exception. 23 * 24 * @param operatorString The operator name. 25 * @param operator The operator implementation. 26 */ 27 void addOperator(String operatorString, OperatorIfc operator); 28 29 /** 30 * Check if the dictionary has a prefix operator with that name. 31 * 32 * @param operatorString The operator name to look for. 33 * @return <code>true</code> if an operator was found or <code>false</code> if not. 34 */ 35 default boolean hasPrefixOperator(String operatorString) { 36 return getPrefixOperator(operatorString) != null; 37 } 38 39 /** 40 * Check if the dictionary has a postfix operator with that name. 41 * 42 * @param operatorString The operator name to look for. 43 * @return <code>true</code> if an operator was found or <code>false</code> if not. 44 */ 45 default boolean hasPostfixOperator(String operatorString) { 46 return getPostfixOperator(operatorString) != null; 47 } 48 49 /** 50 * Check if the dictionary has an infix operator with that name. 51 * 52 * @param operatorString The operator name to look for. 53 * @return <code>true</code> if an operator was found or <code>false</code> if not. 54 */ 55 default boolean hasInfixOperator(String operatorString) { 56 return getInfixOperator(operatorString) != null; 57 } 58 59 /** 60 * Get the operator definition for a prefix operator name. 61 * 62 * @param operatorString The name of the operator. 63 * @return The operator definition or <code>null</code> if no operator was found. 64 */ 65 OperatorIfc getPrefixOperator(String operatorString); 66 67 /** 68 * Get the operator definition for a postfix operator name. 69 * 70 * @param operatorString The name of the operator. 71 * @return The operator definition or <code>null</code> if no operator was found. 72 */ 73 OperatorIfc getPostfixOperator(String operatorString); 74 75 /** 76 * Get the operator definition for an infix operator name. 77 * 78 * @param operatorString The name of the operator. 79 * @return The operator definition or <code>null</code> if no operator was found. 80 */ 81 OperatorIfc getInfixOperator(String operatorString); 82} 83``` 84 85Only four methods need to be implemented: 86 87- addOperator(String operatorString, OperatorIfc operator) - which allows to add an operator. 88- OperatorIfc getPrefixOperator(String operatorString) - retrieves a prefix operator. 89- OperatorIfc getPostfixOperator(String operatorString) - retrieves a postfix operator. 90- OperatorIfc getInfixOperator(String operatorString) - retrieves a infix operator. 91 92The _addOperator()_ method is only called when an operator is added to the configuration, using the 93_withAdditionalOperators()_ in the _ExpressionConfiguration_ object. 94 95> NOTE: The operator names that are passed to the methods are case-sensitive, just like they were 96> entered in the expression. 97 98The custom operator dictionary can then be specified in the expression configuration: 99 100```java 101ExpressionConfiguration configuration = ExpressionConfiguration.builder() 102 .operatorDictionary(new MyOperatorDictionary()) 103 .build(); 104```