#5 -- parse date value csv file

This commit is contained in:
Anton Romanov 2021-06-25 16:46:02 +04:00
parent 88249612ec
commit 5b325a122a
3 changed files with 32 additions and 4 deletions

View File

@ -15,8 +15,10 @@ import ru.ulstu.db.model.TimeSeriesSet;
import ru.ulstu.service.UtilService; import ru.ulstu.service.UtilService;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
@ -56,7 +58,7 @@ public class DbFileService implements DbService {
String row; String row;
TimeSeries result = new TimeSeries(timeSeriesKey); TimeSeries result = new TimeSeries(timeSeriesKey);
while ((row = csvReader.readLine()) != null) { while ((row = csvReader.readLine()) != null) {
TimeSeries partOfTimeSeries = new UtilService().getTimeSeriesFromString(row); TimeSeries partOfTimeSeries = new UtilService().getTimeSeriesFromDateValueString(row);
if (partOfTimeSeries.getLength() > 0) { if (partOfTimeSeries.getLength() > 0) {
result.getValues().addAll(partOfTimeSeries.getValues()); result.getValues().addAll(partOfTimeSeries.getValues());
} }
@ -76,9 +78,11 @@ public class DbFileService implements DbService {
} }
@Override @Override
public boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) { public boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException {
validateTimeSeriesSet(timeSeriesSet); validateTimeSeriesSet(timeSeriesSet);
BufferedWriter writer = new BufferedWriter(new FileWriter(Paths.get(timeSeriesDbPath, timeSeriesSet.getKey()).toFile()));
writer.write(new UtilService().getTimeSeriesToDateValueString(timeSeries));
writer.close();
return true; return true;
} }

View File

@ -22,5 +22,5 @@ public interface DbService {
boolean addNewSet(String key); boolean addNewSet(String key);
boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries); boolean addTimeSeries(TimeSeriesSet timeSeriesSet, TimeSeries timeSeries) throws IOException;
} }

View File

@ -59,4 +59,28 @@ public class UtilService {
.map(v -> v.getValue().toString().replace("\\.", ",")) .map(v -> v.getValue().toString().replace("\\.", ","))
.collect(Collectors.joining(";")); .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<TimeSeriesValue> 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));
}
} }