package stepwise; import java.util.LinkedList; public abstract class CoroutineBase implements Coroutine { private LinkedList> _actions; private boolean _mustYieldAction; public CoroutineBase(final boolean mustYieldAction) { _mustYieldAction = mustYieldAction; _actions = new LinkedList>(); } @Override public Report nextStep() { Report rep = null; boolean didVisit = false; while (true) { rep = _actions.poll(); if (rep == null) { if (didVisit && _mustYieldAction) throw new RuntimeException( "Visit method did not end with at least one action."); visit(); didVisit = true; } else { return rep; } } } protected abstract void visit(); protected void emit(final I info) { _actions.add(new ReportInfo(info)); } protected void resumeAfter(final Stepwise child) { _actions.add(new ReportChild(child)); } protected void abort(final E failure) { _actions.add(new ReportFail(failure)); } protected void done() { _actions.add(new ReportDone()); } protected void commit(final Stepwise comp) { _actions.add(new ReportReplace(comp)); } }