|
| 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 | +} |
0 commit comments