X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fformula%2FAverageFormulaFunction.java;fp=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fformula%2FAverageFormulaFunction.java;h=b8ca32e7f4c9dbd809716e6e60b9c73a7d63cdaa;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=f8b7196e3569b18878456682ba121176707da83b;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git 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 index f8b7196e3..b8ca32e7f 100644 --- 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 @@ -1,53 +1,53 @@ -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 { - - @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; - } +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 { + + @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