Skip to content

Commit

Permalink
Add CSV file header matching for multiple headers (#2098)
Browse files Browse the repository at this point in the history
* Add CSV file header matching for multiple headers

* Ignore temporary tests

Useful when investigating forum posts
  • Loading branch information
jodastephen authored and cjkent committed Oct 23, 2019
1 parent fd6a36c commit 46b153e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,6 +16,7 @@ tests/classes/
tests/output/
test-output/
.DS_Store
TempTest.java

# IntelliJ config files
*.iws
Expand Down
Expand Up @@ -9,6 +9,7 @@
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -470,24 +471,36 @@ public CsvRow row(int index) {
}

/**
* Checks if the header is known.
* Checks if the header is present in the file.
* <p>
* Matching is case insensitive.
*
* @param header the column header to match
* @return true if the header is known
* @return true if the header is present
*/
public boolean containsHeader(String header) {
return searchHeaders.containsKey(header.toLowerCase(Locale.ENGLISH));
}

/**
* Checks if the header pattern is known.
* Checks if the headers are present in the file.
* <p>
* Matching is case insensitive.
*
* @param headers the column headers to match
* @return true if all the headers are present
*/
public boolean containsHeaders(Collection<String> headers) {
return headers.stream().allMatch(this::containsHeader);
}

/**
* Checks if the header pattern is present in the file.
* <p>
* Matching is case insensitive.
*
* @param headerPattern the header pattern to match
* @return true if the header is known
* @return true if the header is present
*/
public boolean containsHeader(Pattern headerPattern) {
for (int i = 0; i < headers.size(); i++) {
Expand Down
Expand Up @@ -10,6 +10,7 @@
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -227,24 +228,36 @@ public ImmutableList<String> headers() {
}

/**
* Checks if the header is known.
* Checks if the header is present in the file.
* <p>
* Matching is case insensitive.
*
* @param header the column header to match
* @return true if the header is known
* @return true if the header is present
*/
public boolean containsHeader(String header) {
return searchHeaders.containsKey(header.toLowerCase(Locale.ENGLISH));
}

/**
* Checks if the header pattern is known.
* Checks if the headers are present in the file.
* <p>
* Matching is case insensitive.
*
* @param headers the column headers to match
* @return true if all the headers are present
*/
public boolean containsHeaders(Collection<String> headers) {
return headers.stream().allMatch(this::containsHeader);
}

/**
* Checks if the header pattern is present in the file.
* <p>
* Matching is case insensitive.
*
* @param headerPattern the header pattern to match
* @return true if the header is known
* @return true if the header is present
*/
public boolean containsHeader(Pattern headerPattern) {
for (int i = 0; i < headers.size(); i++) {
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.junit.jupiter.params.provider.MethodSource;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.CharSource;
import com.google.common.io.Files;

Expand Down Expand Up @@ -92,8 +93,10 @@ public void test_of_empty_no_header() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(""), false);
assertThat(csvFile.headers().size()).isEqualTo(0);
assertThat(csvFile.rowCount()).isEqualTo(0);
assertThat(csvFile.containsHeader("Foo")).isEqualTo(false);
assertThat(csvFile.containsHeader(Pattern.compile("Foo"))).isEqualTo(false);
assertThat(csvFile.containsHeader("Foo")).isFalse();
assertThat(csvFile.containsHeaders(ImmutableSet.of())).isTrue();
assertThat(csvFile.containsHeaders(ImmutableSet.of("foo"))).isFalse();
assertThat(csvFile.containsHeader(Pattern.compile("Foo"))).isFalse();
}

@Test
Expand All @@ -106,8 +109,8 @@ public void test_of_simple_no_header() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1), false);
assertThat(csvFile.headers().size()).isEqualTo(0);
assertThat(csvFile.rowCount()).isEqualTo(4);
assertThat(csvFile.containsHeader("Foo")).isEqualTo(false);
assertThat(csvFile.containsHeader(Pattern.compile("Foo"))).isEqualTo(false);
assertThat(csvFile.containsHeader("Foo")).isFalse();
assertThat(csvFile.containsHeader(Pattern.compile("Foo"))).isFalse();
assertThat(csvFile.row(0).lineNumber()).isEqualTo(1);
assertThat(csvFile.row(1).lineNumber()).isEqualTo(2);
assertThat(csvFile.row(2).lineNumber()).isEqualTo(3);
Expand Down Expand Up @@ -135,8 +138,8 @@ public void test_of_simple_no_header() {
public void test_of_simple_no_header_tabs() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1T), false, '\t');
assertThat(csvFile.headers().size()).isEqualTo(0);
assertThat(csvFile.containsHeader("Foo")).isEqualTo(false);
assertThat(csvFile.containsHeader(Pattern.compile("Foo"))).isEqualTo(false);
assertThat(csvFile.containsHeader("Foo")).isFalse();
assertThat(csvFile.containsHeader(Pattern.compile("Foo"))).isFalse();
assertThat(csvFile.rowCount()).isEqualTo(3);
assertThat(csvFile.row(0).lineNumber()).isEqualTo(1);
assertThat(csvFile.row(1).lineNumber()).isEqualTo(2);
Expand All @@ -159,9 +162,13 @@ public void test_of_simple_no_header_tabs() {
@Test
public void test_of_simple_with_header() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1), true);
assertThat(csvFile.containsHeader("Foo")).isEqualTo(false);
assertThat(csvFile.containsHeader("h1")).isEqualTo(true);
assertThat(csvFile.containsHeader(Pattern.compile("Foo"))).isEqualTo(false);
assertThat(csvFile.containsHeader("Foo")).isFalse();
assertThat(csvFile.containsHeader("h1")).isTrue();
assertThat(csvFile.containsHeaders(ImmutableSet.of())).isTrue();
assertThat(csvFile.containsHeaders(ImmutableSet.of("h1"))).isTrue();
assertThat(csvFile.containsHeaders(ImmutableSet.of("h1", "h2"))).isTrue();
assertThat(csvFile.containsHeaders(ImmutableSet.of("h1", "h2", "h3"))).isFalse();
assertThat(csvFile.containsHeader(Pattern.compile("Foo"))).isFalse();
assertThat(csvFile.containsHeader(Pattern.compile("h[0-9]"))).isTrue();
ImmutableList<String> headers = csvFile.headers();
assertThat(headers.size()).isEqualTo(2);
Expand Down Expand Up @@ -238,8 +245,8 @@ public void test_of_simple_with_header() {
public void test_of_duplicate_headers() {
CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV5), true);
assertThat(csvFile.headers()).isEqualTo(ImmutableList.of("a", "b", "c", "b", "c"));
assertThat(csvFile.containsHeader("Foo")).isEqualTo(false);
assertThat(csvFile.containsHeader("a")).isEqualTo(true);
assertThat(csvFile.containsHeader("Foo")).isFalse();
assertThat(csvFile.containsHeader("a")).isTrue();
assertThat(csvFile.row(0).getField("a")).isEqualTo("aa");
assertThat(csvFile.row(0).getField("b")).isEqualTo("b1");
assertThat(csvFile.row(0).getField("c")).isEqualTo("c1");
Expand Down Expand Up @@ -556,21 +563,21 @@ public void test_equalsHashCodeToString() {
CsvFile b = CsvFile.of(CharSource.wrap(CSV2), true);
CsvFile c = CsvFile.of(CharSource.wrap(CSV3), false);
// file
assertThat(a1.equals(a1)).isEqualTo(true);
assertThat(a1.equals(a2)).isEqualTo(true);
assertThat(a1.equals(b)).isEqualTo(false);
assertThat(a1.equals(c)).isEqualTo(false);
assertThat(a1.equals(null)).isEqualTo(false);
assertThat(a1.equals(ANOTHER_TYPE)).isEqualTo(false);
assertThat(a1.equals(a1)).isTrue();
assertThat(a1.equals(a2)).isTrue();
assertThat(a1.equals(b)).isFalse();
assertThat(a1.equals(c)).isFalse();
assertThat(a1.equals(null)).isFalse();
assertThat(a1.equals(ANOTHER_TYPE)).isFalse();
assertThat(a1.hashCode()).isEqualTo(a2.hashCode());
assertThat(a1.toString()).isNotNull();
// row
assertThat(a1.row(0).equals(a1.row(0))).isEqualTo(true);
assertThat(a1.row(0).equals(a2.row(0))).isEqualTo(true);
assertThat(a1.row(0).equals(b.row(0))).isEqualTo(false);
assertThat(c.row(0).equals(c.row(1))).isEqualTo(false);
assertThat(a1.row(0).equals(ANOTHER_TYPE)).isEqualTo(false);
assertThat(a1.row(0).equals(null)).isEqualTo(false);
assertThat(a1.row(0).equals(a1.row(0))).isTrue();
assertThat(a1.row(0).equals(a2.row(0))).isTrue();
assertThat(a1.row(0).equals(b.row(0))).isFalse();
assertThat(c.row(0).equals(c.row(1))).isFalse();
assertThat(a1.row(0).equals(ANOTHER_TYPE)).isFalse();
assertThat(a1.row(0).equals(null)).isFalse();
assertThat(a1.row(0).hashCode()).isEqualTo(a2.row(0).hashCode());
assertThat(a1.row(0)).isNotNull();
}
Expand Down

0 comments on commit 46b153e

Please sign in to comment.