]> gerrit.simantics Code Review - simantics/platform.git/blob
299973db59329d0d56ea9dfff0e5f1251b4fbcc1
[simantics/platform.git] /
1 package org.simantics.spreadsheet.graph.formula;
2
3 import org.simantics.spreadsheet.graph.CellFormulaFunction;
4 import org.simantics.spreadsheet.graph.CellValueVisitor;
5 import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;
6 import org.simantics.spreadsheet.graph.SpreadsheetMatrix;
7 import org.simantics.spreadsheet.graph.parser.ast.AstArgList;
8 import org.simantics.spreadsheet.graph.parser.ast.AstValue;
9
10 class SumFormulaFunction implements CellFormulaFunction<Object> {
11
12     @Override
13     public Object evaluate(CellValueVisitor visitor, AstArgList args) {
14         Double sum = 0.0;
15         for (AstValue value : args.values) {
16             Object result = value.accept(visitor);
17             
18             if (result instanceof SpreadsheetMatrix) {
19                 Object value2 = ((SpreadsheetMatrix) result).sumWithFormulaError();
20                 if(value2 instanceof String) return value2; //means sumWithFormulaError returned an Error message.
21                 sum += ((Number)value2).doubleValue();
22             } else if(result instanceof String && !result.equals("")){
23                 FormulaError2 error = FormulaError2.forString(result.toString());
24                 if(error!=null) return error.getString();
25                 Double v = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(result);
26                 if(v!=null)
27                         sum += v;
28             } else if(result instanceof Number){
29                 sum += ((Number)result).doubleValue();
30             } else {
31                 sum += SpreadsheetGraphUtils.asNumber(result);
32             }
33         }
34         return sum;
35     }
36 }