BAEL-6228-Pipeline-Design-Pattern-in-Java (#13547)
This commit is contained in:
parent
075540af10
commit
463a5d4dc5
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.pipeline.functional;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class BiFunctionExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
BiFunction<Integer, Integer, Integer> add = Integer::sum;
|
||||
BiFunction<Integer, Integer, Integer> mul = (a, b) -> a * b;
|
||||
Function<Integer, String> toString = Object::toString;
|
||||
BiFunction<Integer, Integer, String> pipeline
|
||||
= add.andThen(a -> mul.apply(a, 2)).andThen(toString);
|
||||
String result = pipeline.apply(1, 2);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.pipeline.functional;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class FunctionExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Function<Integer, Integer> square = s -> s * s;
|
||||
Function<Integer, Integer> half = s -> s / 2;
|
||||
Function<Integer, String> toString = Object::toString;
|
||||
Function<Integer, String> pipeline = square.andThen(half)
|
||||
.andThen(toString);
|
||||
String result = pipeline.apply(5);
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.pipeline.immutable;
|
||||
|
||||
public interface Pipe<IN, OUT> {
|
||||
OUT process(IN input);
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.pipeline.immutable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
public class Pipeline<IN, OUT> {
|
||||
|
||||
private Collection<Pipe<?, ?>> pipes;
|
||||
|
||||
private Pipeline(Pipe<IN, OUT> pipe) {
|
||||
pipes = Collections.singletonList(pipe);
|
||||
}
|
||||
|
||||
private Pipeline(Collection<Pipe<?, ?>> pipes) {
|
||||
this.pipes = new ArrayList<>(pipes);
|
||||
}
|
||||
|
||||
public static <IN, OUT> Pipeline<IN, OUT> of(Pipe<IN, OUT> pipe) {
|
||||
return new Pipeline<>(pipe);
|
||||
}
|
||||
|
||||
|
||||
public <NEW_OUT> Pipeline<IN, NEW_OUT> withNextPipe(Pipe<OUT, NEW_OUT> pipe) {
|
||||
final ArrayList<Pipe<?, ?>> newPipes = new ArrayList<>(pipes);
|
||||
newPipes.add(pipe);
|
||||
return new Pipeline<>(newPipes);
|
||||
}
|
||||
|
||||
public OUT process(IN input) {
|
||||
Object output = input;
|
||||
for (final Pipe pipe : pipes) {
|
||||
output = pipe.process(output);
|
||||
}
|
||||
return (OUT) output;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.pipeline.pipes;
|
||||
|
||||
public interface Pipe<IN, OUT> {
|
||||
OUT process(IN input);
|
||||
|
||||
default <NEW_OUT> Pipe<IN, NEW_OUT> add(Pipe <OUT, NEW_OUT> pipe) {
|
||||
return input -> pipe.process(process(input));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.pipeline;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.baeldung.pipeline.pipes.Pipe;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PipeUnitTest {
|
||||
|
||||
@Test
|
||||
void simplePipeTest() {
|
||||
Pipe<Integer, Integer> square = s -> s * s;
|
||||
Pipe<Integer, Integer> half = s -> s / 2;
|
||||
Pipe<Integer, String> toString = Object::toString;
|
||||
Pipe<Integer, String> pipeline = square.add(half).add(toString);
|
||||
String result = pipeline.process(5);
|
||||
String expected = "12";
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.pipeline;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import com.baeldung.pipeline.immutable.Pipe;
|
||||
import com.baeldung.pipeline.immutable.Pipeline;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PipelineUnitTest {
|
||||
|
||||
@Test
|
||||
void simplePipelineTest() {
|
||||
Pipe<Integer, Integer> square = s -> s * s;
|
||||
Pipe<Integer, Integer> half = s -> s / 2;
|
||||
Pipe<Integer, String> toString = Object::toString;
|
||||
Pipeline<Integer, Integer> squarePipeline = Pipeline.of(square);
|
||||
Pipeline<Integer, Integer> squareAndHalfPipeline = squarePipeline.withNextPipe(half);
|
||||
Pipeline<Integer, String> squareHalfAndStringPipeline = squareAndHalfPipeline.withNextPipe(toString);
|
||||
|
||||
String result = squareHalfAndStringPipeline.process(5);
|
||||
String expected = "12";
|
||||
assertThat(result).isEqualTo(expected);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue