Til inspiration:
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
public class DataAnalysis {
public static class DataPoint {
private Timestamp timestamp;
private double value;
public DataPoint(Timestamp timestamp, double value) {
this.timestamp = timestamp;
this.value = value;
}
public DataPoint(long epochtime, double value) {
this(new Timestamp(epochtime), value);
}
public Timestamp getTimestamp() {
return timestamp;
}
public double getValue() {
return value;
}
}
public static class DataSet {
private List<DataPoint> datapoints = new ArrayList<>();
public void add(DataPoint dp) {
datapoints.add(dp);
}
public int size() {
return datapoints.size();
}
public DataPoint get(int ix) {
return datapoints.get(ix);
}
}
public static class DataAnalyzer {
private DataSet dataset;
private int interval;
public DataAnalyzer(DataSet dataset, int interval) {
this.dataset = dataset;
this.interval = interval;
}
public void process(BiConsumer<DataPoint, Double> f) {
int ix0 = 0;
for(int ix = 0; ix < dataset.size(); ix++) {
while(dataset.get(ix0).getTimestamp().getTime() < dataset.get(ix).getTimestamp().getTime() - interval) ix0++;
double avg = 0;
for(int i = ix0; i <= ix; i++) {
avg += dataset.get(i).getValue();
}
avg /= (ix - ix0 + 1);
f.accept(dataset.get(ix), avg);
}
}
}
public static void main(String[] args) {
long now = (new Date()).getTime();
DataSet ds = new DataSet();
ds.add(new DataPoint(now - 5000, 1.0));
ds.add(new DataPoint(now - 3000, 4.0));
ds.add(new DataPoint(now - 2000, 9.0));
ds.add(new DataPoint(now - 1000, 16.0));
ds.add(new DataPoint(now - 0, 25.0));
DataAnalyzer da = new DataAnalyzer(ds, 1500);
da.process((dp,avg) -> System.out.println(dp.getValue() + " " + avg));
}
}