Skip to content

Commit fd7629a

Browse files
committed
Small changes to how CsvFileDefinition is provided as an argument.
1 parent 2a00887 commit fd7629a

File tree

5 files changed

+26
-26
lines changed

5 files changed

+26
-26
lines changed

CHANGELOG.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased/Snapshot]
88

9+
### Added
10+
- Enhancing `VoltageLevel` with `equals` method [#1063](https://github.com/ie3-institute/PowerSystemDataModel/issues/1063)
11+
12+
### Fixed
13+
14+
15+
### Changed
16+
- Refactor `CsvFileConnector` and `CsvDataSource` [#1007](https://github.com/ie3-institute/PowerSystemDataModel/issues/1007)
17+
18+
919
## [5.0.1] - 2024-03-07
1020

1121
### Fixed
@@ -23,7 +33,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2333
- Added test for invalid input data in `CsvRawGridSource` [#1021](https://github.com/ie3-institute/PowerSystemDataModel/issues/1021)
2434
- Added `CsvThermalGridSource` [#1009](https://github.com/ie3-institute/PowerSystemDataModel/issues/1009)
2535
- Enhance documentation for CSV timeseries [#825](https://github.com/ie3-institute/PowerSystemDataModel/issues/825)
26-
- Enhancing `VoltageLevel` with `equals` method [#1063](https://github.com/ie3-institute/PowerSystemDataModel/issues/1063)
2736

2837
### Fixed
2938
- Fixed Couchbase integration tests that randomly failed [#755](https://github.com/ie3-institute/PowerSystemDataModel/issues/755)
@@ -51,7 +60,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5160
- Removing `uuid` as required column from input and result time series [#826](https://github.com/ie3-institute/PowerSystemDataModel/issues/826)
5261
- Removing the support for the old csv format that was marked `deprecated` back in version `1.1.0` [#795](https://github.com/ie3-institute/PowerSystemDataModel/issues/795)
5362
- BREAKING: Updating PowerSystemUtils dependency to 2.2 [#1006](https://github.com/ie3-institute/PowerSystemDataModel/issues/1006)
54-
- Refactor `CsvFileConnector` and `CsvDataSource` [#1007](https://github.com/ie3-institute/PowerSystemDataModel/issues/1007)
5563

5664
## [4.1.0] - 2023-11-02
5765

src/main/java/edu/ie3/datamodel/io/connectors/CsvFileConnector.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
package edu.ie3.datamodel.io.connectors;
77

88
import edu.ie3.datamodel.exceptions.ConnectorException;
9-
import edu.ie3.datamodel.exceptions.FileException;
109
import edu.ie3.datamodel.io.IoUtil;
1110
import edu.ie3.datamodel.io.csv.BufferedCsvWriter;
1211
import edu.ie3.datamodel.io.csv.CsvFileDefinition;
1312
import edu.ie3.datamodel.models.Entity;
1413
import edu.ie3.datamodel.models.timeseries.TimeSeries;
1514
import edu.ie3.datamodel.models.timeseries.TimeSeriesEntry;
1615
import edu.ie3.datamodel.models.value.Value;
17-
import edu.ie3.datamodel.utils.Try.TrySupplier;
1816
import java.io.*;
1917
import java.nio.charset.StandardCharsets;
2018
import java.nio.file.Path;
@@ -48,39 +46,37 @@ public Path getBaseDirectory() {
4846
}
4947

5048
public synchronized BufferedCsvWriter getOrInitWriter(
51-
Class<? extends Entity> clz, TrySupplier<CsvFileDefinition, FileException> supplier)
52-
throws ConnectorException {
49+
Class<? extends Entity> clz, CsvFileDefinition fileDefinition) throws ConnectorException {
5350
/* Try to the right writer */
5451
BufferedCsvWriter predefinedWriter = entityWriters.get(clz);
5552
if (predefinedWriter != null) return predefinedWriter;
5653

5754
/* If it is not available, build and register one */
5855
try {
59-
BufferedCsvWriter newWriter = initWriter(baseDirectory, supplier.get());
56+
BufferedCsvWriter newWriter = initWriter(baseDirectory, fileDefinition);
6057

6158
entityWriters.put(clz, newWriter);
6259
return newWriter;
63-
} catch (ConnectorException | FileException | IOException e) {
60+
} catch (ConnectorException | IOException e) {
6461
throw new ConnectorException(
6562
"Can neither find suitable writer nor build the correct one in CsvFileConnector.", e);
6663
}
6764
}
6865

6966
public synchronized <T extends TimeSeries<E, V>, E extends TimeSeriesEntry<V>, V extends Value>
70-
BufferedCsvWriter getOrInitWriter(
71-
T timeSeries, TrySupplier<CsvFileDefinition, FileException> supplier)
67+
BufferedCsvWriter getOrInitWriter(T timeSeries, CsvFileDefinition fileDefinition)
7268
throws ConnectorException {
7369
/* Try to the right writer */
7470
BufferedCsvWriter predefinedWriter = timeSeriesWriters.get(timeSeries.getUuid());
7571
if (predefinedWriter != null) return predefinedWriter;
7672

7773
/* If it is not available, build and register one */
7874
try {
79-
BufferedCsvWriter newWriter = initWriter(baseDirectory, supplier.get());
75+
BufferedCsvWriter newWriter = initWriter(baseDirectory, fileDefinition);
8076

8177
timeSeriesWriters.put(timeSeries.getUuid(), newWriter);
8278
return newWriter;
83-
} catch (ConnectorException | FileException | IOException e) {
79+
} catch (ConnectorException | IOException e) {
8480
throw new ConnectorException(
8581
"Can neither find suitable writer nor build the correct one in CsvFileConnector.", e);
8682
}
@@ -127,8 +123,7 @@ public synchronized void closeTimeSeriesWriter(UUID uuid) throws IOException {
127123
Optional<BufferedCsvWriter> maybeWriter = Optional.ofNullable(timeSeriesWriters.get(uuid));
128124
if (maybeWriter.isPresent()) {
129125
log.debug("Remove reference to time series writer for UUID '{}'.", uuid);
130-
timeSeriesWriters.remove(uuid);
131-
maybeWriter.get().close();
126+
timeSeriesWriters.remove(uuid).close();
132127
} else {
133128
log.warn("No writer found for time series '{}'.", uuid);
134129
}
@@ -145,8 +140,7 @@ public synchronized <C extends Entity> void closeEntityWriter(Class<C> clz) thro
145140
Optional<BufferedCsvWriter> maybeWriter = Optional.ofNullable(entityWriters.get(clz));
146141
if (maybeWriter.isPresent()) {
147142
log.debug("Remove reference to entity writer for class '{}'.", clz);
148-
entityWriters.remove(clz);
149-
maybeWriter.get().close();
143+
entityWriters.remove(clz).close();
150144
} else {
151145
log.warn("No writer found for class '{}'.", clz);
152146
}

src/main/java/edu/ie3/datamodel/io/sink/CsvFileSink.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ public <E extends TimeSeriesEntry<V>, V extends Value> void persistTimeSeries(
251251
BufferedCsvWriter writer =
252252
connector.getOrInitWriter(
253253
timeSeries,
254-
() -> new CsvFileDefinition(timeSeries, headerElements, csvSep, fileNamingStrategy));
254+
new CsvFileDefinition(timeSeries, headerElements, csvSep, fileNamingStrategy));
255255
persistTimeSeries(timeSeries, writer);
256256
connector.closeTimeSeriesWriter(timeSeries.getUuid());
257257
} catch (ProcessorProviderException e) {
258258
log.error(
259259
"Exception occurred during receiving of header elements. Cannot write this element.", e);
260-
} catch (ConnectorException e) {
260+
} catch (ConnectorException | FileException e) {
261261
log.error("Exception occurred during acquisition of writer.", e);
262262
} catch (IOException e) {
263263
log.error("Exception occurred during closing of writer.", e);
@@ -299,14 +299,12 @@ private <C extends Entity> void write(C entity) {
299299
BufferedCsvWriter writer =
300300
connector.getOrInitWriter(
301301
entity.getClass(),
302-
() ->
303-
new CsvFileDefinition(
304-
entity.getClass(), headerElements, csvSep, fileNamingStrategy));
302+
new CsvFileDefinition(entity.getClass(), headerElements, csvSep, fileNamingStrategy));
305303
writer.write(entityFieldData);
306304
} catch (ProcessorProviderException e) {
307305
log.error(
308306
"Exception occurred during receiving of header elements. Cannot write this element.", e);
309-
} catch (ConnectorException e) {
307+
} catch (ConnectorException | FileException e) {
310308
log.error("Exception occurred during retrieval of writer. Cannot write this element.", e);
311309
} catch (IOException e) {
312310
log.error("Exception occurred during writing of this element. Cannot write this element.", e);

src/main/java/edu/ie3/datamodel/io/source/csv/CsvDataSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public class CsvDataSource implements DataSource {
4848

4949
private final FileNamingStrategy fileNamingStrategy;
5050

51-
public CsvDataSource(String csvSep, Path folderPath, FileNamingStrategy fileNamingStrategy) {
51+
public CsvDataSource(String csvSep, Path directoryPath, FileNamingStrategy fileNamingStrategy) {
5252
this.csvSep = csvSep;
53-
this.connector = new CsvFileConnector(folderPath);
53+
this.connector = new CsvFileConnector(directoryPath);
5454
this.fileNamingStrategy = fileNamingStrategy;
5555
}
5656

src/test/groovy/edu/ie3/datamodel/io/connectors/CsvFileConnectorTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class CsvFileConnectorTest extends Specification {
7070

7171
when:
7272
/* The head line is of no interest here */
73-
connector.getOrInitWriter(NodeInput, () -> new CsvFileDefinition(NodeInput, [] as String[], ",", fileNamingStrategy))
73+
connector.getOrInitWriter(NodeInput, new CsvFileDefinition(NodeInput, [] as String[], ",", fileNamingStrategy))
7474

7575
then:
7676
noExceptionThrown()
@@ -89,7 +89,7 @@ class CsvFileConnectorTest extends Specification {
8989

9090
when:
9191
/* The head line is of no interest here */
92-
connector.getOrInitWriter(NodeInput, () -> new CsvFileDefinition(NodeInput, [] as String[], ",", fileNamingStrategy))
92+
connector.getOrInitWriter(NodeInput, new CsvFileDefinition(NodeInput, [] as String[], ",", fileNamingStrategy))
9393

9494
then:
9595
noExceptionThrown()

0 commit comments

Comments
 (0)