From 4821aa8f27d3a35af234346c2f456cb20ab2956c Mon Sep 17 00:00:00 2001
From: Christopher Bohn <bohn@unl.edu>
Date: Tue, 19 Nov 2019 11:29:08 -0600
Subject: [PATCH] Added reader that returns List, for preserving row order

---
 .../edu/unl/cse/csv_io/CSVReaderWriter.java   | 63 ++++++++-----------
 src/main/resources/csv/demo.csv               |  1 +
 2 files changed, 27 insertions(+), 37 deletions(-)

diff --git a/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java b/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
index 29d1ec0..6541434 100644
--- a/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
+++ b/src/main/java/edu/unl/cse/csv_io/CSVReaderWriter.java
@@ -12,6 +12,7 @@ import java.io.*;
 import java.net.URL;
 import java.util.*;
 
+@SuppressWarnings("unused")
 public class CSVReaderWriter {
     @SuppressWarnings("WeakerAccess")
     public static Set<Map<String, String>> readCSVasSet(String filename) {
@@ -32,6 +33,24 @@ public class CSVReaderWriter {
         return csvSet;
     }
 
+    public static List<Map<String, String>> readCSVasList(String filename) {
+        List<Map<String, String>> csvList = null;
+        try (InputStreamReader inputStreamReader = new InputStreamReader(Objects.requireNonNull(
+                CSVReaderWriter.class.getClassLoader().getResourceAsStream("csv/" + filename)))) {
+            csvList = (List<Map<String, String>>) parseCSV(inputStreamReader, new LinkedList<>());
+        } catch (NullPointerException nullPointerException) {
+            System.err.println("Check spelling of file " + filename + ".");
+            System.exit(1);
+        } catch (IOException ioException) {
+            System.err.println("Error loading " + filename + ".  " + ioException);
+            System.exit(1);
+        } catch (CsvValidationException csvValidationException) {
+            System.err.println("Could not validate a line in " + filename + ".  " + csvValidationException);
+            System.exit(1);
+        }
+        return csvList;
+    }
+
     private static Collection<Map<String, String>> parseCSV(InputStreamReader inputStreamReader,
                                                             Collection<Map<String, String>> destination)
             throws IOException, CsvValidationException {
@@ -48,7 +67,6 @@ public class CSVReaderWriter {
     }
 
     /* LEGACY METHODS */
-    @SuppressWarnings("WeakerAccess")
     public static Set<Map<String, String>> readCSV(String filename) {
         return readCSVasSet(filename);
     }
@@ -65,6 +83,8 @@ public class CSVReaderWriter {
     }
 */
 
+    // I'd like to replace this with something that uses openCSV, but it works, and openCSV doesn't seem to have
+    // writers that take map<string,string>
     public static boolean writeCSV(String filename, Set<Map<String, String>> data) {
         boolean wroteFile = true;
         ClassLoader classLoader = CSVReaderWriter.class.getClassLoader();
@@ -147,42 +167,11 @@ public class CSVReaderWriter {
             }
         }
     }
-
-    /*
-        public static void main(String[] args) {
-            Set<Map<String, String>> demo = readCSV("demo.csv");
-            boolean success = writeCSV("out.csv", demo);
-            System.out.println(success ? "Wrote file!" : "Didn't write file");
-        }
-    */
 /*
-    public static void main(String[] args) {
-        Set<Map<String,String>> students = readCSV("students.csv");
-        for (Map<String,String> student: students) {
-            System.out.println(student.get("Name"));
-        }
-    }
-*//*
-    public static void main(String[] args) {
-        Set<Map<String, String>> courses = readCSV("courses.csv");
-        for (Map<String, String> course : courses) {
-            String foo = course.get("URL");
-            if (foo == null) {
-                foo = "Null object";
-            } else if (foo.equals("")) foo = "Empty String";
-            System.out.println(course.get("CourseID") + " " + course.get("Section") + " " + foo + " " + course.get(
-                    "Prerequisite1"));
-        }
-    }*/
-    public static void main(String[] args) {
-        Set<Map<String, String>> courses = readCSV("empty.csv");
-        for (Map<String, String> course : courses) {
-            String foo = course.get("URL");
-            if (foo == null) {
-                foo = "Null object";
-            } else if (foo.equals("")) foo = "Empty String";
-            System.out.println(course.get("CourseID") + " " + course.get("Section") + " " + foo + " " + course.get(
-                    "Prerequisite1"));
-        }
+        public static void main(String[] args) {
+        Set<Map<String, String>> demo = readCSV("demo.csv");
+        boolean success = writeCSV("out.csv", demo);
+        System.out.println(success ? "Wrote file!" : "Didn't write file");
     }
+ */
 }
diff --git a/src/main/resources/csv/demo.csv b/src/main/resources/csv/demo.csv
index 8e1318e..c0e8085 100644
--- a/src/main/resources/csv/demo.csv
+++ b/src/main/resources/csv/demo.csv
@@ -1,4 +1,5 @@
 A,B,C,D
+larry,curly,moe,
 one,two,,three
 four,,five,six
 7,8,9,
-- 
GitLab