Skip to content

Commit

Permalink
Extend CsvFile (#1702)
Browse files Browse the repository at this point in the history
Provide ability to search headers without loading a row
  • Loading branch information
jodastephen committed Jun 5, 2018
1 parent 094e2f9 commit 97397b0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public final class CsvFile {
* The header row, ordered as the headers appear in the file.
*/
private final ImmutableList<String> headers;
/**
* The header map, transformed for case-insensitive searching.
*/
private final ImmutableMap<String, Integer> searchHeaders;
/**
* The data rows in the CSV file.
*/
Expand Down Expand Up @@ -184,7 +188,7 @@ public static CsvFile of(List<String> headers, List<? extends List<String>> rows
for (int i = 0; i < rows.size(); i++) {
csvRows.add(new CsvRow(copiedHeaders, searchHeaders, i + firstLine, ImmutableList.copyOf(rows.get(i))));
}
return new CsvFile(copiedHeaders, csvRows.build());
return new CsvFile(copiedHeaders, searchHeaders, csvRows.build());
}

//------------------------------------------------------------------------
Expand All @@ -203,7 +207,7 @@ private static CsvFile parseAll(
rows.add(new CsvRow(headers, searchHeaders, i + 1, fields));
}
}
return new CsvFile(headers, rows.build());
return new CsvFile(headers, searchHeaders, rows.build());
}

// parse a single line
Expand Down Expand Up @@ -275,10 +279,12 @@ static ImmutableMap<String, Integer> buildSearchHeaders(ImmutableList<String> he
* Restricted constructor.
*
* @param headers the header row
* @param searchHeaders the headers transformed for searching
* @param rows the data rows
*/
private CsvFile(ImmutableList<String> headers, ImmutableList<CsvRow> rows) {
private CsvFile(ImmutableList<String> headers, ImmutableMap<String, Integer> searchHeaders, ImmutableList<CsvRow> rows) {
this.headers = headers;
this.searchHeaders = searchHeaders;
this.rows = rows;
}

Expand Down Expand Up @@ -322,6 +328,17 @@ public CsvRow row(int index) {
return rows.get(index);
}

/**
* Checks if the file contains a header.
* <p>
* Matching is case insensitive.
*
* @return the header row
*/
public boolean containsHeader(String header) {
return searchHeaders.containsKey(header.toLowerCase(Locale.ENGLISH));
}

//-------------------------------------------------------------------------
/**
* Checks if this CSV file equals another.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public void test_of_empty_no_header() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(""), false);
assertEquals(csvFile.headers().size(), 0);
assertEquals(csvFile.rowCount(), 0);
assertEquals(csvFile.containsHeader("Foo"), false);
}

public void test_of_empty_with_header() {
Expand All @@ -100,6 +101,7 @@ public void test_of_simple_no_header() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1), false);
assertEquals(csvFile.headers().size(), 0);
assertEquals(csvFile.rowCount(), 4);
assertEquals(csvFile.containsHeader("Foo"), false);
assertEquals(csvFile.row(0).lineNumber(), 1);
assertEquals(csvFile.row(1).lineNumber(), 2);
assertEquals(csvFile.row(2).lineNumber(), 3);
Expand All @@ -126,6 +128,7 @@ public void test_of_simple_no_header() {
public void test_of_simple_no_header_tabs() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1T), false, '\t');
assertEquals(csvFile.headers().size(), 0);
assertEquals(csvFile.containsHeader("Foo"), false);
assertEquals(csvFile.rowCount(), 3);
assertEquals(csvFile.row(0).lineNumber(), 1);
assertEquals(csvFile.row(1).lineNumber(), 2);
Expand All @@ -147,6 +150,8 @@ public void test_of_simple_no_header_tabs() {

public void test_of_simple_with_header() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1), true);
assertEquals(csvFile.containsHeader("Foo"), false);
assertEquals(csvFile.containsHeader("h1"), true);
ImmutableList<String> headers = csvFile.headers();
assertEquals(headers.size(), 2);
assertEquals(headers.get(0), "h1");
Expand Down Expand Up @@ -221,6 +226,8 @@ public void test_of_simple_with_header() {
public void test_of_duplicate_headers() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV5), true);
assertEquals(csvFile.headers(), ImmutableList.of("a", "b", "c", "b", "c"));
assertEquals(csvFile.containsHeader("Foo"), false);
assertEquals(csvFile.containsHeader("a"), true);
assertEquals(csvFile.row(0).getField("a"), "aa");
assertEquals(csvFile.row(0).getField("b"), "b1");
assertEquals(csvFile.row(0).getField("c"), "c1");
Expand Down

0 comments on commit 97397b0

Please sign in to comment.