Skip to content

Instantly share code, notes, and snippets.

@damonkelley
Last active May 21, 2017 18:46
Show Gist options
  • Save damonkelley/a2f7ef9f9793eaf1fe65edab0eb2015b to your computer and use it in GitHub Desktop.
Save damonkelley/a2f7ef9f9793eaf1fe65edab0eb2015b to your computer and use it in GitHub Desktop.
Java Pipeline Class
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;
}
}
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