com.cloudbees.groovy.cps.impl.SwitchBlock Maven / Gradle / Ivy
package com.cloudbees.groovy.cps.impl;
import com.cloudbees.groovy.cps.Block;
import com.cloudbees.groovy.cps.CaseExpression;
import com.cloudbees.groovy.cps.Continuation;
import com.cloudbees.groovy.cps.Env;
import com.cloudbees.groovy.cps.Next;
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;
import java.util.List;
/**
* switch/case statement.
*
* @author Kohsuke Kawaguchi
*/
public class SwitchBlock implements Block {
final String label;
final Block exp;
final List cases;
/**
* Statement to run in case there's no match. Can be null.
*/
final Block default_;
public SwitchBlock(String label, Block exp, Block default_, List cases) {
this.label = label;
this.exp = exp;
this.cases = cases;
this.default_ = default_;
}
public Next eval(Env e, Continuation k) {
return new ContinuationImpl(e,k).then(exp, e, test);
}
class ContinuationImpl extends ContinuationGroup {
final Continuation k;
final Env e;
/**
* {@link Env} to evaluate case statements in, that changes the target of the "break" statement.
*/
final CaseEnv caseEnv;
/**
* Result of evaluating {@link #exp}
*/
Object switchValue;
/**
* {@link CaseExpression} in {@link #cases} that we are testing.
*/
int index;
ContinuationImpl(Env e, Continuation k) {
this.e = e;
this.k = k;
this.caseEnv = new CaseEnv(e,label,k);
}
public Next test(Object value) {
this.switchValue = value;
return matcher();
}
private Next matcher() {
if (index