diff --git a/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/functional/BiFunctionExample.java b/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/functional/BiFunctionExample.java new file mode 100644 index 0000000000..8fedce3943 --- /dev/null +++ b/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/functional/BiFunctionExample.java @@ -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 add = Integer::sum; + BiFunction mul = (a, b) -> a * b; + Function toString = Object::toString; + BiFunction pipeline + = add.andThen(a -> mul.apply(a, 2)).andThen(toString); + String result = pipeline.apply(1, 2); + System.out.println(result); + } +} diff --git a/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/functional/FunctionExample.java b/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/functional/FunctionExample.java new file mode 100644 index 0000000000..eb393af0ac --- /dev/null +++ b/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/functional/FunctionExample.java @@ -0,0 +1,17 @@ +package com.baeldung.pipeline.functional; + +import java.util.function.Function; + +public class FunctionExample { + + public static void main(String[] args) { + Function square = s -> s * s; + Function half = s -> s / 2; + Function toString = Object::toString; + Function pipeline = square.andThen(half) + .andThen(toString); + String result = pipeline.apply(5); + System.out.println(result); + } + +} diff --git a/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/immutable/Pipe.java b/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/immutable/Pipe.java new file mode 100644 index 0000000000..6fdb4c71c3 --- /dev/null +++ b/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/immutable/Pipe.java @@ -0,0 +1,5 @@ +package com.baeldung.pipeline.immutable; + +public interface Pipe { + OUT process(IN input); +} diff --git a/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/immutable/Pipeline.java b/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/immutable/Pipeline.java new file mode 100644 index 0000000000..db46a26d7e --- /dev/null +++ b/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/immutable/Pipeline.java @@ -0,0 +1,37 @@ +package com.baeldung.pipeline.immutable; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +public class Pipeline { + + private Collection> pipes; + + private Pipeline(Pipe pipe) { + pipes = Collections.singletonList(pipe); + } + + private Pipeline(Collection> pipes) { + this.pipes = new ArrayList<>(pipes); + } + + public static Pipeline of(Pipe pipe) { + return new Pipeline<>(pipe); + } + + + public Pipeline withNextPipe(Pipe pipe) { + final ArrayList> 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; + } +} diff --git a/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/pipes/Pipe.java b/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/pipes/Pipe.java new file mode 100644 index 0000000000..93f005fdd2 --- /dev/null +++ b/patterns-modules/design-patterns-structural/src/main/java/com/baeldung/pipeline/pipes/Pipe.java @@ -0,0 +1,9 @@ +package com.baeldung.pipeline.pipes; + +public interface Pipe { + OUT process(IN input); + + default Pipe add(Pipe pipe) { + return input -> pipe.process(process(input)); + } +} diff --git a/patterns-modules/design-patterns-structural/src/test/java/com/baeldung/pipeline/PipeUnitTest.java b/patterns-modules/design-patterns-structural/src/test/java/com/baeldung/pipeline/PipeUnitTest.java new file mode 100644 index 0000000000..0e17d87fe2 --- /dev/null +++ b/patterns-modules/design-patterns-structural/src/test/java/com/baeldung/pipeline/PipeUnitTest.java @@ -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 square = s -> s * s; + Pipe half = s -> s / 2; + Pipe toString = Object::toString; + Pipe pipeline = square.add(half).add(toString); + String result = pipeline.process(5); + String expected = "12"; + assertThat(result).isEqualTo(expected); + } +} \ No newline at end of file diff --git a/patterns-modules/design-patterns-structural/src/test/java/com/baeldung/pipeline/PipelineUnitTest.java b/patterns-modules/design-patterns-structural/src/test/java/com/baeldung/pipeline/PipelineUnitTest.java new file mode 100644 index 0000000000..e6ce47f0e8 --- /dev/null +++ b/patterns-modules/design-patterns-structural/src/test/java/com/baeldung/pipeline/PipelineUnitTest.java @@ -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 square = s -> s * s; + Pipe half = s -> s / 2; + Pipe toString = Object::toString; + Pipeline squarePipeline = Pipeline.of(square); + Pipeline squareAndHalfPipeline = squarePipeline.withNextPipe(half); + Pipeline squareHalfAndStringPipeline = squareAndHalfPipeline.withNextPipe(toString); + + String result = squareHalfAndStringPipeline.process(5); + String expected = "12"; + assertThat(result).isEqualTo(expected); + } +} \ No newline at end of file