]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/SumifFormulaFunction.java
SpreadsheetCells with Circular References support iterations.
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / formula / SumifFormulaFunction.java
1 package org.simantics.spreadsheet.graph.formula;\r
2 \r
3 import org.simantics.spreadsheet.graph.CellFormulaFunction;\r
4 import org.simantics.spreadsheet.graph.CellValueVisitor;\r
5 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;\r
6 import org.simantics.spreadsheet.graph.SpreadsheetMatrix;\r
7 import org.simantics.spreadsheet.graph.parser.ast.AstArgList;\r
8 \r
9 class SumifFormulaFunction implements CellFormulaFunction<Object> {\r
10 \r
11     @Override\r
12     public Object evaluate(CellValueVisitor visitor, AstArgList args) {\r
13         if (args.values.size() == 2) {\r
14             Object test = args.values.get(0).accept(visitor);\r
15             Object criteria = null;\r
16             try {\r
17                 criteria = args.values.get(1).accept(visitor);\r
18             } catch (IllegalStateException e){\r
19                 return 0;\r
20             }\r
21             FormulaError2 error = FormulaError2.forObject(criteria);\r
22             if(error!=null) return error.getString();\r
23             \r
24             if (test instanceof SpreadsheetMatrix) {\r
25                 double sum = 0.0;\r
26                 SpreadsheetMatrix tm = (SpreadsheetMatrix) test;\r
27                 for (int i = 0; i < tm.values.length; i++) {\r
28                     if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) {\r
29                         double d = SpreadsheetGraphUtils.asNumber(tm.values[i]);\r
30                         if (Double.isFinite(d))\r
31                             sum += d;\r
32                     }\r
33                 }\r
34                 return sum;\r
35             } else {\r
36                 Double d = SpreadsheetGraphUtils.asNumber(test);\r
37                 return d;\r
38             }\r
39         }\r
40 \r
41         if (args.values.size() == 3) {\r
42             Object test = args.values.get(0).accept(visitor);\r
43             Object criteria = null;\r
44             try {\r
45                 criteria = args.values.get(1).accept(visitor);\r
46             } catch (IllegalStateException e){\r
47                 return 0;\r
48             }\r
49             FormulaError2 error = FormulaError2.forObject(criteria);\r
50             if(error!=null) return error.getString();\r
51             \r
52             Object range = args.values.get(2).accept(visitor);\r
53 \r
54             if (test instanceof SpreadsheetMatrix) {\r
55                 Double sum = 0.0;\r
56                 SpreadsheetMatrix tm = (SpreadsheetMatrix) test;\r
57                 SpreadsheetMatrix rm = (SpreadsheetMatrix) range;\r
58                 for (int i = 0; i < tm.values.length; i++) {\r
59                     if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) {\r
60                         double d = SpreadsheetGraphUtils.asNumber(rm.values[i]);\r
61                         if (Double.isFinite(d))\r
62                             sum += d;\r
63                     }\r
64                 }\r
65                 return sum;\r
66             } else {\r
67                 Double d = SpreadsheetGraphUtils.asNumber(test);\r
68                 return d;\r
69             }\r
70 \r
71         }\r
72         throw new IllegalStateException();\r
73     }\r
74 }