Last active
July 8, 2020 14:43
-
-
Save t81lal/4e270895a0164731f3d841ef864558eb to your computer and use it in GitHub Desktop.
TestPipeline2.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.pipeline2; | |
import java.util.function.Function; | |
public class TestPipeline2 { | |
static interface PipelineStep<I, O> extends Function<I, O> { | |
} | |
static interface PipelineProducer<S, I> { | |
I get(PipelineExecutionContext<S> context); | |
} | |
static class Pipeline<S, I, O> implements PipelineProducer<S, O> { | |
PipelineHeadStub<S> head; | |
PipelineProducer<S, I> inputProducer; | |
PipelineStep<I, O> step; | |
Pipeline(PipelineHeadStub<S> head, PipelineProducer<S, I> inputProducer, PipelineStep<I, O> step) { | |
this.head = head; | |
this.inputProducer = inputProducer; | |
this.step = step; | |
} | |
public <T> Pipeline<S, O, T> then(PipelineStep<O, T> step) { | |
return new Pipeline<>(head, this, step); | |
} | |
@Override | |
public O get(PipelineExecutionContext<S> context) { | |
return step.apply(inputProducer.get(context)); | |
} | |
public PipelineExecutorImpl<S, O> build() { | |
return new PipelineExecutorImpl<>(this); | |
} | |
} | |
static class PipelineHeadStub<V> implements PipelineProducer<V, V> { | |
public <T> Pipeline<V, V, T> then(PipelineStep<V, T> step) { | |
return new Pipeline<>(this, this, step); | |
} | |
@Override | |
public V get(PipelineExecutionContext<V> context) { | |
return context.input; | |
} | |
} | |
static class PipelineExecutionContext<V> implements PipelineProducer<V, V> { | |
V input; | |
PipelineExecutionContext(V input) { | |
this.input = input; | |
} | |
@Override | |
public V get(PipelineExecutionContext<V> ctx) { | |
if (ctx != this) { | |
throw new IllegalStateException(); | |
} | |
return input; | |
} | |
} | |
static interface PipelineExecutor<S, O> { | |
O execute(S input); | |
} | |
static class PipelineExecutorImpl<S, O> implements PipelineExecutor<S, O> { | |
private Pipeline<S, ?, O> pipeline; | |
public PipelineExecutorImpl(Pipeline<S, ?, O> pipeline) { | |
this.pipeline = pipeline; | |
} | |
@Override | |
public O execute(S input) { | |
PipelineExecutionContext<S> context = new PipelineExecutionContext<>(input); | |
return pipeline.get(context); | |
} | |
} | |
static <T> PipelineHeadStub<T> from() { | |
return new PipelineHeadStub<>(); | |
} | |
static PipelineStep<Integer, Integer> addTwo() { | |
return x -> x + 2; | |
} | |
static PipelineStep<Integer, Integer> mul5() { | |
return x -> x * 5; | |
} | |
static PipelineStep<Integer, String> asHex() { | |
return Integer::toHexString; | |
} | |
public static void main(String[] args) { | |
PipelineExecutor<Integer, String> executor = TestPipeline2.<Integer>from().then(addTwo()).then(mul5()) | |
.then(asHex()).build(); | |
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