From 0a13721299a57b35605568b4cf50347e1b67448e Mon Sep 17 00:00:00 2001 From: jjjimenez100 Date: Sun, 6 Oct 2019 23:12:19 +0800 Subject: [PATCH 01/19] #496 Add pipeline module to parent pom :sparkles: --- pipeline/README.md | 0 pipeline/pom.xml | 47 +++++++++++++++++++ .../ConvertToCharArrayHandler.java | 8 ++++ .../java/com.iluwatar.pipeline/Handler.java | 5 ++ .../java/com.iluwatar.pipeline/Pipeline.java | 18 +++++++ .../RemoveAlphabetsHandler.java | 19 ++++++++ .../RemoveDigitsHandler.java | 19 ++++++++ .../com.iluwatar.pipeline/PipelineTest.java | 18 +++++++ pom.xml | 1 + 9 files changed, 135 insertions(+) create mode 100644 pipeline/README.md create mode 100644 pipeline/pom.xml create mode 100644 pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java create mode 100644 pipeline/src/main/java/com.iluwatar.pipeline/Handler.java create mode 100644 pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java create mode 100644 pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java create mode 100644 pipeline/src/main/java/com.iluwatar.pipeline/RemoveDigitsHandler.java create mode 100644 pipeline/src/test/java/com.iluwatar.pipeline/PipelineTest.java diff --git a/pipeline/README.md b/pipeline/README.md new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/pipeline/pom.xml b/pipeline/pom.xml new file mode 100644 index 000000000000..7ad60d5620e4 --- /dev/null +++ b/pipeline/pom.xml @@ -0,0 +1,47 @@ + + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.22.0-SNAPSHOT + + pipeline + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.mockito + mockito-core + test + + + diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java b/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java new file mode 100644 index 000000000000..ce878e1298e0 --- /dev/null +++ b/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java @@ -0,0 +1,8 @@ +package com.iluwatar.pipeline; + +class ConvertToCharArrayHandler implements Handler { + @Override + public char[] process(String input) { + return input.toCharArray(); + } +} diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java b/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java new file mode 100644 index 000000000000..e78acd9a8670 --- /dev/null +++ b/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java @@ -0,0 +1,5 @@ +package com.iluwatar.pipeline; + +interface Handler { + O process(I input); +} \ No newline at end of file diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java b/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java new file mode 100644 index 000000000000..4483bf68fbf4 --- /dev/null +++ b/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java @@ -0,0 +1,18 @@ +package com.iluwatar.pipeline; + +class Pipeline { + + private final Handler currentHandler; + + Pipeline(Handler currentHandler) { + this.currentHandler = currentHandler; + } + + Pipeline addHandler(Handler newHandler){ + return new Pipeline<>(input -> newHandler.process(currentHandler.process(input))); + } + + O execute(I input) { + return currentHandler.process(input); + } +} \ No newline at end of file diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java new file mode 100644 index 000000000000..6d7d4da3e394 --- /dev/null +++ b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java @@ -0,0 +1,19 @@ +package com.iluwatar.pipeline; + +class RemoveAlphabetsHandler implements Handler { + @Override + public String process(String input) { + StringBuilder inputWithoutAlphabets = new StringBuilder(); + + for(int index=0; index { + @Override + public String process(String input) { + StringBuilder inputWithoutDigits = new StringBuilder(); + + for(int index=0; index filters = new Pipeline<>(new RemoveAlphabetsHandler()) + .addHandler(new RemoveDigitsHandler()) + .addHandler(new ConvertToCharArrayHandler()); + + assertArrayEquals( + new char[]{'#', '!', '(', '&', '%', '#', '!'}, + filters.execute("#H!E(L&L0O%THE3R#34E!") + ); + } +} diff --git a/pom.xml b/pom.xml index f1a586eb8b34..325a2ff00025 100644 --- a/pom.xml +++ b/pom.xml @@ -94,6 +94,7 @@ property intercepting-filter producer-consumer + pipeline poison-pill reader-writer-lock lazy-loading From 7ec12abb1c85e82092ef67ad9ce38b3c10ca87f0 Mon Sep 17 00:00:00 2001 From: jjjimenez100 Date: Sun, 6 Oct 2019 23:47:30 +0800 Subject: [PATCH 02/19] #496: Add main application class and test for pipeline --- .../main/java/com.iluwatar.pipeline/App.java | 9 +++++++ .../java/com.iluwatar.pipeline/AppTest.java | 15 +++++++++++ .../com.iluwatar.pipeline/PipelineTest.java | 25 +++++++++++-------- 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 pipeline/src/main/java/com.iluwatar.pipeline/App.java create mode 100644 pipeline/src/test/java/com.iluwatar.pipeline/AppTest.java diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/App.java b/pipeline/src/main/java/com.iluwatar.pipeline/App.java new file mode 100644 index 000000000000..4e73135ff1d9 --- /dev/null +++ b/pipeline/src/main/java/com.iluwatar.pipeline/App.java @@ -0,0 +1,9 @@ +package com.iluwatar.pipeline; + +public class App { + public static void main(String[] args) { + Pipeline filters = new Pipeline<>(new RemoveAlphabetsHandler()) + .addHandler(new RemoveDigitsHandler()) + .addHandler(new ConvertToCharArrayHandler()); + } +} diff --git a/pipeline/src/test/java/com.iluwatar.pipeline/AppTest.java b/pipeline/src/test/java/com.iluwatar.pipeline/AppTest.java new file mode 100644 index 000000000000..1623088c0133 --- /dev/null +++ b/pipeline/src/test/java/com.iluwatar.pipeline/AppTest.java @@ -0,0 +1,15 @@ +package com.iluwatar.pipeline; + +import org.junit.jupiter.api.Test; + +/** + * Application Test + */ +public class AppTest { + + @Test + public void test() { + String[] args = {}; + App.main(args); + } +} diff --git a/pipeline/src/test/java/com.iluwatar.pipeline/PipelineTest.java b/pipeline/src/test/java/com.iluwatar.pipeline/PipelineTest.java index 7267fed320e1..36a3320e88ec 100644 --- a/pipeline/src/test/java/com.iluwatar.pipeline/PipelineTest.java +++ b/pipeline/src/test/java/com.iluwatar.pipeline/PipelineTest.java @@ -1,18 +1,23 @@ package com.iluwatar.pipeline; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertArrayEquals; +/** + * Test for {@link Pipeline} + */ public class PipelineTest { - @Test - public void testStringFiltersPipeline() { - Pipeline filters = new Pipeline<>(new RemoveAlphabetsHandler()) - .addHandler(new RemoveDigitsHandler()) - .addHandler(new ConvertToCharArrayHandler()); - assertArrayEquals( - new char[]{'#', '!', '(', '&', '%', '#', '!'}, - filters.execute("#H!E(L&L0O%THE3R#34E!") - ); - } + @Test + public void testAddHandlersToPipeline() { + Pipeline filters = new Pipeline<>(new RemoveAlphabetsHandler()) + .addHandler(new RemoveDigitsHandler()) + .addHandler(new ConvertToCharArrayHandler()); + + assertArrayEquals( + new char[] {'#', '!', '(', '&', '%', '#', '!'}, + filters.execute("#H!E(L&L0O%THE3R#34E!") + ); + } } From 5040db11e9dd47e02676a2f2915a29aaf3a3947b Mon Sep 17 00:00:00 2001 From: jjjimenez100 Date: Sun, 6 Oct 2019 23:48:48 +0800 Subject: [PATCH 03/19] #496: Checkstyle format and add log messages on pipeline stages :art: --- .../ConvertToCharArrayHandler.java | 19 +++++++++--- .../java/com.iluwatar.pipeline/Handler.java | 2 +- .../java/com.iluwatar.pipeline/Pipeline.java | 20 ++++++------ .../RemoveAlphabetsHandler.java | 31 ++++++++++++------- .../RemoveDigitsHandler.java | 31 ++++++++++++------- 5 files changed, 66 insertions(+), 37 deletions(-) diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java b/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java index ce878e1298e0..633d4b82e82f 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java @@ -1,8 +1,19 @@ package com.iluwatar.pipeline; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; + class ConvertToCharArrayHandler implements Handler { - @Override - public char[] process(String input) { - return input.toCharArray(); - } + + private final Logger LOGGER = LoggerFactory.getLogger(ConvertToCharArrayHandler.class); + + @Override + public char[] process(String input) { + char[] characters = input.toCharArray(); + LOGGER.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", ConvertToCharArrayHandler.class, input, String.class, Arrays.toString(characters), Character[].class)); + + return characters; + } } diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java b/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java index e78acd9a8670..7214a597f685 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java @@ -1,5 +1,5 @@ package com.iluwatar.pipeline; interface Handler { - O process(I input); + O process(I input); } \ No newline at end of file diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java b/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java index 4483bf68fbf4..b614b41cb724 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java @@ -2,17 +2,17 @@ class Pipeline { - private final Handler currentHandler; + private final Handler currentHandler; - Pipeline(Handler currentHandler) { - this.currentHandler = currentHandler; - } + Pipeline(Handler currentHandler) { + this.currentHandler = currentHandler; + } - Pipeline addHandler(Handler newHandler){ - return new Pipeline<>(input -> newHandler.process(currentHandler.process(input))); - } + Pipeline addHandler(Handler newHandler) { + return new Pipeline<>(input -> newHandler.process(currentHandler.process(input))); + } - O execute(I input) { - return currentHandler.process(input); - } + O execute(I input) { + return currentHandler.process(input); + } } \ No newline at end of file diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java index 6d7d4da3e394..07c67b310603 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java @@ -1,19 +1,28 @@ package com.iluwatar.pipeline; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + class RemoveAlphabetsHandler implements Handler { - @Override - public String process(String input) { - StringBuilder inputWithoutAlphabets = new StringBuilder(); - for(int index=0; index { - @Override - public String process(String input) { - StringBuilder inputWithoutDigits = new StringBuilder(); - for(int index=0; index Date: Mon, 7 Oct 2019 01:06:14 +0800 Subject: [PATCH 04/19] #496: Fill readme sections of pipeline :sparkles: --- pipeline/README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/pipeline/README.md b/pipeline/README.md index e69de29bb2d1..efb05a42758c 100644 --- a/pipeline/README.md +++ b/pipeline/README.md @@ -0,0 +1,40 @@ +--- +layout: pattern +title: Pipeline +folder: pipeline +permalink: /patterns/pipeline/ +categories: Behavioral +tags: + - Java + - Functional + - Difficulty-Intermediate +--- + +## Also known as +None + +## Intent +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. + +## Applicability +Use the Pipeline pattern when you want to + +* execute individual stages that yields a final value +* add readability to complex sequence of operations by providing a fluent builder as an interface +* improve testability of code since stages will most likely be doing a single thing, complying to the Single Responsibility Principle (SRP) + +## Typical Use Case + +* implement stages and execute them in an ordered manner + +## Real world examples + +* [java.util.Stream](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html) +* [Maven Build Lifecycle](http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html) +* [Functional Java](https://github.com/functionaljava/functionaljava) + +## Credits + +* [The Pipeline Pattern — for fun and profit](https://medium.com/@aaronweatherall/the-pipeline-pattern-for-fun-and-profit-9b5f43a98130) +* [The Pipeline design pattern (in Java)](https://medium.com/@deepakbapat/the-pipeline-design-pattern-in-java-831d9ce2fe21) +* [Pipelines | Microsoft Docs](https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff963548(v=pandp.10)) \ No newline at end of file From a038a4fe1ab377facd6bc13f4da647860cbf7746 Mon Sep 17 00:00:00 2001 From: jjjimenez100 Date: Mon, 7 Oct 2019 01:06:54 +0800 Subject: [PATCH 05/19] #496: Javadocs and checkstyle formatting :art: --- .../main/java/com.iluwatar.pipeline/App.java | 38 +++++++++++++++++++ .../ConvertToCharArrayHandler.java | 30 ++++++++++++++- .../java/com.iluwatar.pipeline/Handler.java | 27 +++++++++++++ .../java/com.iluwatar.pipeline/Pipeline.java | 28 ++++++++++++++ .../RemoveAlphabetsHandler.java | 30 ++++++++++++++- .../RemoveDigitsHandler.java | 30 ++++++++++++++- .../java/com.iluwatar.pipeline/AppTest.java | 22 +++++++++++ .../com.iluwatar.pipeline/PipelineTest.java | 22 +++++++++++ 8 files changed, 221 insertions(+), 6 deletions(-) diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/App.java b/pipeline/src/main/java/com.iluwatar.pipeline/App.java index 4e73135ff1d9..0fce06309a7b 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/App.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/App.java @@ -1,6 +1,44 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.pipeline; +/** + * The Pipeline pattern uses ordered stages to process a sequence of input values. + * Each implemented task is represented by a stage of the pipeline. You can think of + * pipelines as similar to assembly lines in a factory, where each item in the assembly + * line is constructed in stages. The partially assembled item is passed from one assembly + * stage to another. The outputs of the assembly line occur in the same order as that of the + * inputs. + * + * Classes used in this example are suffixed with "Handlers", and synonymously refers to the + * "stage". + */ public class App { + /** + * Specify the initial input type for the first stage handler and the expected output type + * of the last stage handler as type parameters for Pipeline. Use the fluent builder by + * calling addHandler to add more stage handlers on the pipeline. + */ public static void main(String[] args) { Pipeline filters = new Pipeline<>(new RemoveAlphabetsHandler()) .addHandler(new RemoveDigitsHandler()) diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java b/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java index 633d4b82e82f..b69241914980 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java @@ -1,3 +1,25 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.pipeline; import org.slf4j.Logger; @@ -5,14 +27,18 @@ import java.util.Arrays; +/** + * Stage handler that converts an input String to its char[] array counterpart. + */ class ConvertToCharArrayHandler implements Handler { - private final Logger LOGGER = LoggerFactory.getLogger(ConvertToCharArrayHandler.class); + private final Logger logger = LoggerFactory.getLogger(ConvertToCharArrayHandler.class); @Override public char[] process(String input) { char[] characters = input.toCharArray(); - LOGGER.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", ConvertToCharArrayHandler.class, input, String.class, Arrays.toString(characters), Character[].class)); + logger.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", + ConvertToCharArrayHandler.class, input, String.class, Arrays.toString(characters), Character[].class)); return characters; } diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java b/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java index 7214a597f685..7d2bc0db7f59 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/Handler.java @@ -1,5 +1,32 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.pipeline; +/** + * Forms a contract to all stage handlers to accept a certain type of input and return a processed output. + * @param the input type of the handler + * @param the processed output type of the handler + */ interface Handler { O process(I input); } \ No newline at end of file diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java b/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java index b614b41cb724..8e231bbc4d09 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/Pipeline.java @@ -1,5 +1,33 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.pipeline; +/** + * Main Pipeline class that initially sets the current handler. Processed output + * of the initial handler is then passed as the input to the next stage handlers. + * @param the type of the input for the first stage handler + * @param the final stage handler's output type + */ class Pipeline { private final Handler currentHandler; diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java index 07c67b310603..2085052742fa 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java @@ -1,11 +1,36 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.pipeline; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Stage handler that returns a new instance of String without the alphabet characters of the input string. + */ class RemoveAlphabetsHandler implements Handler { - private final Logger LOGGER = LoggerFactory.getLogger(RemoveAlphabetsHandler.class); + private final Logger logger = LoggerFactory.getLogger(RemoveAlphabetsHandler.class); @Override public String process(String input) { @@ -21,7 +46,8 @@ public String process(String input) { } String inputWithoutAlphabetsStr = inputWithoutAlphabets.toString(); - LOGGER.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", RemoveAlphabetsHandler.class, input, String.class, inputWithoutAlphabetsStr, String.class)); + logger.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", + RemoveAlphabetsHandler.class, input, String.class, inputWithoutAlphabetsStr, String.class)); return inputWithoutAlphabetsStr; } diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveDigitsHandler.java b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveDigitsHandler.java index 217bdbc0dfa4..e0c0aa6a87b2 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveDigitsHandler.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveDigitsHandler.java @@ -1,11 +1,36 @@ +/** + * The MIT License + * Copyright (c) 2014-2016 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.pipeline; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Stage handler that returns a new instance of String without the digit characters of the input string. + */ class RemoveDigitsHandler implements Handler { - private final Logger LOGGER = LoggerFactory.getLogger(RemoveDigitsHandler.class); + private final Logger logger = LoggerFactory.getLogger(RemoveDigitsHandler.class); @Override public String process(String input) { @@ -21,7 +46,8 @@ public String process(String input) { } String inputWithoutDigitsStr = inputWithoutDigits.toString(); - LOGGER.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", RemoveDigitsHandler.class, input, String.class, inputWithoutDigitsStr, String.class)); + logger.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", + RemoveDigitsHandler.class, input, String.class, inputWithoutDigitsStr, String.class)); return inputWithoutDigitsStr; } diff --git a/pipeline/src/test/java/com.iluwatar.pipeline/AppTest.java b/pipeline/src/test/java/com.iluwatar.pipeline/AppTest.java index 1623088c0133..79524575c13c 100644 --- a/pipeline/src/test/java/com.iluwatar.pipeline/AppTest.java +++ b/pipeline/src/test/java/com.iluwatar.pipeline/AppTest.java @@ -1,3 +1,25 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.pipeline; import org.junit.jupiter.api.Test; diff --git a/pipeline/src/test/java/com.iluwatar.pipeline/PipelineTest.java b/pipeline/src/test/java/com.iluwatar.pipeline/PipelineTest.java index 36a3320e88ec..1a2676e25d05 100644 --- a/pipeline/src/test/java/com.iluwatar.pipeline/PipelineTest.java +++ b/pipeline/src/test/java/com.iluwatar.pipeline/PipelineTest.java @@ -1,3 +1,25 @@ +/** + * The MIT License + * Copyright (c) 2014 Ilkka Seppälä + *

+ * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + *

+ * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + *

+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.pipeline; import org.junit.jupiter.api.Test; From ba3ce8314cd9d694719e0e9e328f16c16a48fb98 Mon Sep 17 00:00:00 2001 From: Joshua Date: Tue, 8 Oct 2019 06:08:46 +0800 Subject: [PATCH 06/19] #496: Follow PMD checks and add more explanation as block comment on App.java --- .../main/java/com.iluwatar.pipeline/App.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/App.java b/pipeline/src/main/java/com.iluwatar.pipeline/App.java index 0fce06309a7b..7efa2ecca003 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/App.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/App.java @@ -40,7 +40,26 @@ public class App { * calling addHandler to add more stage handlers on the pipeline. */ public static void main(String[] args) { - Pipeline filters = new Pipeline<>(new RemoveAlphabetsHandler()) + /* + Suppose we wanted to pass through a String to a series of filtering stages and convert it + as a char array on the last stage. + + - Stage handler 1 (pipe): Removing the alphabets, accepts a String input and returns the + processed String output. This will be used by the next handler as its input. + + - Stage handler 2 (pipe): Removing the digits, accepts a String input and returns the + processed String output. This shall also be used by the last handler we have. + + - Stage handler 3 (pipe): Converting the String input to a char array handler. We would + be returning a different type in here since that is what's specified by the requirement. + This means that at any stages along the pipeline, the handler can return any type of data + as long as it fulfills the requirements for the next handler's input. + + Suppose we wanted to add another handler after ConvertToCharArrayHandler. That handler + then is expected to receive an input of char[] array since that is the type being returned + by the previous handler, ConvertToCharArrayHandler. + */ + new Pipeline<>(new RemoveAlphabetsHandler()) .addHandler(new RemoveDigitsHandler()) .addHandler(new ConvertToCharArrayHandler()); } From e79598376cd48b6edf861d34dcddbc8d1b722e69 Mon Sep 17 00:00:00 2001 From: Joshua Date: Tue, 8 Oct 2019 13:51:40 +0800 Subject: [PATCH 07/19] #496: Apply requested PR changes by iluwatar :art: --- pipeline/README.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pipeline/README.md b/pipeline/README.md index efb05a42758c..e990affd62c7 100644 --- a/pipeline/README.md +++ b/pipeline/README.md @@ -10,9 +10,6 @@ tags: - Difficulty-Intermediate --- -## Also known as -None - ## Intent 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. @@ -21,7 +18,7 @@ Use the Pipeline pattern when you want to * execute individual stages that yields a final value * add readability to complex sequence of operations by providing a fluent builder as an interface -* improve testability of code since stages will most likely be doing a single thing, complying to the Single Responsibility Principle (SRP) +* 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) ## Typical Use Case From f27eb1fd6a19ef609b8edaaa6fd8ef9573725f58 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 12 Oct 2019 11:59:15 +0800 Subject: [PATCH 08/19] #970: Replace log4j usage on commander pattern to Slf4j API :art: --- commander/pom.xml | 5 ----- .../src/main/java/com/iluwatar/commander/Commander.java | 5 +++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/commander/pom.xml b/commander/pom.xml index 4db6b12e7e06..fbe45337707e 100644 --- a/commander/pom.xml +++ b/commander/pom.xml @@ -36,10 +36,5 @@ junit-jupiter-engine test - - log4j - log4j - 1.2.17 - diff --git a/commander/src/main/java/com/iluwatar/commander/Commander.java b/commander/src/main/java/com/iluwatar/commander/Commander.java index 8729ea75aeef..57914fe9bff5 100644 --- a/commander/src/main/java/com/iluwatar/commander/Commander.java +++ b/commander/src/main/java/com/iluwatar/commander/Commander.java @@ -23,7 +23,6 @@ package com.iluwatar.commander; import java.util.ArrayList; -import org.apache.log4j.Logger; import com.iluwatar.commander.employeehandle.EmployeeHandle; import com.iluwatar.commander.exceptions.DatabaseUnavailableException; import com.iluwatar.commander.exceptions.ItemUnavailableException; @@ -37,6 +36,8 @@ import com.iluwatar.commander.queue.QueueTask; import com.iluwatar.commander.queue.QueueTask.TaskType; import com.iluwatar.commander.shippingservice.ShippingService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** *

Commander pattern is used to handle all issues that can come up while making a @@ -86,7 +87,7 @@ public class Commander { private final long messageTime; private final long employeeTime; private boolean finalSiteMsgShown; - static final Logger LOG = Logger.getLogger(Commander.class); + static final Logger LOG = LoggerFactory.getLogger(Commander.class); //we could also have another db where it stores all orders Commander(EmployeeHandle empDb, PaymentService pService, ShippingService sService, From e7efa2dbbae4da0416837c475ff138f012cfa5b4 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 12 Oct 2019 11:59:47 +0800 Subject: [PATCH 09/19] #970: Replace log4j usage on dao pattern to Slf4j API :art: --- dao/pom.xml | 49 ------------------- dao/src/main/java/com/iluwatar/dao/App.java | 9 ++-- .../java/com/iluwatar/dao/DbCustomerDao.java | 7 +-- 3 files changed, 9 insertions(+), 56 deletions(-) diff --git a/dao/pom.xml b/dao/pom.xml index b8453f6a574d..5d6bc94546fa 100644 --- a/dao/pom.xml +++ b/dao/pom.xml @@ -40,10 +40,6 @@ junit-jupiter-engine test - - log4j - log4j - com.h2database h2 @@ -53,49 +49,4 @@ mockito-core - - - - - - - - src/main/resources - - - src/main/resources - - log4j.xml - - .. - - - - - - - - org.apache.maven.plugins - maven-jar-plugin - 2.6 - - - log4j.xml - - - - true - - - - - - - diff --git a/dao/src/main/java/com/iluwatar/dao/App.java b/dao/src/main/java/com/iluwatar/dao/App.java index 5824d48cfea6..08d0a42bda3d 100644 --- a/dao/src/main/java/com/iluwatar/dao/App.java +++ b/dao/src/main/java/com/iluwatar/dao/App.java @@ -31,8 +31,9 @@ import javax.sql.DataSource; -import org.apache.log4j.Logger; import org.h2.jdbcx.JdbcDataSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Data Access Object (DAO) is an object that provides an abstract interface to some type of @@ -50,7 +51,7 @@ */ public class App { private static final String DB_URL = "jdbc:h2:~/dao"; - private static Logger log = Logger.getLogger(App.class); + private static Logger log = LoggerFactory.getLogger(App.class); private static final String ALL_CUSTOMERS = "customerDao.getAllCustomers(): "; /** @@ -94,7 +95,7 @@ private static void performOperationsUsing(final CustomerDao customerDao) throws addCustomers(customerDao); log.info(ALL_CUSTOMERS); try (Stream customerStream = customerDao.getAll()) { - customerStream.forEach((customer) -> log.info(customer)); + customerStream.forEach((customer) -> log.info(customer.toString())); } log.info("customerDao.getCustomerById(2): " + customerDao.getById(2)); final Customer customer = new Customer(4, "Dan", "Danson"); @@ -105,7 +106,7 @@ private static void performOperationsUsing(final CustomerDao customerDao) throws customerDao.update(customer); log.info(ALL_CUSTOMERS); try (Stream customerStream = customerDao.getAll()) { - customerStream.forEach((cust) -> log.info(cust)); + customerStream.forEach((cust) -> log.info(cust.toString())); } customerDao.delete(customer); log.info(ALL_CUSTOMERS + customerDao.getAll()); diff --git a/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java b/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java index 9d183e91284e..d8640dcb911d 100644 --- a/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java +++ b/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java @@ -22,6 +22,9 @@ */ package com.iluwatar.dao; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -35,15 +38,13 @@ import javax.sql.DataSource; -import org.apache.log4j.Logger; - /** * An implementation of {@link CustomerDao} that persists customers in RDBMS. * */ public class DbCustomerDao implements CustomerDao { - private static final Logger LOGGER = Logger.getLogger(DbCustomerDao.class); + private static final Logger LOGGER = LoggerFactory.getLogger(DbCustomerDao.class); private final DataSource dataSource; From 5f2c347acd38d5a45c136a799a0b213cf6e7b30d Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 12 Oct 2019 12:00:26 +0800 Subject: [PATCH 10/19] #970: Replace log4j usage on data mapper pattern to Slf4j API :art: --- data-mapper/pom.xml | 4 ---- data-mapper/src/main/java/com/iluwatar/datamapper/App.java | 7 ++++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/data-mapper/pom.xml b/data-mapper/pom.xml index cf53fdf65814..7da70f8fd243 100644 --- a/data-mapper/pom.xml +++ b/data-mapper/pom.xml @@ -37,9 +37,5 @@ junit-jupiter-engine test - - log4j - log4j - diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/App.java b/data-mapper/src/main/java/com/iluwatar/datamapper/App.java index 3504ebbe1c94..6b1d9925333f 100644 --- a/data-mapper/src/main/java/com/iluwatar/datamapper/App.java +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/App.java @@ -22,9 +22,10 @@ */ package com.iluwatar.datamapper; -import java.util.Optional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import org.apache.log4j.Logger; +import java.util.Optional; /** * The Data Mapper (DM) is a layer of software that separates the in-memory objects from the @@ -39,7 +40,7 @@ */ public final class App { - private static Logger log = Logger.getLogger(App.class); + private static Logger log = LoggerFactory.getLogger(App.class); private static final String STUDENT_STRING = "App.main(), student : "; From ddc2ccef7e62ab6a08c192bf6616cd981acfa95b Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 12 Oct 2019 12:01:05 +0800 Subject: [PATCH 11/19] #970: Remove log4j dependency on data transfer object pom :fire: --- data-transfer-object/pom.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/data-transfer-object/pom.xml b/data-transfer-object/pom.xml index 20c8a52f6c0a..ebdb15afa3dd 100644 --- a/data-transfer-object/pom.xml +++ b/data-transfer-object/pom.xml @@ -37,9 +37,5 @@ junit-jupiter-engine test - - log4j - log4j - From 5a948fbc0a1b5e10a5e07d1261b7f7b7978d76e7 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 12 Oct 2019 12:01:43 +0800 Subject: [PATCH 12/19] #970: Replace log4j usage on module pattern to Slf4j API :art: --- module/pom.xml | 4 ---- .../main/java/com/iluwatar/module/ConsoleLoggerModule.java | 7 ++++--- .../main/java/com/iluwatar/module/FileLoggerModule.java | 7 ++++--- .../java/com/iluwatar/module/FileLoggerModuleTest.java | 5 +++-- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/module/pom.xml b/module/pom.xml index 920efc01c9da..0a60960b53c6 100644 --- a/module/pom.xml +++ b/module/pom.xml @@ -37,9 +37,5 @@ junit-jupiter-engine test - - log4j - log4j - diff --git a/module/src/main/java/com/iluwatar/module/ConsoleLoggerModule.java b/module/src/main/java/com/iluwatar/module/ConsoleLoggerModule.java index 6ab358777f6e..12eaae1d9be4 100644 --- a/module/src/main/java/com/iluwatar/module/ConsoleLoggerModule.java +++ b/module/src/main/java/com/iluwatar/module/ConsoleLoggerModule.java @@ -22,9 +22,10 @@ */ package com.iluwatar.module; -import java.io.PrintStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import org.apache.log4j.Logger; +import java.io.PrintStream; /** * The ConsoleLoggerModule is responsible for showing logs on System Console @@ -34,7 +35,7 @@ */ public final class ConsoleLoggerModule { - private static final Logger LOGGER = Logger.getLogger(ConsoleLoggerModule.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleLoggerModule.class); private static ConsoleLoggerModule singleton = null; diff --git a/module/src/main/java/com/iluwatar/module/FileLoggerModule.java b/module/src/main/java/com/iluwatar/module/FileLoggerModule.java index 1ec85ea47547..65f954b6cccd 100644 --- a/module/src/main/java/com/iluwatar/module/FileLoggerModule.java +++ b/module/src/main/java/com/iluwatar/module/FileLoggerModule.java @@ -22,12 +22,13 @@ */ package com.iluwatar.module; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; -import org.apache.log4j.Logger; - /** * The FileLoggerModule is responsible for showing logs on File System *

@@ -36,7 +37,7 @@ */ public final class FileLoggerModule { - private static final Logger LOGGER = Logger.getLogger(FileLoggerModule.class); + private static final Logger LOGGER = LoggerFactory.getLogger(FileLoggerModule.class); private static FileLoggerModule singleton = null; diff --git a/module/src/test/java/com/iluwatar/module/FileLoggerModuleTest.java b/module/src/test/java/com/iluwatar/module/FileLoggerModuleTest.java index 5f7616f02321..30f0baae5703 100644 --- a/module/src/test/java/com/iluwatar/module/FileLoggerModuleTest.java +++ b/module/src/test/java/com/iluwatar/module/FileLoggerModuleTest.java @@ -22,8 +22,9 @@ */ package com.iluwatar.module; -import org.apache.log4j.Logger; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.FileNotFoundException; @@ -45,7 +46,7 @@ */ public final class FileLoggerModuleTest { - private static final Logger LOGGER = Logger.getLogger(FileLoggerModuleTest.class); + private static final Logger LOGGER = LoggerFactory.getLogger(FileLoggerModuleTest.class); private static final String OUTPUT_FILE = "output.txt"; private static final String ERROR_FILE = "error.txt"; From 5766dfe306c16b52e2a27dae10adb5ba8a7a8cad Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 12 Oct 2019 12:02:43 +0800 Subject: [PATCH 13/19] #970: Replace log4j usage on serverless pattern to Slf4j API :art: This also removes the aws log4j dependency --- serverless/pom.xml | 5 ----- .../serverless/baas/api/FindPersonApiHandler.java | 11 ++++++++--- .../serverless/baas/api/SavePersonApiHandler.java | 5 +++-- .../serverless/faas/api/LambdaInfoApiHandler.java | 7 +++---- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/serverless/pom.xml b/serverless/pom.xml index 9e142cb3aa30..292df78c55f1 100644 --- a/serverless/pom.xml +++ b/serverless/pom.xml @@ -60,11 +60,6 @@ aws-lambda-java-events ${aws-lambda-java-events.version} - - com.amazonaws - aws-lambda-java-log4j - ${aws-lambda-log4j.version} - com.fasterxml.jackson.core jackson-core diff --git a/serverless/src/main/java/com/iluwatar/serverless/baas/api/FindPersonApiHandler.java b/serverless/src/main/java/com/iluwatar/serverless/baas/api/FindPersonApiHandler.java index 0dac4c614b33..14f2b25a5153 100644 --- a/serverless/src/main/java/com/iluwatar/serverless/baas/api/FindPersonApiHandler.java +++ b/serverless/src/main/java/com/iluwatar/serverless/baas/api/FindPersonApiHandler.java @@ -27,7 +27,10 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; import com.iluwatar.serverless.baas.model.Person; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; /** * find person from persons collection @@ -36,13 +39,15 @@ public class FindPersonApiHandler extends AbstractDynamoDbHandler implements RequestHandler { - private static final Logger LOG = Logger.getLogger(FindPersonApiHandler.class); + private static final Logger LOG = LoggerFactory.getLogger(FindPersonApiHandler.class); private static final Integer SUCCESS_STATUS_CODE = 200; @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent, Context context) { - LOG.info(apiGatewayProxyRequestEvent.getPathParameters()); + Map pathParameters = apiGatewayProxyRequestEvent.getPathParameters(); + pathParameters.keySet().stream().map(key -> key + "=" + pathParameters.get(key)).forEach(LOG::info); + Person person = this.getDynamoDbMapper().load(Person.class, apiGatewayProxyRequestEvent .getPathParameters().get("id")); diff --git a/serverless/src/main/java/com/iluwatar/serverless/baas/api/SavePersonApiHandler.java b/serverless/src/main/java/com/iluwatar/serverless/baas/api/SavePersonApiHandler.java index d77687d23b1f..8ef4b839ec63 100644 --- a/serverless/src/main/java/com/iluwatar/serverless/baas/api/SavePersonApiHandler.java +++ b/serverless/src/main/java/com/iluwatar/serverless/baas/api/SavePersonApiHandler.java @@ -27,7 +27,8 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; import com.iluwatar.serverless.baas.model.Person; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; @@ -38,7 +39,7 @@ public class SavePersonApiHandler extends AbstractDynamoDbHandler implements RequestHandler { - private static final Logger LOG = Logger.getLogger(SavePersonApiHandler.class); + private static final Logger LOG = LoggerFactory.getLogger(SavePersonApiHandler.class); private static final Integer CREATED_STATUS_CODE = 201; private static final Integer BAD_REQUEST_STATUS_CODE = 400; diff --git a/serverless/src/main/java/com/iluwatar/serverless/faas/api/LambdaInfoApiHandler.java b/serverless/src/main/java/com/iluwatar/serverless/faas/api/LambdaInfoApiHandler.java index 1157c962cb7c..174e409c35c6 100644 --- a/serverless/src/main/java/com/iluwatar/serverless/faas/api/LambdaInfoApiHandler.java +++ b/serverless/src/main/java/com/iluwatar/serverless/faas/api/LambdaInfoApiHandler.java @@ -27,8 +27,8 @@ import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.iluwatar.serverless.faas.LambdaInfo; -import org.apache.log4j.BasicConfigurator; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; @@ -39,13 +39,12 @@ */ public class LambdaInfoApiHandler implements RequestHandler, ApiGatewayResponse> { - private static final Logger LOG = Logger.getLogger(LambdaInfoApiHandler.class); + private static final Logger LOG = LoggerFactory.getLogger(LambdaInfoApiHandler.class); private static final Integer SUCCESS_STATUS_CODE = 200; @Override public ApiGatewayResponse handleRequest(Map input, Context context) { - BasicConfigurator.configure(); LOG.info("received: " + input); return new ApiGatewayResponse From a2cd605c93f1102e1a003367cde127cc4062a0b1 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sun, 13 Oct 2019 21:07:46 +0800 Subject: [PATCH 14/19] --- pom.xml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pom.xml b/pom.xml index 4ed357181a12..41117c95c649 100644 --- a/pom.xml +++ b/pom.xml @@ -56,10 +56,8 @@ 1.2.3 1.1.0 1.11.289 - 1.0.0 2.0.1 2.8.5 - 1.2.17 2.3.1 2.3.2 1.3.2 @@ -304,11 +302,6 @@ mongo-java-driver ${mongo-java-driver.version} - - log4j - log4j - ${log4j.version} - javax.xml.bind jaxb-api From 73878ddb69c4336dfe59a886c055f1f33e69e3df Mon Sep 17 00:00:00 2001 From: Joshua Date: Sat, 12 Oct 2019 12:03:44 +0800 Subject: [PATCH 15/19] #970: Remove unnecessary gitignore line for log4j.xml :fire: --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index fd0bb7810faf..ada1e7d10ce5 100644 --- a/.gitignore +++ b/.gitignore @@ -16,5 +16,4 @@ datanucleus.log /bin/ /bin/ *.log -data-mapper/src/main/resources/log4j.xml event-sourcing/Journal.json From 1f346307d8cb41a64f4461c3303fd57c02c62914 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sun, 13 Oct 2019 21:02:58 +0800 Subject: [PATCH 16/19] --- naked-objects/dom/exclude-pmd.properties | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 naked-objects/dom/exclude-pmd.properties diff --git a/naked-objects/dom/exclude-pmd.properties b/naked-objects/dom/exclude-pmd.properties deleted file mode 100644 index e281170f75a4..000000000000 --- a/naked-objects/dom/exclude-pmd.properties +++ /dev/null @@ -1,24 +0,0 @@ -# -# The MIT License -# Copyright © 2014-2019 Ilkka Seppälä -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# - -domainapp.dom.modules.simple.QSimpleObject=UnusedFormalParameter \ No newline at end of file From a367a3256f1922ced58aa98877e43e64124fb3fb Mon Sep 17 00:00:00 2001 From: Joshua Date: Sun, 13 Oct 2019 21:29:54 +0800 Subject: [PATCH 17/19] #970: Remove remaining remnants of log4j :fire: --- commander/properties/log4j.properties | 41 ------------------- dao/src/main/resources/log4j.xml | 41 ------------------- data-mapper/src/main/resources/log4j.xml | 41 ------------------- module/src/main/resources/log4j.xml | 41 ------------------- naked-objects/dom/pom.xml | 1 - .../bootstrap/SimpleAppSystemInitializer.java | 1 - .../specglue/BootstrappingGlue.java | 1 - .../integtests/tests/SimpleAppIntegTest.java | 1 - naked-objects/webapp/pom.xml | 20 --------- .../src/main/resources/log4j.properties | 29 ------------- 10 files changed, 217 deletions(-) delete mode 100644 commander/properties/log4j.properties delete mode 100644 dao/src/main/resources/log4j.xml delete mode 100644 data-mapper/src/main/resources/log4j.xml delete mode 100644 module/src/main/resources/log4j.xml delete mode 100644 serverless/src/main/resources/log4j.properties diff --git a/commander/properties/log4j.properties b/commander/properties/log4j.properties deleted file mode 100644 index 6569015544ba..000000000000 --- a/commander/properties/log4j.properties +++ /dev/null @@ -1,41 +0,0 @@ -# -# The MIT License -# Copyright © 2014-2019 Ilkka Seppälä -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# - -#Define root logger options -log4j.rootLogger=TRACE, file, console - -#Define console appender -log4j.appender.console=org.apache.log4j.ConsoleAppender -logrj.appender.console.Target=System.out -log4j.appender.console.layout=org.apache.log4j.PatternLayout -log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd} %d{HH:mm:ss} %5p[%t] %m%n - -#Define rolling file appender -log4j.appender.file=org.apache.log4j.RollingFileAppender -log4j.appender.file.File=/log/logFile.log -log4j.appender.file.Append=true -log4j.appender.file.ImmediateFlush=true -log4j.appender.file.MaxFileSize=10MB -log4j.appender.file.MaxBackupIndex=5 -log4j.appender.file.layout=org.apache.log4j.PatternLayout -log4j.appender.file.layout.ConversionPattern=%d %d{HH:mm:ss} %5p[%t] %m%n diff --git a/dao/src/main/resources/log4j.xml b/dao/src/main/resources/log4j.xml deleted file mode 100644 index 5c7e49d51a98..000000000000 --- a/dao/src/main/resources/log4j.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/data-mapper/src/main/resources/log4j.xml b/data-mapper/src/main/resources/log4j.xml deleted file mode 100644 index 5c7e49d51a98..000000000000 --- a/data-mapper/src/main/resources/log4j.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/module/src/main/resources/log4j.xml b/module/src/main/resources/log4j.xml deleted file mode 100644 index 5c7e49d51a98..000000000000 --- a/module/src/main/resources/log4j.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/naked-objects/dom/pom.xml b/naked-objects/dom/pom.xml index b8a04a53bb24..2709edda03f4 100644 --- a/naked-objects/dom/pom.xml +++ b/naked-objects/dom/pom.xml @@ -143,7 +143,6 @@ ${datanucleus-maven-plugin.version} false - ${basedir}/log4j.properties true ${basedir}/datanucleus.properties diff --git a/naked-objects/integtests/src/test/java/domainapp/integtests/bootstrap/SimpleAppSystemInitializer.java b/naked-objects/integtests/src/test/java/domainapp/integtests/bootstrap/SimpleAppSystemInitializer.java index d5f0cd55265a..930a805d5fe2 100644 --- a/naked-objects/integtests/src/test/java/domainapp/integtests/bootstrap/SimpleAppSystemInitializer.java +++ b/naked-objects/integtests/src/test/java/domainapp/integtests/bootstrap/SimpleAppSystemInitializer.java @@ -49,7 +49,6 @@ public static void initIsft() { private static class SimpleAppSystemBuilder extends IsisSystemForTest.Builder { public SimpleAppSystemBuilder() { - withLoggingAt(org.apache.log4j.Level.INFO); with(testConfiguration()); with(new DataNucleusPersistenceMechanismInstaller()); diff --git a/naked-objects/integtests/src/test/java/domainapp/integtests/specglue/BootstrappingGlue.java b/naked-objects/integtests/src/test/java/domainapp/integtests/specglue/BootstrappingGlue.java index b53e88af0c0e..4b22615ecc96 100644 --- a/naked-objects/integtests/src/test/java/domainapp/integtests/specglue/BootstrappingGlue.java +++ b/naked-objects/integtests/src/test/java/domainapp/integtests/specglue/BootstrappingGlue.java @@ -36,7 +36,6 @@ public class BootstrappingGlue extends CukeGlueAbstract { @Before(value = {"@integration"}, order = 100) public void beforeScenarioIntegrationScope() { - org.apache.log4j.PropertyConfigurator.configure("logging.properties"); SimpleAppSystemInitializer.initIsft(); before(ScenarioExecutionScope.INTEGRATION); diff --git a/naked-objects/integtests/src/test/java/domainapp/integtests/tests/SimpleAppIntegTest.java b/naked-objects/integtests/src/test/java/domainapp/integtests/tests/SimpleAppIntegTest.java index 0db09fceb69f..0c19714253ba 100644 --- a/naked-objects/integtests/src/test/java/domainapp/integtests/tests/SimpleAppIntegTest.java +++ b/naked-objects/integtests/src/test/java/domainapp/integtests/tests/SimpleAppIntegTest.java @@ -36,7 +36,6 @@ public abstract class SimpleAppIntegTest extends IntegrationTestAbstract { @BeforeClass public static void initClass() { - org.apache.log4j.PropertyConfigurator.configure("logging.properties"); SimpleAppSystemInitializer.initIsft(); // instantiating will install onto ThreadLocal diff --git a/naked-objects/webapp/pom.xml b/naked-objects/webapp/pom.xml index e26c5f5610ad..e0697b855b31 100644 --- a/naked-objects/webapp/pom.xml +++ b/naked-objects/webapp/pom.xml @@ -216,26 +216,6 @@ hsqldb - - - - - - - org.lazyluke - log4jdbc-remix - - - org.slf4j - slf4j-api - - - - - junit junit diff --git a/serverless/src/main/resources/log4j.properties b/serverless/src/main/resources/log4j.properties deleted file mode 100644 index 2c666e2869ad..000000000000 --- a/serverless/src/main/resources/log4j.properties +++ /dev/null @@ -1,29 +0,0 @@ -# -# The MIT License -# Copyright © 2014-2019 Ilkka Seppälä -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# - -log = . -log4j.rootLogger = DEBUG, LAMBDA - -log4j.appender.LAMBDA=com.amazonaws.services.lambda.runtime.log4j.LambdaAppender -log4j.appender.LAMBDA.layout=org.apache.log4j.PatternLayout -log4j.appender.LAMBDA.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} <%X{AWSRequestId}> %-5p %c:%L - %m%n From f736c7ea50009a7f361a6038a632ee8d56c473d2 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sun, 13 Oct 2019 22:54:52 +0800 Subject: [PATCH 18/19] #970: Replace System.out logging with appropriate logging methods :art: --- .../iluwatar/collectionpipeline/AppTest.java | 7 +++++-- .../java/com/iluwatar/commander/Commander.java | 14 +++++++------- .../messagingservice/MessagingService.java | 5 ++++- .../main/java/com/iluwatar/converter/App.java | 10 +++++++--- .../main/java/com/iluwatar/dirtyflag/App.java | 9 +++++++-- .../com/iluwatar/dirtyflag/DataFetcher.java | 8 +++++++- .../main/java/com/iluwatar/event/queue/App.java | 7 ++++++- .../administration/ConsoleAdministration.java | 2 +- .../hexagonal/service/ConsoleLottery.java | 2 +- .../service/LotteryConsoleServiceImpl.java | 2 +- .../java/com/iluwatar/masterworker/App.java | 8 +++++--- .../masterworker/ArrayUtilityMethods.java | 10 +++++++--- .../domainapp/webapp/SimpleApplication.java | 5 ++++- .../ConvertToCharArrayHandler.java | 4 ++-- .../RemoveAlphabetsHandler.java | 4 ++-- .../RemoveDigitsHandler.java | 4 ++-- .../java/com/iluwatar/spatialpartition/App.java | 14 +++++++++----- .../com/iluwatar/spatialpartition/Bubble.java | 6 +++++- tls/src/main/java/com/iluwatar/tls/App.java | 15 ++++++++++----- .../com/iluwatar/tls/DateFormatCallable.java | 9 +++++++-- .../main/java/com/iluwatar/typeobject/App.java | 17 ++++++++++------- .../java/com/iluwatar/typeobject/CandyGame.java | 17 +++++++++++------ 22 files changed, 120 insertions(+), 59 deletions(-) diff --git a/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java b/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java index 83756985fe06..938cce195a27 100644 --- a/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java +++ b/collection-pipeline/src/test/java/com/iluwatar/collectionpipeline/AppTest.java @@ -30,12 +30,15 @@ import java.util.Map; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Tests that Collection Pipeline methods work as expected. */ public class AppTest { - + private static final Logger LOGGER = LoggerFactory.getLogger(AppTest.class); + private List cars = CarFactory.createCars(); @Test @@ -61,7 +64,7 @@ public void testGetGroupingOfCarsByCategory() { new Car("Jeep", "Comanche", 1990, Category.JEEP))); Map> modelsFunctional = FunctionalProgramming.getGroupingOfCarsByCategory(cars); Map> modelsImperative = ImperativeProgramming.getGroupingOfCarsByCategory(cars); - System.out.println("Category " + modelsFunctional); + LOGGER.info("Category " + modelsFunctional); assertEquals(modelsExpected, modelsFunctional); assertEquals(modelsExpected, modelsImperative); } diff --git a/commander/src/main/java/com/iluwatar/commander/Commander.java b/commander/src/main/java/com/iluwatar/commander/Commander.java index 57914fe9bff5..dee12df1cee8 100644 --- a/commander/src/main/java/com/iluwatar/commander/Commander.java +++ b/commander/src/main/java/com/iluwatar/commander/Commander.java @@ -126,27 +126,27 @@ private void sendShippingRequest(Order order) throws Exception { String transactionId = shippingService.receiveRequest(order.item, order.user.address); //could save this transaction id in a db too LOG.info("Order " + order.id + ": Shipping placed successfully, transaction id: " + transactionId); - System.out.println("Order has been placed and will be shipped to you. Please wait while we make your" + LOG.info("Order has been placed and will be shipped to you. Please wait while we make your" + " payment... "); sendPaymentRequest(order); return; }; Retry.HandleErrorIssue handleError = (o,err) -> { if (ShippingNotPossibleException.class.isAssignableFrom(err.getClass())) { - System.out.println("Shipping is currently not possible to your address. We are working on the problem " + LOG.info("Shipping is currently not possible to your address. We are working on the problem " + "and will get back to you asap."); finalSiteMsgShown = true; LOG.info("Order " + order.id + ": Shipping not possible to address, trying to add problem to employee db.."); employeeHandleIssue(o); } else if (ItemUnavailableException.class.isAssignableFrom(err.getClass())) { - System.out.println("This item is currently unavailable. We will inform you as soon as the item becomes " + LOG.info("This item is currently unavailable. We will inform you as soon as the item becomes " + "available again."); finalSiteMsgShown = true; LOG.info("Order " + order.id + ": Item " + order.item + " unavailable, trying to add problem to employee " + "handle.."); employeeHandleIssue(o); } else { - System.out.println("Sorry, there was a problem in creating your order. Please try later."); + LOG.info("Sorry, there was a problem in creating your order. Please try later."); LOG.error("Order " + order.id + ": Shipping service unavailable, order not placed.."); finalSiteMsgShown = true; } @@ -184,7 +184,7 @@ public void run() { order.paid = PaymentStatus.Done; LOG.info("Order " + order.id + ": Payment successful, transaction Id: " + transactionId); if (!finalSiteMsgShown) { - System.out.println("Payment made successfully, thank you for shopping with us!!"); + LOG.info("Payment made successfully, thank you for shopping with us!!"); finalSiteMsgShown = true; } sendSuccessMessage(order); @@ -194,7 +194,7 @@ public void run() { Retry.HandleErrorIssue handleError = (o,err) -> { if (PaymentDetailsErrorException.class.isAssignableFrom(err.getClass())) { if (!finalSiteMsgShown) { - System.out.println("There was an error in payment. Your account/card details may have been incorrect. " + LOG.info("There was an error in payment. Your account/card details may have been incorrect. " + "Meanwhile, your order has been converted to COD and will be shipped."); finalSiteMsgShown = true; } @@ -205,7 +205,7 @@ public void run() { try { if (o.messageSent.equals(MessageSent.NoneSent)) { if (!finalSiteMsgShown) { - System.out.println("There was an error in payment. We are on it, and will get back to you " + LOG.info("There was an error in payment. We are on it, and will get back to you " + "asap. Don't worry, your order has been placed and will be shipped."); finalSiteMsgShown = true; } diff --git a/commander/src/main/java/com/iluwatar/commander/messagingservice/MessagingService.java b/commander/src/main/java/com/iluwatar/commander/messagingservice/MessagingService.java index 27c1eee3f1f2..5023cfa1faa2 100644 --- a/commander/src/main/java/com/iluwatar/commander/messagingservice/MessagingService.java +++ b/commander/src/main/java/com/iluwatar/commander/messagingservice/MessagingService.java @@ -24,6 +24,8 @@ import com.iluwatar.commander.Service; import com.iluwatar.commander.exceptions.DatabaseUnavailableException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The MessagingService is used to send messages to user regarding their order and @@ -32,6 +34,7 @@ */ public class MessagingService extends Service { + private static final Logger LOGGER = LoggerFactory.getLogger(MessagingService.class); enum MessageToSend { PaymentFail, PaymentTrying, PaymentSuccessful @@ -74,7 +77,7 @@ protected String updateDb(Object...parameters) throws DatabaseUnavailableExcepti MessageRequest req = (MessageRequest) parameters[0]; if (this.database.get(req.reqId) == null) { //idempotence, in case db fails here database.add(req); //if successful: - System.out.println(sendMessage(req.msg)); + LOGGER.info(sendMessage(req.msg)); return req.reqId; } return null; diff --git a/converter/src/main/java/com/iluwatar/converter/App.java b/converter/src/main/java/com/iluwatar/converter/App.java index 7d0821c5b501..a317457b9af6 100644 --- a/converter/src/main/java/com/iluwatar/converter/App.java +++ b/converter/src/main/java/com/iluwatar/converter/App.java @@ -24,6 +24,8 @@ import com.google.common.collect.Lists; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; @@ -35,6 +37,8 @@ * objects between types. */ public class App { + + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** * Program entry point * @@ -45,14 +49,14 @@ public static void main(String[] args) { UserDto dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com"); User user = userConverter.convertFromDto(dtoUser); - System.out.println("Entity converted from DTO:" + user); + LOGGER.info("Entity converted from DTO:" + user); ArrayList users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"), new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243")); - System.out.println("Domain entities:"); + LOGGER.info("Domain entities:"); users.forEach(System.out::println); - System.out.println("DTO entities converted from domain:"); + LOGGER.info("DTO entities converted from domain:"); List dtoEntities = userConverter.createFromEntities(users); dtoEntities.forEach(System.out::println); diff --git a/dirty-flag/src/main/java/com/iluwatar/dirtyflag/App.java b/dirty-flag/src/main/java/com/iluwatar/dirtyflag/App.java index af95b57bca88..e5368bc3a5a6 100644 --- a/dirty-flag/src/main/java/com/iluwatar/dirtyflag/App.java +++ b/dirty-flag/src/main/java/com/iluwatar/dirtyflag/App.java @@ -22,6 +22,9 @@ */ package com.iluwatar.dirtyflag; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -48,6 +51,8 @@ * when needed. {@link World} mainly serves the data to the front-end. */ public class App { + + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** * Program execution point */ @@ -59,9 +64,9 @@ public void run() { @Override public void run() { List countries = world.fetch(); - System.out.println("Our world currently has the following countries:-"); + LOGGER.info("Our world currently has the following countries:-"); for (String country : countries) { - System.out.println("\t" + country); + LOGGER.info("\t" + country); } } }, 0, 15, TimeUnit.SECONDS); // Run at every 15 seconds. diff --git a/dirty-flag/src/main/java/com/iluwatar/dirtyflag/DataFetcher.java b/dirty-flag/src/main/java/com/iluwatar/dirtyflag/DataFetcher.java index 3b91ccd7debb..eca422a86165 100644 --- a/dirty-flag/src/main/java/com/iluwatar/dirtyflag/DataFetcher.java +++ b/dirty-flag/src/main/java/com/iluwatar/dirtyflag/DataFetcher.java @@ -22,6 +22,10 @@ */ package com.iluwatar.dirtyflag; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.xml.crypto.Data; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; @@ -37,6 +41,8 @@ */ public class DataFetcher { + private static final Logger LOGGER = LoggerFactory.getLogger(DataFetcher.class); + private final String filename = "world.txt"; private long lastFetched; @@ -62,7 +68,7 @@ public List fetch() { File file = new File(classLoader.getResource(filename).getFile()); if (isDirty(file.lastModified())) { - System.out.println(filename + " is dirty! Re-fetching file content..."); + LOGGER.info(filename + " is dirty! Re-fetching file content..."); List data = new ArrayList(); try (BufferedReader br = new BufferedReader(new FileReader(file))) { diff --git a/event-queue/src/main/java/com/iluwatar/event/queue/App.java b/event-queue/src/main/java/com/iluwatar/event/queue/App.java index 88cc0bf570ae..87ad78ae6d40 100644 --- a/event-queue/src/main/java/com/iluwatar/event/queue/App.java +++ b/event-queue/src/main/java/com/iluwatar/event/queue/App.java @@ -22,6 +22,9 @@ */ package com.iluwatar.event.queue; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -39,6 +42,8 @@ * items from the queue at a later time. */ public class App { + + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** * Program entry point. * @@ -51,7 +56,7 @@ public static void main(String[] args) throws UnsupportedAudioFileException, IOE audio.playSound(audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f); audio.playSound(audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f); - System.out.println("Press Enter key to stop the program..."); + LOGGER.info("Press Enter key to stop the program..."); try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { br.read(); } diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java index f2aa6d681ba5..4d51710b9d04 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/administration/ConsoleAdministration.java @@ -81,7 +81,7 @@ private static void printMainMenu() { } private static String readString(Scanner scanner) { - System.out.print("> "); + LOGGER.info("> "); return scanner.next(); } } diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleLottery.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleLottery.java index 583bf348fc7e..3a41e1ef322f 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleLottery.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/service/ConsoleLottery.java @@ -82,7 +82,7 @@ private static void printMainMenu() { } private static String readString(Scanner scanner) { - System.out.print("> "); + LOGGER.info("> "); return scanner.next(); } } diff --git a/hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryConsoleServiceImpl.java b/hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryConsoleServiceImpl.java index 17f5f1a9bc09..d61ec9108156 100644 --- a/hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryConsoleServiceImpl.java +++ b/hexagonal/src/main/java/com/iluwatar/hexagonal/service/LotteryConsoleServiceImpl.java @@ -122,7 +122,7 @@ public void queryLotteryAccountFunds(WireTransfers bank, Scanner scanner) { } private String readString(Scanner scanner) { - System.out.print( "> " ); + logger.info( "> " ); return scanner.next(); } } diff --git a/master-worker-pattern/src/main/java/com/iluwatar/masterworker/App.java b/master-worker-pattern/src/main/java/com/iluwatar/masterworker/App.java index ce1f7b625653..46c0baf8d16c 100644 --- a/master-worker-pattern/src/main/java/com/iluwatar/masterworker/App.java +++ b/master-worker-pattern/src/main/java/com/iluwatar/masterworker/App.java @@ -23,6 +23,8 @@ package com.iluwatar.masterworker; import com.iluwatar.masterworker.system.ArrayTransposeMasterWorker; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** *

The Master-Worker pattern is used when the problem at hand can be solved by dividing into @@ -45,7 +47,8 @@ */ public class App { - + + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** * Program entry point. * @param args command line args @@ -60,10 +63,9 @@ public static void main(String[] args) { ArrayResult result = (ArrayResult) mw.getResult(input); if (result != null) { ArrayUtilityMethods.printMatrix(inputMatrix); - System.out.println(""); ArrayUtilityMethods.printMatrix(result.data); } else { - System.out.println("Please enter non-zero input"); + LOGGER.info("Please enter non-zero input"); } } diff --git a/master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayUtilityMethods.java b/master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayUtilityMethods.java index e3125abe8032..fb27e5b486a4 100644 --- a/master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayUtilityMethods.java +++ b/master-worker-pattern/src/main/java/com/iluwatar/masterworker/ArrayUtilityMethods.java @@ -22,6 +22,9 @@ */ package com.iluwatar.masterworker; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.Random; /** @@ -29,7 +32,8 @@ */ public class ArrayUtilityMethods { - + + private static final Logger LOGGER = LoggerFactory.getLogger(ArrayUtilityMethods.class); /** * Method arraysSame compares 2 arrays @param a1 and @param a2 * and @return whether their values are equal (boolean). @@ -100,9 +104,9 @@ public static void printMatrix(int[][] matrix) { //prints out int[][] for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { - System.out.print(matrix[i][j] + " "); + LOGGER.info(matrix[i][j] + " "); } - System.out.println(""); + LOGGER.info(""); } } diff --git a/naked-objects/webapp/src/main/java/domainapp/webapp/SimpleApplication.java b/naked-objects/webapp/src/main/java/domainapp/webapp/SimpleApplication.java index cf15632faf53..875212963b67 100644 --- a/naked-objects/webapp/src/main/java/domainapp/webapp/SimpleApplication.java +++ b/naked-objects/webapp/src/main/java/domainapp/webapp/SimpleApplication.java @@ -50,6 +50,8 @@ import de.agilecoders.wicket.core.settings.IBootstrapSettings; import de.agilecoders.wicket.themes.markup.html.bootswatch.BootswatchTheme; import de.agilecoders.wicket.themes.markup.html.bootswatch.BootswatchThemeProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** @@ -72,6 +74,7 @@ */ public class SimpleApplication extends IsisWicketApplication { + private static final Logger LOGGER = LoggerFactory.getLogger(SimpleApplication.class); private static final long serialVersionUID = 1L; /** @@ -124,7 +127,7 @@ public WebRequest newWebRequest(HttpServletRequest servletRequest, String filter servletRequest.getSession().invalidate(); } } catch (Exception e) { - System.out.println(e); + LOGGER.error(e.getMessage()); } return super.newWebRequest(servletRequest, filterPath); } diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java b/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java index 3e8df8eefe3f..6a418fef4cf1 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/ConvertToCharArrayHandler.java @@ -32,12 +32,12 @@ */ class ConvertToCharArrayHandler implements Handler { - private final Logger logger = LoggerFactory.getLogger(ConvertToCharArrayHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ConvertToCharArrayHandler.class); @Override public char[] process(String input) { char[] characters = input.toCharArray(); - logger.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", + LOGGER.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", ConvertToCharArrayHandler.class, input, String.class, Arrays.toString(characters), Character[].class)); return characters; diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java index 76219249d312..342ed86c23c7 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveAlphabetsHandler.java @@ -30,7 +30,7 @@ */ class RemoveAlphabetsHandler implements Handler { - private final Logger logger = LoggerFactory.getLogger(RemoveAlphabetsHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(RemoveAlphabetsHandler.class); @Override public String process(String input) { @@ -46,7 +46,7 @@ public String process(String input) { } String inputWithoutAlphabetsStr = inputWithoutAlphabets.toString(); - logger.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", + LOGGER.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", RemoveAlphabetsHandler.class, input, String.class, inputWithoutAlphabetsStr, String.class)); return inputWithoutAlphabetsStr; diff --git a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveDigitsHandler.java b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveDigitsHandler.java index d1785c340858..a5320a0a55c4 100644 --- a/pipeline/src/main/java/com.iluwatar.pipeline/RemoveDigitsHandler.java +++ b/pipeline/src/main/java/com.iluwatar.pipeline/RemoveDigitsHandler.java @@ -30,7 +30,7 @@ */ class RemoveDigitsHandler implements Handler { - private final Logger logger = LoggerFactory.getLogger(RemoveDigitsHandler.class); + private static final Logger LOGGER = LoggerFactory.getLogger(RemoveDigitsHandler.class); @Override public String process(String input) { @@ -46,7 +46,7 @@ public String process(String input) { } String inputWithoutDigitsStr = inputWithoutDigits.toString(); - logger.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", + LOGGER.info(String.format("Current handler: %s, input is %s of type %s, output is %s, of type %s", RemoveDigitsHandler.class, input, String.class, inputWithoutDigitsStr, String.class)); return inputWithoutDigitsStr; diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java index 179f11d045f2..f6a07de62f6a 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/App.java @@ -22,6 +22,9 @@ */ package com.iluwatar.spatialpartition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.Enumeration; import java.util.Hashtable; @@ -52,6 +55,7 @@ */ public class App { + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); static void noSpatialPartition(int height, int width, int numOfMovements, Hashtable bubbles) { @@ -73,7 +77,7 @@ static void noSpatialPartition(int height, int width, } for (Integer key : bubbles.keySet()) { //bubbles not popped - System.out.println("Bubble " + key + " not popped"); + LOGGER.info("Bubble " + key + " not popped"); } } @@ -101,7 +105,7 @@ static void withSpatialPartition(int height, int width, } for (Integer key : bubbles.keySet()) { //bubbles not popped - System.out.println("Bubble " + key + " not popped"); + LOGGER.info("Bubble " + key + " not popped"); } } @@ -119,7 +123,7 @@ public static void main(String[] args) { Bubble b = new Bubble(rand.nextInt(300), rand.nextInt(300), i, rand.nextInt(2) + 1); bubbles1.put(i, b); bubbles2.put(i, b); - System.out.println("Bubble " + i + " with radius " + b.radius + " added at (" + b.x + "," + b.y + ")"); + LOGGER.info("Bubble " + i + " with radius " + b.radius + " added at (" + b.x + "," + b.y + ")"); } long start1 = System.currentTimeMillis(); @@ -128,8 +132,8 @@ public static void main(String[] args) { long start2 = System.currentTimeMillis(); App.withSpatialPartition(300,300,20,bubbles2); long end2 = System.currentTimeMillis(); - System.out.println("Without spatial partition takes " + (end1 - start1) + "ms"); - System.out.println("With spatial partition takes " + (end2 - start2) + "ms"); + LOGGER.info("Without spatial partition takes " + (end1 - start1) + "ms"); + LOGGER.info("With spatial partition takes " + (end2 - start2) + "ms"); } } diff --git a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java index 3e3d418373ff..01faa8371378 100644 --- a/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java +++ b/spatial-partition/src/main/java/com/iluwatar/spatialpartition/Bubble.java @@ -22,6 +22,9 @@ */ package com.iluwatar.spatialpartition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.Hashtable; import java.util.Random; @@ -32,6 +35,7 @@ */ public class Bubble extends Point { + private static final Logger LOGGER = LoggerFactory.getLogger(Bubble.class); final int radius; @@ -54,7 +58,7 @@ boolean touches(Bubble b) { } void pop(Hashtable allBubbles) { - System.out.println("Bubble " + this.id + " popped at (" + this.x + "," + this.y + ")!"); + LOGGER.info("Bubble " + this.id + " popped at (" + this.x + "," + this.y + ")!"); allBubbles.remove(this.id); } diff --git a/tls/src/main/java/com/iluwatar/tls/App.java b/tls/src/main/java/com/iluwatar/tls/App.java index cd216ac75f1e..33f8c312907f 100644 --- a/tls/src/main/java/com/iluwatar/tls/App.java +++ b/tls/src/main/java/com/iluwatar/tls/App.java @@ -22,6 +22,9 @@ */ package com.iluwatar.tls; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.Calendar; import java.util.Date; import java.util.concurrent.ExecutorService; @@ -64,6 +67,8 @@ * @author Thomas Bauer, 2017 */ public class App { + + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** * Program entry point * @@ -99,11 +104,11 @@ public static void main(String[] args) { // a correct run should deliver 20 times 15.12.2015 // and a correct run shouldn't deliver any exception - System.out.println("The List dateList contains " + counterDateValues + " date values"); - System.out.println("The List exceptionList contains " + counterExceptions + " exceptions"); + LOGGER.info("The List dateList contains " + counterDateValues + " date values"); + LOGGER.info("The List exceptionList contains " + counterExceptions + " exceptions"); } catch (Exception e) { - System.out.println("Abnormal end of program. Program throws exception: " + e); + LOGGER.info("Abnormal end of program. Program throws exception: " + e); } executor.shutdown(); } @@ -121,7 +126,7 @@ private static int printAndCountDates(Result res) { Calendar cal = Calendar.getInstance(); cal.setTime(dt); // Formatted output of the date value: DD.MM.YYYY - System.out.println( + LOGGER.info( cal.get(Calendar.DAY_OF_MONTH) + "." + cal.get(Calendar.MONTH) + "." + +cal.get(Calendar.YEAR)); } return counter; @@ -138,7 +143,7 @@ private static int printAndCountExceptions(Result res) { int counter = 0; for (String ex : res.getExceptionList()) { counter++; - System.out.println(ex); + LOGGER.info(ex); } return counter; } diff --git a/tls/src/main/java/com/iluwatar/tls/DateFormatCallable.java b/tls/src/main/java/com/iluwatar/tls/DateFormatCallable.java index 8018e7996cb9..a047828ce3e6 100644 --- a/tls/src/main/java/com/iluwatar/tls/DateFormatCallable.java +++ b/tls/src/main/java/com/iluwatar/tls/DateFormatCallable.java @@ -22,6 +22,9 @@ */ package com.iluwatar.tls; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.concurrent.Callable; @@ -40,6 +43,8 @@ * @author Thomas Bauer, 2017 */ public class DateFormatCallable implements Callable { + + private static final Logger LOGGER = LoggerFactory.getLogger(DateFormatCallable.class); // class variables (members) private ThreadLocal df; //TLTL // private DateFormat df; //NTLNTL @@ -72,7 +77,7 @@ protected DateFormat initialValue() { //TLTL */ @Override public Result call() { - System.out.println(Thread.currentThread() + " started executing..."); + LOGGER.info(Thread.currentThread() + " started executing..."); Result result = new Result(); // Convert date value to date 5 times @@ -90,7 +95,7 @@ public Result call() { } - System.out.println(Thread.currentThread() + " finished processing part of the thread"); + LOGGER.info(Thread.currentThread() + " finished processing part of the thread"); return result; } diff --git a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java index 93e7a4ea84b9..2e6db9dd68a3 100644 --- a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java +++ b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java @@ -25,6 +25,8 @@ import java.io.FileNotFoundException; import java.io.IOException; import org.json.simple.parser.ParseException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /**

Type object pattern is the pattern we use when the OOP concept of creating a base class and * inheriting from it just doesn't work for the case in hand. This happens when we either don't know @@ -45,6 +47,7 @@ public class App { + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); /** * Program entry point. * @param args command line args @@ -62,9 +65,9 @@ public static void main(String[] args) throws FileNotFoundException, IOException CellPool pool = new CellPool(numOfRows * numOfRows + 5); CandyGame cg = new CandyGame(numOfRows, pool); if (round > 1) { - System.out.println("Refreshing.."); + LOGGER.info("Refreshing.."); } else { - System.out.println("Starting game.."); + LOGGER.info("Starting game.."); } cg.printGameStatus(); end = System.currentTimeMillis(); @@ -72,13 +75,13 @@ public static void main(String[] args) throws FileNotFoundException, IOException pointsWon += cg.totalPoints; end = System.currentTimeMillis(); } - System.out.println("Game Over"); + LOGGER.info("Game Over"); if (pointsWon >= toWin) { - System.out.println(pointsWon); - System.out.println("You win!!"); + LOGGER.info("" + pointsWon); + LOGGER.info("You win!!"); } else { - System.out.println(pointsWon); - System.out.println("Sorry, you lose!"); + LOGGER.info("" + pointsWon); + LOGGER.info("Sorry, you lose!"); } } } diff --git a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CandyGame.java b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CandyGame.java index 200a96f8b9c5..516b4cf76bfd 100644 --- a/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CandyGame.java +++ b/typeobjectpattern/src/main/java/com/iluwatar/typeobject/CandyGame.java @@ -24,6 +24,8 @@ import java.util.ArrayList; import com.iluwatar.typeobject.Candy.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * The CandyGame class contains the rules for the continuation of the game and has @@ -31,6 +33,9 @@ */ public class CandyGame { + + private static final Logger LOGGER = LoggerFactory.getLogger(CandyGame.class); + Cell[][] cells; CellPool pool; int totalPoints; @@ -57,21 +62,21 @@ static String numOfSpaces(int num) { } void printGameStatus() { - System.out.println(""); + LOGGER.info(""); for (int i = 0; i < cells.length; i++) { for (int j = 0; j < cells.length; j++) { String candyName = cells[i][j].candy.name; if (candyName.length() < 20) { int totalSpaces = 20 - candyName.length(); - System.out.print(numOfSpaces(totalSpaces / 2) + cells[i][j].candy.name + LOGGER.info(numOfSpaces(totalSpaces / 2) + cells[i][j].candy.name + numOfSpaces(totalSpaces - totalSpaces / 2) + "|"); } else { - System.out.print(candyName + "|"); + LOGGER.info(candyName + "|"); } } - System.out.println(""); + LOGGER.info(""); } - System.out.println(""); + LOGGER.info(""); } ArrayList adjacentCells(int yIndex, int xIndex) { @@ -121,7 +126,7 @@ boolean continueRound() { } void handleChange(int points) { - System.out.println("+" + points + " points!"); + LOGGER.info("+" + points + " points!"); this.totalPoints += points; printGameStatus(); } From 3069e17f9cc17f9b16215366db1593cc56f22144 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sun, 13 Oct 2019 23:00:12 +0800 Subject: [PATCH 19/19] #970: Replace System.out method references to Logger::info :art: --- converter/src/main/java/com/iluwatar/converter/App.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/converter/src/main/java/com/iluwatar/converter/App.java b/converter/src/main/java/com/iluwatar/converter/App.java index a317457b9af6..b21d4dd6afe8 100644 --- a/converter/src/main/java/com/iluwatar/converter/App.java +++ b/converter/src/main/java/com/iluwatar/converter/App.java @@ -54,11 +54,11 @@ public static void main(String[] args) { ArrayList users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"), new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243")); LOGGER.info("Domain entities:"); - users.forEach(System.out::println); + users.stream().map(User::toString).forEach(LOGGER::info); LOGGER.info("DTO entities converted from domain:"); List dtoEntities = userConverter.createFromEntities(users); - dtoEntities.forEach(System.out::println); + dtoEntities.stream().map(UserDto::toString).forEach(LOGGER::info); } }