a fork of EvalEx by ezylang with a handful of breaking changes
1---
2layout: default
3title: Function Dictionary
4parent: Customization
5nav_order: 3
6---
7
8## Function Dictionary
9
10By default, EvalEx has its own dictionary for the functions that can be used in an expression,
11the _MapBasedFunctionDictionary_. It stores and retrieves the functions in a case-insensitive
12_TreeMap_.
13
14You can easily define your own function directory, by defining a class that implements the
15_FunctionDictionaryIfc_:
16
17```java
18public interface FunctionDictionaryIfc {
19
20 /**
21 * Allows to add a function to the dictionary. Implementation is optional, if you have a fixed set
22 * of functions, this method can throw an exception.
23 *
24 * @param functionName The function name.
25 * @param function The function implementation.
26 */
27 void addFunction(String functionName, FunctionIfc function);
28
29 /**
30 * Check if the dictionary has a function with that name.
31 *
32 * @param functionName The function name to look for.
33 * @return <code>true</code> if a function was found or <code>false</code> if not.
34 */
35 default boolean hasFunction(String functionName) {
36 return getFunction(functionName) != null;
37 }
38
39 /**
40 * Get the function definition for a function name.
41 *
42 * @param functionName The name of the function.
43 * @return The function definition or <code>null</code> if no function was found.
44 */
45 FunctionIfc getFunction(String functionName);
46```
47
48Only two methods need to be implemented:
49
50- addFunction(String functionName, FunctionIfc function) - which allows to add a function.
51- FunctionIfc getFunction(String functionName) - retrieves a function implementation.
52
53The _addFunction()_ method is only called when a function is added to the configuration, using the
54_withAdditionalFunctions()_ in the _ExpressionConfiguration_ object.
55
56> NOTE: The function names that are passed to the methods are case-sensitive, just like they were
57> entered in the expression.
58
59The custom function dictionary can then be specified in the expression configuration:
60
61```java
62ExpressionConfiguration configuration = ExpressionConfiguration.builder()
63 .functionDictionary(new MyFunctionDirectory())
64 .build();
65```