Skip to content

Commit 358a1b9

Browse files
committed
Write csv entries in correct order
1 parent 657d038 commit 358a1b9

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Fix JavaDoc creation
2424
- Create JavaDoc with java 17 instead of java 8
2525
- Let JavDoc pass, if there are warnings **ATTENTION:** Should be removed, when JavaDoc is fixed! (cf. Issue [#494](https://github.com/ie3-institute/PowerSystemDataModel/issues/494))
26+
- `BufferedCsvWriter` writes columns in the order, that the headline elements are defined [#434](https://github.com/ie3-institute/PowerSystemDataModel/issues/393)
2627

2728
### Changed
2829
- BREAKING: PvInput Model parameter name height changed to elevationAngle [#393](https://github.com/ie3-institute/PowerSystemDataModel/issues/393) :warning:

src/main/java/edu/ie3/datamodel/io/csv/BufferedCsvWriter.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ public class BufferedCsvWriter extends BufferedWriter {
2727
"Direct appending is prohibited. Use write instead.";
2828

2929
/**
30-
* Build a new CsvBufferedWriter
30+
* Build a new CsvBufferedWriter. The order of headline elements given in this constructor defines
31+
* the order of columns in file
3132
*
3233
* @param filePath String representation of the full path to the target file
33-
* @param headLineElements Elements of the csv head line
34+
* @param headLineElements Elements of the csv headline
3435
* @param csvSep csv separator char
3536
* @param append true to append to an existing file, false to overwrite an existing file (if any),
3637
* if no file exists, a new one will be created in both cases
@@ -47,7 +48,8 @@ public BufferedCsvWriter(
4748
/**
4849
* Build a new CsvBufferedWriter. This is a "convenience" Constructor. The absolute file path is
4950
* assembled by concatenation of {@code baseFolder} and {@code fileDefinition}'s file path
50-
* information.
51+
* information. The order of headline elements in {@code fileDefinition} defines the order of
52+
* columns in file
5153
*
5254
* @param baseFolder Base folder, from where the file hierarchy should start
5355
* @param fileDefinition The foreseen shape of the file
@@ -73,15 +75,16 @@ public BufferedCsvWriter(String baseFolder, CsvFileDefinition fileDefinition, bo
7375
*/
7476
public synchronized void write(Map<String, String> entityFieldData)
7577
throws IOException, SinkException {
76-
/* Check against eligible head line elements */
78+
/* Check against eligible headline elements */
7779
if (entityFieldData.size() != headLineElements.length
7880
|| !entityFieldData.keySet().containsAll(Arrays.asList(headLineElements)))
7981
throw new SinkException(
8082
"The provided data does not meet the pre-defined head line elements '"
8183
+ String.join(",", headLineElements)
8284
+ "'.");
8385

84-
String[] entries = entityFieldData.values().toArray(new String[0]);
86+
String[] entries =
87+
Arrays.stream(headLineElements).map(entityFieldData::get).toArray(String[]::new);
8588
writeOneLine(entries);
8689
}
8790

src/test/groovy/edu/ie3/datamodel/io/csv/BufferedCsvWriterTest.groovy

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package edu.ie3.datamodel.io.csv
77

8+
import edu.ie3.datamodel.exceptions.FileException
89
import edu.ie3.datamodel.exceptions.SinkException
910
import edu.ie3.util.io.FileIOUtils
1011
import org.apache.commons.io.FilenameUtils
@@ -78,4 +79,33 @@ class BufferedCsvWriterTest extends Specification {
7879
def e = thrown(SinkException)
7980
e.message == "The provided data does not meet the pre-defined head line elements 'a,b,c'."
8081
}
82+
83+
def "The buffered csv writer writes out content in the order specified by the headline elements"() {
84+
given:
85+
def targetFile = FilenameUtils.concat(tmpDirectory.toString(), "order_test.csv")
86+
def writer = new BufferedCsvWriter(targetFile, ["c", "b", "a"] as String[], ",", false)
87+
writer.writeFileHeader()
88+
def content = [
89+
"c": "z",
90+
"a": "x",
91+
"b": "y"
92+
]
93+
94+
when:
95+
writer.write(content)
96+
writer.close()
97+
/* Read in the content */
98+
def writtenContent = ""
99+
def headline = ""
100+
try(BufferedReader reader = new BufferedReader(new FileReader(targetFile))) {
101+
headline = reader.readLine()
102+
writtenContent = reader.readLine()
103+
} catch (Exception e) {
104+
throw new FileException("Unable to read content of test file '"+targetFile+"'.", e)
105+
}
106+
107+
then:
108+
headline == "c,b,a"
109+
writtenContent == "z,y,x"
110+
}
81111
}

0 commit comments

Comments
 (0)