Skip to content

Commit f903d7e

Browse files
jjjimenez100iluwatar
authored andcommitted
iluwatar#496 Pipeline pattern (iluwatar#967)
* iluwatar#496 Add pipeline module to parent pom ✨ * iluwatar#496: Add main application class and test for pipeline * iluwatar#496: Checkstyle format and add log messages on pipeline stages 🎨 * iluwatar#496: Fill readme sections of pipeline ✨ * iluwatar#496: Javadocs and checkstyle formatting 🎨 * iluwatar#496: Follow PMD checks and add more explanation as block comment on App.java * iluwatar#496: Apply requested PR changes by iluwatar 🎨
1 parent 84c4b03 commit f903d7e

11 files changed

Lines changed: 464 additions & 0 deletions

File tree

‎pipeline/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
layout: pattern
3+
title: Pipeline
4+
folder: pipeline
5+
permalink: /patterns/pipeline/
6+
categories: Behavioral
7+
tags:
8+
- Java
9+
- Functional
10+
- Difficulty-Intermediate
11+
---
12+
13+
## Intent
14+
Allows processing of data in a series of stages by giving in an initial input and passing the processed output to be used by the next stages.
15+
16+
## Applicability
17+
Use the Pipeline pattern when you want to
18+
19+
* execute individual stages that yields a final value
20+
* add readability to complex sequence of operations by providing a fluent builder as an interface
21+
* improve testability of code since stages will most likely be doing a single thing, complying to the [Single Responsibility Principle (SRP)](https://java-design-patterns.com/principles/#single-responsibility-principle)
22+
23+
## Typical Use Case
24+
25+
* implement stages and execute them in an ordered manner
26+
27+
## Real world examples
28+
29+
* [java.util.Stream](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html)
30+
* [Maven Build Lifecycle](http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html)
31+
* [Functional Java](https://github.com/functionaljava/functionaljava)
32+
33+
## Credits
34+
35+
* [The Pipeline Pattern — for fun and profit](https://medium.com/@aaronweatherall/the-pipeline-pattern-for-fun-and-profit-9b5f43a98130)
36+
* [The Pipeline design pattern (in Java)](https://medium.com/@deepakbapat/the-pipeline-design-pattern-in-java-831d9ce2fe21)
37+
* [Pipelines | Microsoft Docs](https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff963548(v=pandp.10))

‎pipeline/pom.xml‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2014-2016 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
28+
<modelVersion>4.0.0</modelVersion>
29+
<parent>
30+
<groupId>com.iluwatar</groupId>
31+
<artifactId>java-design-patterns</artifactId>
32+
<version>1.22.0-SNAPSHOT</version>
33+
</parent>
34+
<artifactId>pipeline</artifactId>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-engine</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.mockito</groupId>
43+
<artifactId>mockito-core</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
</dependencies>
47+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.pipeline;
24+
25+
/**
26+
* The Pipeline pattern uses ordered stages to process a sequence of input values.
27+
* Each implemented task is represented by a stage of the pipeline. You can think of
28+
* pipelines as similar to assembly lines in a factory, where each item in the assembly
29+
* line is constructed in stages. The partially assembled item is passed from one assembly
30+
* stage to another. The outputs of the assembly line occur in the same order as that of the
31+
* inputs.
32+
*
33+
* Classes used in this example are suffixed with "Handlers", and synonymously refers to the
34+
* "stage".
35+
*/
36+
public class App {
37+
/**
38+
* Specify the initial input type for the first stage handler and the expected output type
39+
* of the last stage handler as type parameters for Pipeline. Use the fluent builder by
40+
* calling addHandler to add more stage handlers on the pipeline.
41+
*/
42+
public static void main(String[] args) {
43+
/*
44+
Suppose we wanted to pass through a String to a series of filtering stages and convert it
45+
as a char array on the last stage.
46+
47+
- Stage handler 1 (pipe): Removing the alphabets, accepts a String input and returns the
48+
processed String output. This will be used by the next handler as its input.
49+
50+
- Stage handler 2 (pipe): Removing the digits, accepts a String input and returns the
51+
processed String output. This shall also be used by the last handler we have.
52+
53+
- Stage handler 3 (pipe): Converting the String input to a char array handler. We would
54+
be returning a different type in here since that is what's specified by the requirement.
55+
This means that at any stages along the pipeline, the handler can return any type of data
56+
as long as it fulfills the requirements for the next handler's input.
57+
58+
Suppose we wanted to add another handler after ConvertToCharArrayHandler. That handler
59+
then is expected to receive an input of char[] array since that is the type being returned
60+
by the previous handler, ConvertToCharArrayHandler.
61+
*/
62+
new Pipeline<>(new RemoveAlphabetsHandler())
63+
.addHandler(new RemoveDigitsHandler())
64+
.addHandler(new ConvertToCharArrayHandler());
65+
}
66+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
* <p>
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* <p>
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
* <p>
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.pipeline;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import java.util.Arrays;
29+
30+
/**
31+
* Stage handler that converts an input String to its char[] array counterpart.
32+
*/
33+
class ConvertToCharArrayHandler implements Handler<String, char[]> {
34+
35+
private final Logger logger = LoggerFactory.getLogger(ConvertToCharArrayHandler.class);
36+
37+
@Override
38+
public char[] process(String input) {
39+
char[] characters = input.toCharArray();
40+
logger.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s",
41+
ConvertToCharArrayHandler.class, input, String.class, Arrays.toString(characters), Character[].class));
42+
43+
return characters;
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.pipeline;
24+
25+
/**
26+
* Forms a contract to all stage handlers to accept a certain type of input and return a processed output.
27+
* @param <I> the input type of the handler
28+
* @param <O> the processed output type of the handler
29+
*/
30+
interface Handler<I, O> {
31+
O process(I input);
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.pipeline;
24+
25+
/**
26+
* Main Pipeline class that initially sets the current handler. Processed output
27+
* of the initial handler is then passed as the input to the next stage handlers.
28+
* @param <I> the type of the input for the first stage handler
29+
* @param <O> the final stage handler's output type
30+
*/
31+
class Pipeline<I, O> {
32+
33+
private final Handler<I, O> currentHandler;
34+
35+
Pipeline(Handler<I, O> currentHandler) {
36+
this.currentHandler = currentHandler;
37+
}
38+
39+
<K> Pipeline<I, K> addHandler(Handler<O, K> newHandler) {
40+
return new Pipeline<>(input -> newHandler.process(currentHandler.process(input)));
41+
}
42+
43+
O execute(I input) {
44+
return currentHandler.process(input);
45+
}
46+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.pipeline;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
/**
29+
* Stage handler that returns a new instance of String without the alphabet characters of the input string.
30+
*/
31+
class RemoveAlphabetsHandler implements Handler<String, String> {
32+
33+
private final Logger logger = LoggerFactory.getLogger(RemoveAlphabetsHandler.class);
34+
35+
@Override
36+
public String process(String input) {
37+
StringBuilder inputWithoutAlphabets = new StringBuilder();
38+
39+
for (int index = 0; index < input.length(); index++) {
40+
char currentCharacter = input.charAt(index);
41+
if (Character.isAlphabetic(currentCharacter)) {
42+
continue;
43+
}
44+
45+
inputWithoutAlphabets.append(currentCharacter);
46+
}
47+
48+
String inputWithoutAlphabetsStr = inputWithoutAlphabets.toString();
49+
logger.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s",
50+
RemoveAlphabetsHandler.class, input, String.class, inputWithoutAlphabetsStr, String.class));
51+
52+
return inputWithoutAlphabetsStr;
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.pipeline;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
/**
29+
* Stage handler that returns a new instance of String without the digit characters of the input string.
30+
*/
31+
class RemoveDigitsHandler implements Handler<String, String> {
32+
33+
private final Logger logger = LoggerFactory.getLogger(RemoveDigitsHandler.class);
34+
35+
@Override
36+
public String process(String input) {
37+
StringBuilder inputWithoutDigits = new StringBuilder();
38+
39+
for (int index = 0; index < input.length(); index++) {
40+
char currentCharacter = input.charAt(index);
41+
if (Character.isDigit(currentCharacter)) {
42+
continue;
43+
}
44+
45+
inputWithoutDigits.append(currentCharacter);
46+
}
47+
48+
String inputWithoutDigitsStr = inputWithoutDigits.toString();
49+
logger.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s",
50+
RemoveDigitsHandler.class, input, String.class, inputWithoutDigitsStr, String.class));
51+
52+
return inputWithoutDigitsStr;
53+
}
54+
}

0 commit comments

Comments
 (0)