Created
July 8, 2020 13:03
-
-
Save t81lal/4a015750a86241b0061240c28dbc7530 to your computer and use it in GitHub Desktop.
TestPipeline.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; | |
public class TestPipeline { | |
public interface PipelineBuilder<I> { | |
<O> PipelineBuilder<O> then(PipelineStep<I, O> step); | |
void run(); | |
} | |
public interface PipelineStep<I, O> { | |
O execute(I input); | |
} | |
static class BasicPipelineBuilder<I, OB> implements PipelineBuilder<I> { | |
BasicPipelineBuilder<?, OB> parent; | |
PipelineStep<I, ?> step; | |
OB result; | |
BasicPipelineBuilder() { | |
} | |
BasicPipelineBuilder(BasicPipelineBuilder<?, OB> parent) { | |
this.parent = parent; | |
} | |
@Override | |
public void run() { | |
I prevOut = null; | |
if(parent != null) { | |
parent.run(); | |
prevOut = (I) parent.result; | |
} | |
if(step != null) { | |
result = (OB) step.execute(prevOut); | |
} | |
} | |
@Override | |
public <O> BasicPipelineBuilder<O, ?> then(PipelineStep<I, O> step) { | |
this.step = step; | |
return new BasicPipelineBuilder<>(this); | |
} | |
} | |
static class PipelineInputValue<I> implements PipelineStep<I, I> { | |
I value; | |
public PipelineInputValue(I value) { | |
this.value = value; | |
} | |
@Override | |
public I execute(I input) { | |
// if(input != sentinel) throw NO(); | |
return value; | |
} | |
} | |
static <I> PipelineBuilder<I> from(I input) { | |
return new BasicPipelineBuilder<I, I>().then(new PipelineInputValue<>(input)); | |
} | |
static PipelineStep<String, Integer> addCharsStep() { | |
return (str) -> { | |
int total = 0; | |
for(char c : str.toCharArray()) { | |
total += (int) c; | |
} | |
return total; | |
}; | |
} | |
static PipelineStep<Integer, String> intToHexStep() { | |
return (val) -> { | |
return Integer.toHexString(val); | |
}; | |
} | |
static <T> PipelineStep<T, Void> printStep() { | |
return (obj) -> { | |
System.out.println(obj); | |
return null; | |
}; | |
} | |
public static void main(String[] args) { | |
var pipeline = from("Hello").then(addCharsStep()).then(intToHexStep()).then(printStep()); | |
pipeline.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment