]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet/src/org/simantics/spreadsheet/solver/formula/IfFormulaFunction.java
Adopt spreadsheet changes made in Balas development
[simantics/platform.git] / bundles / org.simantics.spreadsheet / src / org / simantics / spreadsheet / solver / formula / IfFormulaFunction.java
1 package org.simantics.spreadsheet.solver.formula;
2
3 import org.simantics.spreadsheet.Spreadsheets;
4 import org.simantics.spreadsheet.solver.formula.parser.ast.AstArgList;
5 import org.simantics.spreadsheet.solver.formula.parser.ast.AstNothing;
6 import org.simantics.spreadsheet.solver.formula.parser.ast.AstValue;
7
8 public class IfFormulaFunction implements CellFormulaFunction<Object> {
9
10     @Override
11     public Object evaluate(CellValueVisitor visitor, AstArgList args) {
12         if (args.values.size() != 3) throw new IllegalStateException();
13
14         Object condition = args.values.get(0).accept(visitor);
15         AstValue ifTrueResult = args.values.get(1);
16         AstValue ifFalseResult = args.values.get(2);
17
18         FormulaError2 error = FormulaError2.forObject(condition);
19         if(error!=null) return error.getString();
20
21         if (Spreadsheets.asBoolean(condition)) {
22             if(ifTrueResult==null || ifTrueResult instanceof AstNothing)
23                 return 0;
24             try {
25                 return ifTrueResult.accept(visitor);
26             } catch (IllegalStateException e){
27                 return FormulaError2.NAME.getString();
28             }
29         }
30         else {
31             if(ifFalseResult==null || ifFalseResult instanceof AstNothing)
32                 return 0;
33             try {
34                 return ifFalseResult.accept(visitor);
35             } catch (IllegalStateException e){
36                 return FormulaError2.NAME.getString();
37             }
38         }
39     }
40 }