-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
+package org.simantics.spreadsheet.graph.formula;
+
+import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.spreadsheet.graph.CellFormulaFunction;
+import org.simantics.spreadsheet.graph.CellValueVisitor;
+import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
+import org.simantics.spreadsheet.graph.SpreadsheetMatrix;
+import org.simantics.spreadsheet.graph.parser.ast.AstArgList;
+import org.simantics.spreadsheet.graph.parser.ast.AstValue;
+
+public class AverageFormulaFunction implements CellFormulaFunction<Object> {
+
+ @Override
+ public Object evaluate(CellValueVisitor visitor, AstArgList args) {
+ if (args.values.size() == 0)
+ throw new IllegalStateException();
+ Double sum = 0.0;
+ double count = 0.0;
+ for(AstValue value : args.values){
+ Object res = value.accept(visitor);
+ if (res instanceof SpreadsheetMatrix) {
+ Object value2 = ((SpreadsheetMatrix) res).sumWithFormulaError();
+ if(value2 instanceof String) return value2;
+
+ sum += ((Number)value2).doubleValue();
+ count += ((SpreadsheetMatrix) res).countOfActualDoubleValues();
+ } else {
+ FormulaError2 err = FormulaError2.forObject(res);
+ if(err!=null) return err.getString();
+
+ if(res instanceof Variant){
+ Double dVal = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);
+ if(dVal==null) res = ((Variant)res).toString();
+ else res = dVal;
+ }
+
+ Double v = null;
+ if(res instanceof String && !res.equals("")){
+ v = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);
+ }
+ else if(res instanceof Number)
+ v = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(res);
+
+ if(v!=null){
+ sum += v;
+ count++;
+ }
+ }
+ }
+ if(count==0.0) return FormulaError2.DIV0.getString();
+ return sum/count;
+ }
}
\ No newline at end of file