Created
July 8, 2020 15:31
-
-
Save t81lal/ddbd62125ea6eb69c782e48356186934 to your computer and use it in GitHub Desktop.
TestPipeline2WithGuice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.krakenrs.spade.pipeline; | |
import java.util.function.Function; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Guice; | |
import com.google.inject.Inject; | |
import com.google.inject.Injector; | |
public class TestPipeline2 { | |
static <T> PipelineHeadStub<T> from() { | |
return new PipelineHeadStub<>(); | |
} | |
static class StepA implements PipelineStep<Integer, Integer> { | |
private BongoObject bongoObject; | |
@Inject | |
public StepA(BongoObject bongoObject) { | |
this.bongoObject = bongoObject; | |
System.out.println("created with " + bongoObject.bongoValue); | |
} | |
@Override | |
public Integer apply(Integer t) { | |
System.out.println("executed with " + bongoObject.bongoValue); | |
bongoObject.bongoValue = "bongo After A for input: " + t; | |
return t + 2; | |
} | |
} | |
static class StepB implements PipelineStep<Integer, Integer> { | |
private BongoObject bongoObject; | |
@Inject | |
public StepB(BongoObject bongoObject) { | |
this.bongoObject = bongoObject; | |
System.out.println("bongo B " + bongoObject.bongoValue); | |
} | |
@Override | |
public Integer apply(Integer t) { | |
System.out.println(" bongo B got " + bongoObject.bongoValue); | |
return t * 5; | |
} | |
} | |
static PipelineStep<Integer, String> asHex() { | |
return Integer::toHexString; | |
} | |
static class TestModule extends AbstractModule { | |
@Override | |
protected void configure() { | |
var globalBongo = new BongoObject(); | |
globalBongo.bongoValue = "bongoINIT"; | |
bind(BongoObject.class).toInstance(globalBongo); | |
} | |
} | |
static class BongoObject { | |
String bongoValue = ""; | |
} | |
static <S> Function<S, PipelineExecutionContext<S>> contextCreator() { | |
return s -> { | |
Injector injector = Guice.createInjector(new TestModule()); | |
return new GuicedPipelineExecutionContext<S>(s, injector); | |
}; | |
} | |
public static void main(String[] args) throws ExecutionException { | |
PipelineExecutor<Integer, String> executor = TestPipeline2.<Integer>from().then(StepA.class).then(StepB.class) | |
.then(asHex()).build(contextCreator()); | |
var inputs = new int[] { 1, 2, 3 }; | |
for (int i : inputs) { | |
String res = executor.execute(i); | |
System.out.println(res); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment