@TargetOperator(Branch.class)
public class BranchFlowProcessor extends LineEndProcessor {
@Override
public void emitLineEnd(Context context) {
ModelFactory f = context.getModelFactory();
Expression input = context.getInput();
Expression impl = context.createImplementation();
OperatorDescription desc = context.getOperatorDescription();
Method method = desc.getDeclaration().toMethod();
assert method != null : desc.getDeclaration();
Class<?> enumType = method.getReturnType();
List<Expression> arguments = Lists.create();
arguments.add(input);
for (OperatorDescription.Parameter param : desc.getParameters()) {
arguments.add(Models.toLiteral(f, param.getValue()));
}
Expression result = context.createLocalVariable(
enumType,
new ExpressionBuilder(f, impl)
.method(desc.getDeclaration().getName(), arguments)
.toExpression());
List<Tuple2<Enum<?>, FlowElementPortDescription>> constants =
EnumUtil.extractConstants(enumType, desc.getOutputPorts());
List<Statement> cases = Lists.create();
for (Tuple2<Enum<?>, FlowElementPortDescription> tuple : constants) {
Enum<?> constant = tuple.first;
FlowElementPortDescription port = tuple.second;
ResultMirror next = context.getOutput(port);
cases.add(f.newSwitchCaseLabel(f.newSimpleName(constant.name())));
cases.add(next.createAdd(input));
cases.add(f.newBreakStatement());
}
cases.add(f.newSwitchDefaultLabel());
cases.add(new TypeBuilder(f, context.convert(AssertionError.class))
.newObject(result)
.toThrowStatement());
context.add(f.newSwitchStatement(result, cases));
}