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