AndroidAPS/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java

52 lines
1.3 KiB
Java
Raw Normal View History

2017-06-02 12:27:21 +02:00
package info.nightscout.androidaps.data;
2017-05-10 14:00:46 +02:00
import android.support.annotation.Nullable;
2017-05-10 14:00:46 +02:00
2017-05-21 22:05:03 +02:00
import info.nightscout.androidaps.interfaces.Interval;
2017-05-10 14:00:46 +02:00
/**
* Created by adrian on 15/07/17.
2017-05-10 14:00:46 +02:00
*/
public class OverlappingIntervals<T extends Interval> extends Intervals<T> {
2017-05-10 14:00:46 +02:00
2018-03-26 19:46:18 +02:00
public OverlappingIntervals() {
super();
}
public OverlappingIntervals(Intervals<T> other) {
rawData = other.rawData.clone();
}
2017-07-19 17:38:20 +02:00
protected synchronized void merge() {
2017-07-15 12:29:51 +02:00
boolean needToCut = false;
long cutTime = 0;
2018-03-26 19:46:18 +02:00
for (int index = rawData.size() - 1; index >= 0; index--) { //begin with newest
2017-07-15 12:29:51 +02:00
Interval cur = rawData.valueAt(index);
2018-03-26 19:46:18 +02:00
if (cur.isEndingEvent()) {
2017-07-15 12:29:51 +02:00
needToCut = true;
cutTime = cur.start();
} else {
//event that is no EndingEvent might need to be stopped by an ending event
2018-03-26 19:46:18 +02:00
if (needToCut && cur.end() > cutTime) {
2017-07-15 12:29:51 +02:00
cur.cutEndTo(cutTime);
}
2017-05-10 14:00:46 +02:00
}
}
}
@Nullable
2017-07-19 17:38:20 +02:00
public synchronized T getValueByInterval(long time) {
2018-03-26 19:46:18 +02:00
for (int index = rawData.size() - 1; index >= 0; index--) { //begin with newest
2017-07-15 12:29:51 +02:00
T cur = rawData.valueAt(index);
2018-03-26 19:46:18 +02:00
if (cur.match(time)) {
2017-07-15 12:29:51 +02:00
return cur;
}
}
2017-05-10 14:00:46 +02:00
return null;
}
}