+package org.simantics.spreadsheet.graph.formula;\r
+\r
+import org.simantics.spreadsheet.graph.CellFormulaFunction;\r
+import org.simantics.spreadsheet.graph.CellValueVisitor;\r
+import org.simantics.spreadsheet.graph.SpreadsheetGraphUtils;\r
+import org.simantics.spreadsheet.graph.parser.ast.AstArgList;\r
+import org.simantics.spreadsheet.graph.parser.ast.AstNothing;\r
+import org.simantics.spreadsheet.graph.parser.ast.AstValue;\r
+\r
+public class IfFormulaFunction implements CellFormulaFunction<Object> {\r
+\r
+ @Override\r
+ public Object evaluate(CellValueVisitor visitor, AstArgList args) {\r
+ if (args.values.size() != 3) throw new IllegalStateException();\r
+ \r
+ Object condition = args.values.get(0).accept(visitor);\r
+ AstValue ifTrueResult = args.values.get(1);\r
+ AstValue ifFalseResult = args.values.get(2);\r
+ \r
+ FormulaError2 error = FormulaError2.forObject(condition);\r
+ if(error!=null) return error.getString();\r
+\r
+ if (SpreadsheetGraphUtils.asBoolean(condition)) {\r
+ if(ifTrueResult==null || ifTrueResult instanceof AstNothing)\r
+ return 0;\r
+ try {\r
+ return ifTrueResult.accept(visitor);\r
+ } catch (IllegalStateException e){\r
+ return FormulaError2.NAME.getString();\r
+ }\r
+ }\r
+ else {\r
+ if(ifFalseResult==null || ifFalseResult instanceof AstNothing)\r
+ return 0;\r
+ try {\r
+ return ifFalseResult.accept(visitor);\r
+ } catch (IllegalStateException e){\r
+ return FormulaError2.NAME.getString();\r
+ }\r
+ }\r
+ }\r
+}
\ No newline at end of file