X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fformula%2FSumifFormulaFunction.java;fp=bundles%2Forg.simantics.spreadsheet.graph%2Fsrc%2Forg%2Fsimantics%2Fspreadsheet%2Fgraph%2Fformula%2FSumifFormulaFunction.java;h=8826a384220b554976ed012e515749841a0fdd55;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=44c38ea5c6a5967ea98b2ac66cec646e773bc88b;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/SumifFormulaFunction.java b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/SumifFormulaFunction.java index 44c38ea5c..8826a3842 100644 --- a/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/SumifFormulaFunction.java +++ b/bundles/org.simantics.spreadsheet.graph/src/org/simantics/spreadsheet/graph/formula/SumifFormulaFunction.java @@ -1,74 +1,74 @@ -package org.simantics.spreadsheet.graph.formula; - -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; - -class SumifFormulaFunction implements CellFormulaFunction { - - @Override - public Object evaluate(CellValueVisitor visitor, AstArgList args) { - if (args.values.size() == 2) { - Object test = args.values.get(0).accept(visitor); - Object criteria = null; - try { - criteria = args.values.get(1).accept(visitor); - } catch (IllegalStateException e){ - return 0; - } - FormulaError2 error = FormulaError2.forObject(criteria); - if(error!=null) return error.getString(); - - if (test instanceof SpreadsheetMatrix) { - double sum = 0.0; - SpreadsheetMatrix tm = (SpreadsheetMatrix) test; - for (int i = 0; i < tm.values.length; i++) { - if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) { - double d = SpreadsheetGraphUtils.asNumber(tm.values[i]); - if (Double.isFinite(d)) - sum += d; - } - } - return sum; - } else { - Double d = SpreadsheetGraphUtils.asNumber(test); - return d; - } - } - - if (args.values.size() == 3) { - Object test = args.values.get(0).accept(visitor); - Object criteria = null; - try { - criteria = args.values.get(1).accept(visitor); - } catch (IllegalStateException e){ - return 0; - } - FormulaError2 error = FormulaError2.forObject(criteria); - if(error!=null) return error.getString(); - - Object range = args.values.get(2).accept(visitor); - - if (test instanceof SpreadsheetMatrix) { - Double sum = 0.0; - SpreadsheetMatrix tm = (SpreadsheetMatrix) test; - SpreadsheetMatrix rm = (SpreadsheetMatrix) range; - for (int i = 0; i < tm.values.length; i++) { - if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) { - double d = SpreadsheetGraphUtils.asNumber(rm.values[i]); - if (Double.isFinite(d)) - sum += d; - } - } - return sum; - } else { - Double d = SpreadsheetGraphUtils.asNumber(test); - return d; - } - - } - throw new IllegalStateException(); - } +package org.simantics.spreadsheet.graph.formula; + +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; + +class SumifFormulaFunction implements CellFormulaFunction { + + @Override + public Object evaluate(CellValueVisitor visitor, AstArgList args) { + if (args.values.size() == 2) { + Object test = args.values.get(0).accept(visitor); + Object criteria = null; + try { + criteria = args.values.get(1).accept(visitor); + } catch (IllegalStateException e){ + return 0; + } + FormulaError2 error = FormulaError2.forObject(criteria); + if(error!=null) return error.getString(); + + if (test instanceof SpreadsheetMatrix) { + double sum = 0.0; + SpreadsheetMatrix tm = (SpreadsheetMatrix) test; + for (int i = 0; i < tm.values.length; i++) { + if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) { + double d = SpreadsheetGraphUtils.asNumber(tm.values[i]); + if (Double.isFinite(d)) + sum += d; + } + } + return sum; + } else { + Double d = SpreadsheetGraphUtils.asNumber(test); + return d; + } + } + + if (args.values.size() == 3) { + Object test = args.values.get(0).accept(visitor); + Object criteria = null; + try { + criteria = args.values.get(1).accept(visitor); + } catch (IllegalStateException e){ + return 0; + } + FormulaError2 error = FormulaError2.forObject(criteria); + if(error!=null) return error.getString(); + + Object range = args.values.get(2).accept(visitor); + + if (test instanceof SpreadsheetMatrix) { + Double sum = 0.0; + SpreadsheetMatrix tm = (SpreadsheetMatrix) test; + SpreadsheetMatrix rm = (SpreadsheetMatrix) range; + for (int i = 0; i < tm.values.length; i++) { + if (SpreadsheetGraphUtils.matchCriteria(tm.values[i], criteria)) { + double d = SpreadsheetGraphUtils.asNumber(rm.values[i]); + if (Double.isFinite(d)) + sum += d; + } + } + return sum; + } else { + Double d = SpreadsheetGraphUtils.asNumber(test); + return d; + } + + } + throw new IllegalStateException(); + } } \ No newline at end of file