Skip to content

Commit

Permalink
Addressing reviewers comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ckittl committed Mar 8, 2022
1 parent 358a1b9 commit 1e739b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
27 changes: 15 additions & 12 deletions src/main/java/edu/ie3/datamodel/io/csv/BufferedCsvWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* This class extends the {@link BufferedWriter} and adds information about the file shape of the
Expand Down Expand Up @@ -83,9 +85,7 @@ public synchronized void write(Map<String, String> entityFieldData)
+ String.join(",", headLineElements)
+ "'.");

String[] entries =
Arrays.stream(headLineElements).map(entityFieldData::get).toArray(String[]::new);
writeOneLine(entries);
writeOneLine(Arrays.stream(headLineElements).map(entityFieldData::get));
}

/**
Expand All @@ -104,15 +104,18 @@ public final synchronized void writeFileHeader() throws IOException {
* @throws IOException If writing is not possible
*/
private void writeOneLine(String[] entries) throws IOException {
for (int i = 0; i < entries.length; i++) {
String attribute = entries[i];
super.append(attribute);
if (i + 1 < entries.length) {
super.append(csvSep);
} else {
super.append("\n");
}
}
writeOneLine(Arrays.stream(entries));
}

/**
* Write one line to the csv file
*
* @param entries Stream of entries to write
* @throws IOException If writing is not possible
*/
private void writeOneLine(Stream<String> entries) throws IOException {
super.append(entries.collect(Collectors.joining(csvSep)));
super.append("\n");
flush();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ class BufferedCsvWriterTest extends Specification {
def "The buffered csv writer writes out content in the order specified by the headline elements"() {
given:
def targetFile = FilenameUtils.concat(tmpDirectory.toString(), "order_test.csv")
def writer = new BufferedCsvWriter(targetFile, ["c", "b", "a"] as String[], ",", false)
def writer = new BufferedCsvWriter(targetFile, ["third_header", "second_header", "first_header"] as String[], ",", false)
writer.writeFileHeader()
def content = [
"c": "z",
"a": "x",
"b": "y"
"third_header": "third_value",
"first_header": "first_value",
"second_header": "second_value"
]

when:
Expand All @@ -105,7 +105,7 @@ class BufferedCsvWriterTest extends Specification {
}

then:
headline == "c,b,a"
writtenContent == "z,y,x"
headline == "third_header,second_header,first_header"
writtenContent == "third_value,second_value,first_value"
}
}

0 comments on commit 1e739b5

Please sign in to comment.