2020import java .nio .charset .StandardCharsets ;
2121import java .nio .file .Files ;
2222import java .nio .file .Path ;
23- import java .nio .file .Paths ;
2423import java .util .*;
2524import java .util .function .Function ;
2625import java .util .stream .Collectors ;
2726import java .util .stream .Stream ;
28- import org .apache .commons .io .FilenameUtils ;
2927import org .slf4j .Logger ;
3028import org .slf4j .LoggerFactory ;
3129
@@ -44,11 +42,11 @@ public class CsvFileConnector implements DataConnector {
4442 private final Map <UUID , BufferedCsvWriter > timeSeriesWriters = new HashMap <>();
4543
4644 private final FileNamingStrategy fileNamingStrategy ;
47- private final String baseDirectoryName ;
45+ private final Path baseDirectoryName ;
4846
4947 private static final String FILE_ENDING = ".csv" ;
5048
51- public CsvFileConnector (String baseDirectoryName , FileNamingStrategy fileNamingStrategy ) {
49+ public CsvFileConnector (Path baseDirectoryName , FileNamingStrategy fileNamingStrategy ) {
5250 this .baseDirectoryName = baseDirectoryName ;
5351 this .fileNamingStrategy = fileNamingStrategy ;
5452 }
@@ -102,16 +100,15 @@ BufferedCsvWriter getOrInitWriter(T timeSeries, String[] headerElements, String
102100 * @throws ConnectorException If the base folder is a file
103101 * @throws IOException If the writer cannot be initialized correctly
104102 */
105- private BufferedCsvWriter initWriter (String baseDirectory , CsvFileDefinition fileDefinition )
103+ private BufferedCsvWriter initWriter (Path baseDirectory , CsvFileDefinition fileDefinition )
106104 throws ConnectorException , IOException {
107105 /* Join the full DIRECTORY path (excluding file name) */
108- String baseDirectoryHarmonized = IoUtil .harmonizeFileSeparator (baseDirectory );
109- String fullDirectoryPath =
110- FilenameUtils .concat (baseDirectoryHarmonized , fileDefinition .directoryPath ());
111- String fullPath = FilenameUtils .concat (baseDirectoryHarmonized , fileDefinition .getFilePath ());
106+ Path baseDirectoryHarmonized = IoUtil .harmonizeFileSeparator (baseDirectory );
107+ Path fullDirectoryPath = baseDirectoryHarmonized .resolve (fileDefinition .getDirectoryPath ());
108+ Path fullPath = baseDirectoryHarmonized .resolve (fileDefinition .getFilePath ());
112109
113110 /* Create missing directories */
114- File directories = new File ( fullDirectoryPath );
111+ File directories = fullDirectoryPath . toFile ( );
115112 if (directories .isFile ())
116113 throw new ConnectorException ("Directory '" + directories + "' already exists and is a file!" );
117114 if (!directories .exists () && !directories .mkdirs ())
@@ -169,10 +166,10 @@ public synchronized <C extends UniqueEntity> void closeEntityWriter(Class<C> clz
169166 * @return the reader that contains information about the file to be read in
170167 * @throws FileNotFoundException If the matching file cannot be found
171168 */
172- public BufferedReader initReader (Class <? extends UniqueEntity > clz ) throws FileNotFoundException {
173- String filePath = null ;
169+ public BufferedReader initReader (Class <? extends UniqueEntity > clz )
170+ throws FileNotFoundException , ConnectorException {
174171 try {
175- filePath =
172+ Path filePath =
176173 fileNamingStrategy
177174 .getFilePath (clz )
178175 .orElseThrow (
@@ -181,13 +178,11 @@ public BufferedReader initReader(Class<? extends UniqueEntity> clz) throws FileN
181178 "Cannot find a naming strategy for class '"
182179 + clz .getSimpleName ()
183180 + "'." ));
181+ return initReader (filePath );
184182 } catch (ConnectorException e ) {
185- log .error (
186- "Cannot get reader for entity '{}' as no file naming strategy for this file exists. Exception: {}" ,
187- clz .getSimpleName (),
188- e );
183+ throw new ConnectorException (
184+ "Cannot initialize reader for entity '" + clz .getSimpleName () + "'." , e );
189185 }
190- return initReader (filePath );
191186 }
192187
193188 /**
@@ -198,8 +193,8 @@ public BufferedReader initReader(Class<? extends UniqueEntity> clz) throws FileN
198193 * @return the reader that contains information about the file to be read in
199194 * @throws FileNotFoundException if no file with the provided file name can be found
200195 */
201- public BufferedReader initReader (String filePath ) throws FileNotFoundException {
202- File fullPath = new File ( baseDirectoryName + File . separator + filePath + FILE_ENDING );
196+ public BufferedReader initReader (Path filePath ) throws FileNotFoundException {
197+ File fullPath = baseDirectoryName . resolve ( filePath . toString () + FILE_ENDING ). toFile ( );
203198 return new BufferedReader (
204199 new InputStreamReader (new FileInputStream (fullPath ), StandardCharsets .UTF_8 ), 16384 );
205200 }
@@ -219,9 +214,9 @@ public BufferedReader initReader(String filePath) throws FileNotFoundException {
219214 filePath -> {
220215 /* Extract meta information from file path and enhance it with the file path itself */
221216 IndividualTimeSeriesMetaInformation metaInformation =
222- fileNamingStrategy .individualTimeSeriesMetaInformation (filePath );
217+ fileNamingStrategy .individualTimeSeriesMetaInformation (filePath . toString () );
223218 return new CsvIndividualTimeSeriesMetaInformation (
224- metaInformation , FileNamingStrategy .removeFileNameEnding (filePath ));
219+ metaInformation , FileNamingStrategy .removeFileNameEnding (filePath . getFileName () ));
225220 })
226221 .filter (
227222 metaInformation ->
@@ -238,23 +233,20 @@ public BufferedReader initReader(String filePath) throws FileNotFoundException {
238233 *
239234 * @return A set of relative paths to time series files, with respect to the base folder path
240235 */
241- private Set <String > getIndividualTimeSeriesFilePaths () {
242- Path baseDirectoryPath =
243- Paths .get (
244- FilenameUtils .getFullPath (baseDirectoryName )
245- + FilenameUtils .getName (baseDirectoryName ));
236+ private Set <Path > getIndividualTimeSeriesFilePaths () {
237+ Path baseDirectoryPath = baseDirectoryName .resolve (baseDirectoryName );
246238 try (Stream <Path > pathStream = Files .walk (baseDirectoryPath )) {
247239 return pathStream
248240 .map (baseDirectoryPath ::relativize )
249241 .filter (
250242 path -> {
251- String withoutEnding = FileNamingStrategy .removeFileNameEnding (path .toString ());
243+ Path withoutEnding =
244+ Path .of (FileNamingStrategy .removeFileNameEnding (path .toString ()));
252245 return fileNamingStrategy
253246 .getIndividualTimeSeriesPattern ()
254- .matcher (withoutEnding )
247+ .matcher (withoutEnding . toString () )
255248 .matches ();
256249 })
257- .map (Path ::toString )
258250 .collect (Collectors .toSet ());
259251 } catch (IOException e ) {
260252 log .error ("Unable to determine time series files readers for time series." , e );
@@ -270,7 +262,7 @@ private Set<String> getIndividualTimeSeriesFilePaths() {
270262 * @throws FileNotFoundException If the file is not present
271263 */
272264 public BufferedReader initIdCoordinateReader () throws FileNotFoundException {
273- String filePath = fileNamingStrategy .getIdCoordinateEntityName ();
265+ Path filePath = Path . of ( fileNamingStrategy .getIdCoordinateEntityName () );
274266 return initReader (filePath );
275267 }
276268
@@ -286,7 +278,7 @@ public BufferedReader initIdCoordinateReader() throws FileNotFoundException {
286278 private <T extends TimeSeries <E , V >, E extends TimeSeriesEntry <V >, V extends Value >
287279 CsvFileDefinition buildFileDefinition (T timeSeries , String [] headLineElements , String csvSep )
288280 throws ConnectorException {
289- String directoryPath = fileNamingStrategy .getDirectoryPath (timeSeries ).orElse ("" );
281+ Path directoryPath = fileNamingStrategy .getDirectoryPath (timeSeries ).orElse (Path . of ( "" ) );
290282 String fileName =
291283 fileNamingStrategy
292284 .getEntityName (timeSeries )
@@ -309,7 +301,7 @@ CsvFileDefinition buildFileDefinition(T timeSeries, String[] headLineElements, S
309301 private CsvFileDefinition buildFileDefinition (
310302 Class <? extends UniqueEntity > clz , String [] headLineElements , String csvSep )
311303 throws ConnectorException {
312- String directoryPath = fileNamingStrategy .getDirectoryPath (clz ).orElse ("" );
304+ Path directoryPath = fileNamingStrategy .getDirectoryPath (clz ).orElse (Path . of ( "" ) );
313305 String fileName =
314306 fileNamingStrategy
315307 .getEntityName (clz )
0 commit comments