package edu.rice.comp322; import edu.rice.hj.Module0; import edu.rice.hj.runtime.actors.Actor; import static edu.rice.hj.Module0.launchHabaneroApp; /** *

SimplePipeline class.

* * @author Shams Imam (shams@rice.edu) */ public final class Lec29Slide6Pipeline { /** * Disallow instance creation of utility class. */ private Lec29Slide6Pipeline() { super(); } /** *

main.

* * @param args an array of {@link String} objects. */ public static void main(final String[] args) { launchHabaneroApp(() -> { // chain the actors final EchoStage echoStage = new EchoStage(); final LowerCaseFilter lowerCaseFilterStage = new LowerCaseFilter(echoStage); final EvenLengthFilter evenLengthFilterStage = new EvenLengthFilter(lowerCaseFilterStage); evenLengthFilterStage.start(); evenLengthFilterStage.send("A"); evenLengthFilterStage.send("Simple"); evenLengthFilterStage.send("pipeline"); evenLengthFilterStage.send("with"); evenLengthFilterStage.send("3"); evenLengthFilterStage.send("stages"); evenLengthFilterStage.send(new StopMessage()); }); } private static class StopMessage { } /** * Only forwards inputs with even length strings */ private static class EvenLengthFilter extends Actor { private final Actor nextStage; EvenLengthFilter(final Actor nextStage) { this.nextStage = nextStage; } @Override protected void onPostStart() { nextStage.start(); } protected void process(final Object msg) { if (msg instanceof StopMessage) { nextStage.send(msg); exit(); } else if (msg instanceof String) { String msgStr = (String) msg; if (msgStr.length() % 2 == 0) { nextStage.send(msgStr); } } } } /** * Only forwards inputs with all lowercase strings */ private static class LowerCaseFilter extends Actor { private final Actor nextStage; LowerCaseFilter(final Actor nextStage) { this.nextStage = nextStage; } @Override protected void onPostStart() { nextStage.start(); } protected void process(final Object msg) { if (msg instanceof StopMessage) { exit(); nextStage.send(msg); } else if (msg instanceof String) { String msgStr = (String) msg; if (msgStr.toLowerCase().equals(msgStr)) { nextStage.send(msgStr); } } } } /** * Prints any input strings */ private static class EchoStage extends Actor { protected void process(final Object msg) { if (msg instanceof StopMessage) { exit(); } else if (msg instanceof String) { System.out.println(msg); } } } }