The problem at hand is to return the change between the first and last known measurement in a certain interval from a series of timestamp/value data. Also, I'd like to learn to use Java 8 Streams, so I'm trying to see if and how this could solve the problem.
A sample of the data:
DateTime,Value
...
1470012671,618.59
1470012912,618.62
1470013212,618.65
1470013512,618.68
1470013632,618.69
1470013900,618.71
...
Example input:
startMillis: 1470012800
endMillis: 1470013800
Expected answer (I choose the 'inner key values' when the start and end time are not present (see bonus question below)):
618.69 - 618.62 = 0.07
The code I have so far:
double amountKiloWattHours = 0;
long startMillis = startingTime.toEpochSecond();
long endMillis = startMillis + PERIOD_LENGTH_MILLIS;
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
// Currently, only returns all pairs within range...
Stream<Pair> pairs = stream
.skip(1)
.map(p -> {
return new Pair(p);
})
.filter(pair -> {
return (pair.getMillis() > startMillis) && (pair.getMillis() < endMillis);
});
} catch (Exception e) {
// TODO specify and handle exceptions...
}
.
public class Pair {
@Getter
private final long millis;
@Getter
private final double kWhs;
public Pair(String input) {
String[] parts = input.split(",");
this.millis = Long.parseLong(parts[0]);
this.kWhs = Double.parseDouble(parts[1]);
}
}
How do I now get the difference between the value of the last and the first pair in the interval?
Bonus question: How do I get the interpolated result where the exact timestamps' value is linearly interpolated between two surrounding values?