]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/GeomeanFormulaFunction.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.graph / src / org / simantics / spreadsheet / graph / formula / GeomeanFormulaFunction.java
diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/GeomeanFormulaFunction.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/GeomeanFormulaFunction.java
new file mode 100644 (file)
index 0000000..fa23d72
--- /dev/null
@@ -0,0 +1,47 @@
+package org.simantics.spreadsheet.graph.formula;\r
+\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 GeomeanFormulaFunction 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 result = 1.0;\r
+        double count = 0.0;\r
+        for (AstValue value : args.values){\r
+               Object r = value.accept(visitor);\r
+               if (r instanceof SpreadsheetMatrix) {\r
+              Object v = ((SpreadsheetMatrix) r).productWithFormulaError();\r
+              if(v instanceof String) return v;\r
+              double dval = ((Number)v).doubleValue();\r
+              if(dval!=0.0){\r
+                  result = result*dval;\r
+                  count += ((SpreadsheetMatrix) r).countOfActualDoubleValues();\r
+              }\r
+               } else {\r
+                               FormulaError2 error = FormulaError2.forObject(r);\r
+                               if(error!=null) return error.getString();\r
+                       \r
+                               Number vNum = SpreadsheetGraphUtils.asValidNumber(r);\r
+                               Double v = null;\r
+                               if(vNum!=null) v = vNum.doubleValue();\r
+                               \r
+                       if(v!=null){\r
+                               if(v<=0) return FormulaError2.NUM.getString();\r
+                               result = result*v;\r
+                               count++;\r
+                       }\r
+               }\r
+        }\r
+        if(result==0.0 || count==0.0) return FormulaError2.NUM.getString();\r
+        return Double.valueOf(Math.pow(result, (1.0/count)));\r
+    }\r
+\r
+}\r