2747a337b0e5a0eb108b68517ea8cff6c413870e
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / formula / SpreadsheetEvaluationEnvironment.java
1 package org.simantics.spreadsheet.graph.formula;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.simantics.spreadsheet.graph.CellFormulaFunction;
7 import org.simantics.spreadsheet.graph.SpreadsheetBook;
8
9 public class SpreadsheetEvaluationEnvironment {
10
11     final private SpreadsheetBook book;
12     private Map<String, CellFormulaFunction<?>> functions = new HashMap<>();
13     public int iterationLimit = 100;
14
15     public SpreadsheetEvaluationEnvironment(SpreadsheetBook book) {
16         this.book = book;
17
18         functions.put("MATCH", new MatchFormulaFunction());
19         functions.put("ISERROR",new IsErrorFormulaFunction());
20         functions.put("IFERROR",new IfErrorFormulaFunction());
21         functions.put("IF", new IfFormulaFunction());
22         functions.put("OR", new OrFormulaFunction());
23         functions.put("AND", new AndFormulaFunction());
24         functions.put("ROUND", new RoundFormulaFunction());
25         functions.put("ROUNDUP", new RoundupFormulaFunction());
26         functions.put("SQRT", new SqrtFormulaFunction());
27         functions.put("PI", new PiFormulaFunction());
28         functions.put("COUNTIF", new CountifFormulaFunction());
29         functions.put("SUM", new SumFormulaFunction());
30         functions.put("AVERAGE", new AverageFormulaFunction());
31         functions.put("SUMIF", new SumifFormulaFunction());
32         functions.put("VLOOKUP", new VlookupFormulaFunction());
33         functions.put("HLOOKUP", new HlookupFormulaFunction());
34         functions.put("TODAY", new TodayFormulaFunction());
35         functions.put("LINEST", new LinestFormulaFunction());
36         functions.put("GEOMEAN", new GeomeanFormulaFunction());
37     }
38
39     public CellFormulaFunction<?> getFunction(String name) {
40         return functions.get(name);
41     }
42
43     public SpreadsheetBook getBook() {
44         return book;
45     }
46
47     public static Map<SpreadsheetBook, SpreadsheetEvaluationEnvironment> INSTANCES = new HashMap<>();
48
49     public static SpreadsheetEvaluationEnvironment getInstance(SpreadsheetBook book) {
50         SpreadsheetEvaluationEnvironment env = INSTANCES.get(book);
51         if (env == null) {
52             env = new SpreadsheetEvaluationEnvironment(book);
53             INSTANCES.put(book, env);
54         }
55         return env;
56     }
57
58 }