From 5b325a122aabe022a6303973141cbf01e47af25f Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 25 Jun 2021 16:46:02 +0400 Subject: [PATCH] #5 -- parse date value csv file --- src/main/java/ru/ulstu/db/DbFileService.java | 10 +++++--- src/main/java/ru/ulstu/db/DbService.java | 2 +- .../java/ru/ulstu/service/UtilService.java | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/ulstu/db/DbFileService.java b/src/main/java/ru/ulstu/db/DbFileService.java index a55e9e7..f5ef631 100644 --- a/src/main/java/ru/ulstu/db/DbFileService.java +++ b/src/main/java/ru/ulstu/db/DbFileService.java @@ -15,8 +15,10 @@ import ru.ulstu.db.model.TimeSeriesSet; import ru.ulstu.service.UtilService; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.nio.file.Paths; import java.util.ArrayList; @@ -56,7 +58,7 @@ public class DbFileService implements DbService { String row; TimeSeries result = new TimeSeries(timeSeriesKey); while ((row = csvReader.readLine()) != null) { - TimeSeries partOfTimeSeries = new UtilService().getTimeSeriesFromString(row); + TimeSeries partOfTimeSeries = new UtilService().getTimeSeriesFromDateValueString(row); if (partOfTimeSeries.getLength() > 0) { result.getValues().addAll(partOfTimeSeries.getValues()); } @@ -76,9 +78,11 @@ public class DbFileService implements DbService { } @Override - public boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) { + public boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException { validateTimeSeriesSet(timeSeriesSet); - + BufferedWriter writer = new BufferedWriter(new FileWriter(Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile())); + writer.write(new UtilService().getTimeSeriesToDateValueString(timeSeries)); + writer.close(); return true; } diff --git a/src/main/java/ru/ulstu/db/DbService.java b/src/main/java/ru/ulstu/db/DbService.java index 4fc121c..b5b58de 100644 --- a/src/main/java/ru/ulstu/db/DbService.java +++ b/src/main/java/ru/ulstu/db/DbService.java @@ -22,5 +22,5 @@ public interface DbService { boolean addNewSet(String key); - boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries); + boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException; } diff --git a/src/main/java/ru/ulstu/service/UtilService.java b/src/main/java/ru/ulstu/service/UtilService.java index 7351583..44d44ec 100644 --- a/src/main/java/ru/ulstu/service/UtilService.java +++ b/src/main/java/ru/ulstu/service/UtilService.java @@ -59,4 +59,28 @@ public class UtilService { .map(v -> v.getValue().toString().replace("\\.", ",")) .collect(Collectors.joining(";")); } + + public String getTimeSeriesToDateValueString(TimeSeries timeSeries) { + return timeSeries + .getValues() + .stream() + .map(v -> v.getDate().toString() + ";" + v.getValue().toString().replace("\\.", ",")) + .collect(Collectors.joining("\n")); + } + + public TimeSeries getTimeSeriesFromDateValueString(String tsString) { + List tsValues = Arrays.stream(tsString.split("\n")) + .filter(v -> { + try { + Double.parseDouble(v); + return true; + } catch (NumberFormatException e) { + return false; + } + }) + .map(Double::parseDouble) + .map(TimeSeriesValue::new) + .collect(Collectors.toList()); + return TimeSeriesUtils.fillDates(new TimeSeries(tsValues)); + } }