Skip to content
Snippets Groups Projects
Commit 03fb253b authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Tightened the design a little.

- Replaced Iterable with Collection in signatures
- Moved exception-handling code closer to exception sources
parent 79f7bf42
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,7 @@ import java.util.*;
@SuppressWarnings("unused")
public class CSVReaderWriter {
@SuppressWarnings("WeakerAccess")
public static Set<Map<String, String>> readCSVasSet(String filename) {
Set<Map<String, String>> csvSet = null;
......@@ -22,13 +23,8 @@ public class CSVReaderWriter {
csvSet = (Set<Map<String, String>>) parseCSV(inputStreamReader, new HashSet<>());
} 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 csvSet;
}
......@@ -40,20 +36,14 @@ public class CSVReaderWriter {
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 {
Collection<Map<String, String>> destination) {
Map<String, String> line;
try {
CSVReaderHeaderAware csvReader = new CSVReaderHeaderAwareBuilder(inputStreamReader).build();
......@@ -62,32 +52,17 @@ public class CSVReaderWriter {
}
} catch (NullPointerException ignored) {
// CSVReaderHeaderAwareBuilder.build() throws NullPointerException if inputStreamReader is empty
}
return destination;
}
/* LEGACY METHODS */
@SuppressWarnings("WeakerAccess")
public static Set<Map<String, String>> readCSV(String filename) {
return readCSVasSet(filename);
}
/*
public static Set<Map<String, String>> readCSV(String filename) {
Set<Map<String, String>> csvSet = null;
ClassLoader classLoader = CSVReaderWriter.class.getClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream("csv/" + filename)) {
csvSet = parseCSV(inputStream);
} catch (IOException ioException) {
System.err.println("Error loading " + filename + ". " + ioException);
System.err.println("Error reading CSV file. " + ioException);
} catch (CsvValidationException csvValidationException) {
System.err.println("Could not validate a line in CSV file. " + csvValidationException);
}
return csvSet;
return destination;
}
*/
// 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>
@SuppressWarnings("WeakerAccess")
public static boolean writeCSV(String filename, Iterable<Map<String, String>> data) {
public static boolean writeCSV(String filename, Collection<Map<String, String>> data) {
boolean wroteFile = true;
ClassLoader classLoader = CSVReaderWriter.class.getClassLoader();
URL resource = classLoader.getResource("csv/" + filename);
......@@ -109,45 +84,7 @@ public class CSVReaderWriter {
return wroteFile;
}
static Set<Map<String, String>> parseCSV(InputStream inputStream) throws IOException {
try {
return (Set<Map<String, String>>) parseCSV(new InputStreamReader(inputStream), new HashSet<>());
} catch (CsvValidationException csvValidationException) {
csvValidationException.printStackTrace();
return null;
}
}
/*
static Set<Map<String, String>> parseCSV(InputStream inputStream) throws IOException {
Set<Map<String, String>> csvSet = new HashSet<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
List<String> fieldNames = new ArrayList<>();
boolean header = true;
while ((line = reader.readLine()) != null) {
String[] elements = line.split(DELIMTER);
if (header) {
if (elements[0].charAt(0) == BYTE_ORDER_MARK) {
elements[0] = elements[0].substring(1); // remove text-encoding character sometimes added by
// Excel
}
fieldNames.addAll(Arrays.asList(elements));
header = false;
} else {
Map<String, String> row = new HashMap<>();
int column = 0;
for (String element : elements) {
row.put(fieldNames.get(column++), element);
}
csvSet.add(row);
}
}
return csvSet;
}
*/
static void placeCSVonStream(Iterable<Map<String, String>> data, OutputStream outputStream) {
static void placeCSVonStream(Collection<Map<String, String>> data, OutputStream outputStream) {
PrintStream writer = new PrintStream(outputStream);
Set<String> fieldNames = null;
int number_of_fields = 0;
......@@ -170,9 +107,20 @@ public class CSVReaderWriter {
}
}
/* LEGACY METHODS */
public static Set<Map<String, String>> readCSV(String filename) {
return readCSVasSet(filename);
}
static Set<Map<String, String>> parseCSV(InputStream inputStream) {
return (Set<Map<String, String>>) parseCSV(new InputStreamReader(inputStream), new HashSet<>());
}
/*
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");
}
*/
}
A,B,C,D
A,B,C,D
larry,curly,moe,
one,two,,three
four,,five,six
7,8,9,
,alpha,beta,gamma
\ No newline at end of file
......@@ -49,12 +49,8 @@ public class CSVReaderWriterTest {
String[][] data = {headers, row};
// Output
placeCSVStringOnInputStream(data);
Set<Map<String, String>> result = null;
try {
Set<Map<String, String>> result;
result = CSVReaderWriter.parseCSV(inputStream);
} catch (IOException ignored) {
fail();
}
// Oracle -- Header
Set<String> expectedKeys = new HashSet<>(Arrays.asList(headers));
// Oracle -- Rows
......@@ -77,12 +73,8 @@ public class CSVReaderWriterTest {
String[][] data = {headers, rows[0], rows[1]};
// Output
placeCSVStringOnInputStream(data);
Set<Map<String, String>> result = null;
try {
Set<Map<String, String>> result;
result = CSVReaderWriter.parseCSV(inputStream);
} catch (IOException ignored) {
fail();
}
// Oracle -- Header
Set<String> expectedKeys = new HashSet<>(Arrays.asList(headers));
// Oracle -- Rows
......@@ -109,12 +101,8 @@ public class CSVReaderWriterTest {
String[][] data = {headers};
// Output
placeCSVStringOnInputStream(data);
Set<Map<String, String>> result = null;
try {
Set<Map<String, String>> result;
result = CSVReaderWriter.parseCSV(inputStream);
} catch (IOException ignored) {
fail();
}
// Oracle
int expectedResultSize = 0;
// Compare
......@@ -128,12 +116,8 @@ public class CSVReaderWriterTest {
String[][] data = {};
// Output
placeCSVStringOnInputStream(data);
Set<Map<String, String>> result = null;
try {
Set<Map<String, String>> result;
result = CSVReaderWriter.parseCSV(inputStream);
} catch (IOException ignored) {
fail();
}
// Oracle
int expectedResultSize = 0;
// Compare
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment