Last active
May 21, 2017 18:46
-
-
Save damonkelley/a2f7ef9f9793eaf1fe65edab0eb2015b to your computer and use it in GitHub Desktop.
Java Pipeline Class
This file contains hidden or 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
public class Pipeline<T> { | |
private final T value; | |
public Pipeline(T value) { | |
this.value = value; | |
} | |
public static <T> Pipeline<T> with(T value) { | |
return new Pipeline<T>(value); | |
} | |
public<U> Pipeline<U> pipe(Function<? super T, ? extends U> mapper) { | |
return new Pipeline<U>(mapper.apply(value)); | |
} | |
public T get() { | |
return value; | |
} | |
} |
This file contains hidden or 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
public class PipelineTest { | |
@Test public void usage() { | |
String value = Pipeline.with(1) | |
.pipe(e -> e.equals(1) ? "a" : "b") | |
.get(); | |
assertEquals("a", value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment