diff --git a/src/main/java/nl/lengrand/patterns/template/BinReportCreator.java b/src/main/java/nl/lengrand/patterns/template/BinReportCreator.java new file mode 100644 index 0000000..11ad0e8 --- /dev/null +++ b/src/main/java/nl/lengrand/patterns/template/BinReportCreator.java @@ -0,0 +1,21 @@ +package nl.lengrand.patterns.template; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class BinReportCreator extends ReportCreator{ + + @Override + protected List readValuesFromInput(File file) throws IOException { + String valuesAsString = new String(Files.readAllBytes(file.toPath()), StandardCharsets.US_ASCII); + + return Arrays.stream(valuesAsString.split("")) + .mapToInt(Integer::parseInt) + .boxed() + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/nl/lengrand/patterns/template/BinaryFileMaker.java b/src/main/java/nl/lengrand/patterns/template/BinaryFileMaker.java new file mode 100644 index 0000000..5df9daf --- /dev/null +++ b/src/main/java/nl/lengrand/patterns/template/BinaryFileMaker.java @@ -0,0 +1,18 @@ +package nl.lengrand.patterns.template; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class BinaryFileMaker { + public static void main(String[] args) throws IOException { + + Path path = Paths.get("src/main/resources/template.bin"); + byte[] bytes = "123456".getBytes(StandardCharsets.UTF_8); + + Files.write(path, bytes); + System.out.println("Successfully written data to the file"); + } +} diff --git a/src/main/java/nl/lengrand/patterns/template/CsvReportCreator.java b/src/main/java/nl/lengrand/patterns/template/CsvReportCreator.java new file mode 100644 index 0000000..38e62eb --- /dev/null +++ b/src/main/java/nl/lengrand/patterns/template/CsvReportCreator.java @@ -0,0 +1,23 @@ +package nl.lengrand.patterns.template; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class CsvReportCreator extends ReportCreator { + + @Override + protected List readValuesFromInput(File file) throws IOException { + try (BufferedReader br = new BufferedReader(new FileReader(file))) { + String[] values = br.readLine().split(","); + return Arrays.stream(values) + .map(v -> v.trim()) + .map(Integer::parseInt) + .collect(Collectors.toList()); + } + } +} diff --git a/src/main/java/nl/lengrand/patterns/template/README.md b/src/main/java/nl/lengrand/patterns/template/README.md new file mode 100644 index 0000000..c125c6b --- /dev/null +++ b/src/main/java/nl/lengrand/patterns/template/README.md @@ -0,0 +1,10 @@ +# Template method pattern + +From [Wikipedia](https://en.wikipedia.org/wiki/Template_method_pattern). + +The template method is a method in a superclass, usually an abstract superclass, and defines the skeleton of an operation in terms of a number of high-level steps. +The intent of the template method is to define the overall structure of the operation, while allowing subclasses to refine, or redefine, certain steps. The template method pattern is an example of inversion of control. + +This pattern is useful to reduce code duplication for variations of the same algorithm. + +Read and run [ReportCreation](ReportCreation.java) to see a live example. \ No newline at end of file diff --git a/src/main/java/nl/lengrand/patterns/template/ReportCreation.java b/src/main/java/nl/lengrand/patterns/template/ReportCreation.java new file mode 100644 index 0000000..5905da0 --- /dev/null +++ b/src/main/java/nl/lengrand/patterns/template/ReportCreation.java @@ -0,0 +1,27 @@ +package nl.lengrand.patterns.template; + +import java.io.File; +import java.io.IOException; + +public class ReportCreation { + public static void main(String[] args) throws IOException { + ReportCreation reportCreation = new ReportCreation(); + File csvFile = reportCreation.getFileFromName("template.csv"); + File binFile = reportCreation.getFileFromName("template.bin"); + + System.out.println("Creating report from CSV file"); + CsvReportCreator csvReportCreator = new CsvReportCreator(); + csvReportCreator.createReport(csvFile); + + System.out.println("Creating report from binary file"); + BinReportCreator binReportCreator = new BinReportCreator(); + binReportCreator.createReport(binFile); + + System.out.println("Done"); + } + + private File getFileFromName(String fileName){ + ClassLoader classLoader = getClass().getClassLoader(); + return new File(classLoader.getResource(fileName).getFile()); + } +} diff --git a/src/main/java/nl/lengrand/patterns/template/ReportCreator.java b/src/main/java/nl/lengrand/patterns/template/ReportCreator.java new file mode 100644 index 0000000..513ef24 --- /dev/null +++ b/src/main/java/nl/lengrand/patterns/template/ReportCreator.java @@ -0,0 +1,34 @@ +package nl.lengrand.patterns.template; + +import java.io.File; +import java.io.IOException; +import java.util.InputMismatchException; +import java.util.List; +import java.util.stream.Collectors; + +public abstract class ReportCreator { + + final void createReport(File filePath) throws IOException { + List values = readValuesFromInput(filePath); + checkValues(values); + printValues(values); + } + + private void printValues(List values) { + System.out.println("================="); + System.out.println("Values are:"); + System.out.println(values.stream() + .map(v -> v.toString()) + .collect(Collectors.joining(","))); + System.out.println("End of report"); + System.out.println("================="); + } + + protected abstract List readValuesFromInput(File file) throws IOException; + + private void checkValues(List values){ + if(values == null || values.stream().anyMatch( v -> v < 0)){ + throw new InputMismatchException("Expecting all values to be positive"); + } + } +} diff --git a/src/main/resources/template.bin b/src/main/resources/template.bin new file mode 100644 index 0000000..4632e06 --- /dev/null +++ b/src/main/resources/template.bin @@ -0,0 +1 @@ +123456 \ No newline at end of file diff --git a/src/main/resources/template.csv b/src/main/resources/template.csv new file mode 100644 index 0000000..c740031 --- /dev/null +++ b/src/main/resources/template.csv @@ -0,0 +1 @@ +1, 2, 3, 4, 5, 6 \ No newline at end of file