]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/AverageFormulaFunction.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / formula / AverageFormulaFunction.java
diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/AverageFormulaFunction.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/AverageFormulaFunction.java
new file mode 100644 (file)
index 0000000..f8b7196
--- /dev/null
@@ -0,0 +1,53 @@
+package org.simantics.spreadsheet.graph.formula;\r
+\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.spreadsheet.graph.CellFormulaFunction;\r
+import org.simantics.spreadsheet.graph.CellValueVisitor;\r
+import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;\r
+import org.simantics.spreadsheet.graph.SpreadsheetMatrix;\r
+import org.simantics.spreadsheet.graph.parser.ast.AstArgList;\r
+import org.simantics.spreadsheet.graph.parser.ast.AstValue;\r
+\r
+public class AverageFormulaFunction implements CellFormulaFunction<Object> {\r
+\r
+    @Override\r
+    public Object evaluate(CellValueVisitor visitor, AstArgList args) {\r
+        if (args.values.size() == 0)\r
+            throw new IllegalStateException();\r
+        Double sum = 0.0;\r
+        double count = 0.0;\r
+        for(AstValue value : args.values){\r
+               Object res = value.accept(visitor);\r
+               if (res instanceof SpreadsheetMatrix) {\r
+              Object value2 = ((SpreadsheetMatrix) res).sumWithFormulaError();\r
+              if(value2 instanceof String) return value2;\r
+              \r
+              sum += ((Number)value2).doubleValue();\r
+              count += ((SpreadsheetMatrix) res).countOfActualDoubleValues();\r
+               } else {\r
+                       FormulaError2 err = FormulaError2.forObject(res);\r
+                       if(err!=null) return err.getString();\r
+                       \r
+                       if(res instanceof Variant){\r
+                               Double dVal = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);\r
+                               if(dVal==null) res = ((Variant)res).toString();\r
+                               else res = dVal;\r
+                       }\r
+                       \r
+                       Double v = null;\r
+               if(res instanceof String && !res.equals("")){\r
+                       v = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);\r
+               }\r
+               else if(res instanceof Number)\r
+                       v = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);\r
+               \r
+                       if(v!=null){\r
+                               sum += v;\r
+                               count++;\r
+                       }\r
+               }\r
+        }\r
+        if(count==0.0) return FormulaError2.DIV0.getString();\r
+        return sum/count;\r
+    }\r
+}
\ No newline at end of file