1 package org.simantics.spreadsheet.graph.formula;
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.parser.ast.AstArgList;
7 import org.simantics.spreadsheet.graph.parser.ast.AstNothing;
8 import org.simantics.spreadsheet.graph.parser.ast.AstValue;
10 public class IfFormulaFunction implements CellFormulaFunction<Object> {
13 public Object evaluate(CellValueVisitor visitor, AstArgList args) {
14 if (args.values.size() != 3) throw new IllegalStateException();
16 Object condition = args.values.get(0).accept(visitor);
17 AstValue ifTrueResult = args.values.get(1);
18 AstValue ifFalseResult = args.values.get(2);
20 FormulaError2 error = FormulaError2.forObject(condition);
21 if(error!=null) return error.getString();
23 if (SpreadsheetGraphUtils.asBoolean(condition)) {
24 if(ifTrueResult==null || ifTrueResult instanceof AstNothing)
27 return ifTrueResult.accept(visitor);
28 } catch (IllegalStateException e){
29 return FormulaError2.NAME.getString();
33 if(ifFalseResult==null || ifFalseResult instanceof AstNothing)
36 return ifFalseResult.accept(visitor);
37 } catch (IllegalStateException e){
38 return FormulaError2.NAME.getString();