/** * Case-switch expresssion. * @param valueToSwitchOn provides the switch value * @return expression */ public static CaseSwitchExpression caseSwitch(Expression valueToSwitchOn) { return new CaseSwitchExpression(valueToSwitchOn); }
/** * Sets the else-part of the case-switch. This result of this expression is returned * when no when-expression matched. * @param elseExpr is the expression returning the no-match value * @return expression */ public CaseSwitchExpression setElse(Expression elseExpr) { int size = this.getChildren().size(); // remove last node representing the else if (size % 2 != 1) { this.getChildren().remove(size - 1); } this.addChild(elseExpr); return this; }
/** * Ctor. * @param switchValue is the expression providing the value to switch on */ public CaseSwitchExpression(Expression switchValue) { // switch value expression is first this.addChild(switchValue); }
public void toEPL(StringWriter writer) { writer.write("case "); getChildren().get(0).toEPL(writer); int index = 1; while(index < this.getChildren().size() - 1) { writer.write(" when "); getChildren().get(index).toEPL(writer); index++; if (index == this.getChildren().size()) { throw new IllegalStateException("Invalid case-when expression, count of when-to-then nodes not matching"); } writer.write(" then "); getChildren().get(index).toEPL(writer); index++; } if (index < this.getChildren().size()) { writer.write(" else "); getChildren().get(index).toEPL(writer); } writer.write(" end"); } }
/** * Adds a pair of expressions representing a "when" and a "then" in the switch. * @param when expression to match on * @param then expression to return a conditional result when the when-expression matches * @return expression */ public CaseSwitchExpression add(Expression when, Expression then) { int size = this.getChildren().size(); if (size % 2 == 1) { this.addChild(when); this.addChild(then); } else { // add next to last as the last node is the else clause this.getChildren().add(this.getChildren().size() - 1, when); this.getChildren().add(this.getChildren().size() - 1, then); } return this; }
/** * Case-switch expresssion. * @param propertyName the name of the property that provides the switch value * @return expression */ public static CaseSwitchExpression caseSwitch(String propertyName) { return new CaseSwitchExpression(getPropExpr(propertyName)); }
if (mycase.isCase2()) return new CaseSwitchExpression();