1 package org.simantics.spreadsheet.solver.formula;
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;
8 public class IfFormulaFunction implements CellFormulaFunction<Object> {
11 public Object evaluate(CellValueVisitor visitor, AstArgList args) {
12 if (args.values.size() != 3) throw new IllegalStateException();
14 Object condition = args.values.get(0).accept(visitor);
15 AstValue ifTrueResult = args.values.get(1);
16 AstValue ifFalseResult = args.values.get(2);
18 FormulaError2 error = FormulaError2.forObject(condition);
19 if(error!=null) return error.getString();
21 if (Spreadsheets.asBoolean(condition)) {
22 if(ifTrueResult==null || ifTrueResult instanceof AstNothing)
25 return ifTrueResult.accept(visitor);
26 } catch (IllegalStateException e){
27 return FormulaError2.NAME.getString();
31 if(ifFalseResult==null || ifFalseResult instanceof AstNothing)
34 return ifFalseResult.accept(visitor);
35 } catch (IllegalStateException e){
36 return FormulaError2.NAME.getString();