]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/AverageFormulaFunction.java
SpreadsheetCells with Circular References support iterations.
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / formula / AverageFormulaFunction.java
1 package org.simantics.spreadsheet.graph.formula;\r
2 \r
3 import org.simantics.databoard.binding.mutable.Variant;\r
4 import org.simantics.spreadsheet.graph.CellFormulaFunction;\r
5 import org.simantics.spreadsheet.graph.CellValueVisitor;\r
6 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;\r
7 import org.simantics.spreadsheet.graph.SpreadsheetMatrix;\r
8 import org.simantics.spreadsheet.graph.parser.ast.AstArgList;\r
9 import org.simantics.spreadsheet.graph.parser.ast.AstValue;\r
10 \r
11 public class AverageFormulaFunction implements CellFormulaFunction<Object> {\r
12 \r
13     @Override\r
14     public Object evaluate(CellValueVisitor visitor, AstArgList args) {\r
15         if (args.values.size() == 0)\r
16             throw new IllegalStateException();\r
17         Double sum = 0.0;\r
18         double count = 0.0;\r
19         for(AstValue value : args.values){\r
20                 Object res = value.accept(visitor);\r
21                 if (res instanceof SpreadsheetMatrix) {\r
22               Object value2 = ((SpreadsheetMatrix) res).sumWithFormulaError();\r
23               if(value2 instanceof String) return value2;\r
24               \r
25               sum += ((Number)value2).doubleValue();\r
26               count += ((SpreadsheetMatrix) res).countOfActualDoubleValues();\r
27                 } else {\r
28                         FormulaError2 err = FormulaError2.forObject(res);\r
29                         if(err!=null) return err.getString();\r
30                         \r
31                         if(res instanceof Variant){\r
32                                 Double dVal = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);\r
33                                 if(dVal==null) res = ((Variant)res).toString();\r
34                                 else res = dVal;\r
35                         }\r
36                         \r
37                         Double v = null;\r
38                 if(res instanceof String && !res.equals("")){\r
39                         v = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);\r
40                 }\r
41                 else if(res instanceof Number)\r
42                         v = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);\r
43                 \r
44                         if(v!=null){\r
45                                 sum += v;\r
46                                 count++;\r
47                         }\r
48                 }\r
49         }\r
50         if(count==0.0) return FormulaError2.DIV0.getString();\r
51         return sum/count;\r
52     }\r
53 }