From 0d2655bbcd9a6f494cc549157dec0a0048284b11 Mon Sep 17 00:00:00 2001 From: Gautam Shah Date: Wed, 1 Mar 2017 23:35:11 +0000 Subject: [PATCH] Printing the compute time taken for entire program to finish --- src/main/java/com/postcode/BulkImport.java | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/postcode/BulkImport.java b/src/main/java/com/postcode/BulkImport.java index 1d30ac4..91a3eaf 100644 --- a/src/main/java/com/postcode/BulkImport.java +++ b/src/main/java/com/postcode/BulkImport.java @@ -10,6 +10,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.time.Instant; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -61,6 +62,7 @@ public String getHeaderString() { public Map readRecords() { Map postcodeMap = new HashMap(); BufferedReader br = null; + try { br = new BufferedReader(source); setHeader(br.lines() @@ -85,19 +87,23 @@ public static String toCsvRow(Map records) { .stream() .map(map -> map.toString().replaceAll("=", ",")) .collect(Collectors.joining("\n")); - } public static Map postcodeValidity(Map records) { Map newMap = new HashMap(); + //SortedMap newMap = new TreeMap<>(); + + //Comparator> byKey = (entry1, entry2) -> entry1.getKey().compareTo(entry2.getKey()); + records.entrySet() .stream() + //.sorted(byKey) .forEach( postMap -> { - if (postMap.getValue() != null && !PostCodeValidator.isPostCode(postMap.getValue())) { + if (postMap.getValue() == null || !PostCodeValidator.isPostCode(postMap.getValue())) { newMap.put(postMap.getKey(), postMap.getValue()); - } else { - // add your map for valid postcodes + } else if (postMap.getValue() != null && PostCodeValidator.isPostCode(postMap.getValue())) { + } } ); @@ -117,6 +123,8 @@ public static void writeCsvFile(String fileNamePath, String header, String conte } public static void main(String[] args) { + final long startTime = Instant.now().toEpochMilli(); + BulkImport bi = null; try { File file = new File("src/main/resources/import_data.csv"); @@ -132,5 +140,9 @@ public static void main(String[] args) { //System.out.println(toCsvRow(records)); //writeCsvFile("testname.csv", bi.getHeaderString(), toCsvRow(records)); writeCsvFile("failed_validation.csv", bi.getHeaderString(), toCsvRow(postcodeValidity(records))); + + final long stopTime = Instant.now().toEpochMilli(); + + System.out.println("Took: " + (stopTime - startTime) + "ms to complete the process"); } }