package org.gcube.smartgears.handlers; import java.util.ArrayList; import java.util.List; /** * An ordered list {@link Handler}s that handle related events. * * @author Fabio Simeoni * * @param the type of events * @param the type of handlers * * */ public abstract class Pipeline> { //private static Logger log = LoggerFactory.getLogger(Pipeline.class); private final List handlers; private int cursor = 0; /** * Creates an instance with a given list of handlers. *

* Modification to the input list do not affect the pipeline. * * @param handlers the handlers */ public Pipeline(List handlers) { this.handlers = new ArrayList(handlers); // defensive copy } /** * Returns the handlers in this pipeline. *

* Modification to the input list do not affect the pipeline. * * @return the handlers */ public List handlers() { return new ArrayList(handlers); // defensive copy } /** * Forwards an event through the pipeline. * * @param event the event */ public void forward(E event) { if (cursor >= handlers.size()) { reset(); return; } H handler = handlers.get(cursor); cursor++; //log.trace("forwarding {} to {}", event, handler); try { handler.onEvent(event); } catch(RuntimeException e) { reset(); throw e; } forward(event); //make sure it's the last thing we do, or it will keep acting as recursion retracts } private void reset() { cursor=0; } }