2017-07-15 11:19:27 +02:00
|
|
|
package info.nightscout.androidaps.data;
|
|
|
|
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v4.util.LongSparseArray;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import info.nightscout.androidaps.interfaces.Interval;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mike on 09.05.2017.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Zero duration means end of interval
|
|
|
|
|
|
|
|
public abstract class Intervals<T extends Interval> {
|
|
|
|
|
2018-03-25 17:33:03 +02:00
|
|
|
LongSparseArray<T> rawData; // oldest at index 0
|
|
|
|
|
|
|
|
public Intervals() {
|
|
|
|
rawData = new LongSparseArray<T>();
|
|
|
|
}
|
|
|
|
|
2017-07-19 17:38:20 +02:00
|
|
|
public synchronized Intervals reset() {
|
2017-07-15 12:29:51 +02:00
|
|
|
rawData = new LongSparseArray<T>();
|
2017-07-15 11:19:27 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void merge();
|
|
|
|
|
2017-07-15 12:29:51 +02:00
|
|
|
/**
|
|
|
|
* The List must be sorted by `T.start()` in ascending order
|
2018-03-25 17:33:03 +02:00
|
|
|
*/
|
2017-07-19 17:38:20 +02:00
|
|
|
public synchronized void add(List<T> list) {
|
2017-07-15 11:19:27 +02:00
|
|
|
for (T interval : list) {
|
|
|
|
rawData.put(interval.start(), interval);
|
|
|
|
}
|
|
|
|
merge();
|
|
|
|
}
|
|
|
|
|
2018-03-26 19:46:18 +02:00
|
|
|
public synchronized void add(T interval) {
|
|
|
|
rawData.put(interval.start(), interval);
|
|
|
|
merge();
|
|
|
|
}
|
|
|
|
|
2017-07-15 11:19:27 +02:00
|
|
|
|
2017-07-19 17:38:20 +02:00
|
|
|
public synchronized List<T> getList() {
|
2017-07-15 11:19:27 +02:00
|
|
|
List<T> list = new ArrayList<>();
|
|
|
|
for (int i = 0; i < rawData.size(); i++)
|
|
|
|
list.add(rawData.valueAt(i));
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-07-19 17:38:20 +02:00
|
|
|
public synchronized List<T> getReversedList() {
|
2017-07-15 11:19:27 +02:00
|
|
|
List<T> list = new ArrayList<>();
|
2018-03-25 17:33:03 +02:00
|
|
|
for (int i = rawData.size() - 1; i >= 0; i--)
|
2017-07-15 11:19:27 +02:00
|
|
|
list.add(rawData.valueAt(i));
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-07-19 17:38:20 +02:00
|
|
|
protected synchronized int binarySearch(long value) {
|
2017-07-15 11:19:27 +02:00
|
|
|
int lo = 0;
|
|
|
|
int hi = rawData.size() - 1;
|
|
|
|
|
|
|
|
while (lo <= hi) {
|
|
|
|
final int mid = (lo + hi) >>> 1;
|
|
|
|
final Interval midVal = rawData.valueAt(mid);
|
|
|
|
|
|
|
|
if (midVal.before(value)) {
|
|
|
|
lo = mid + 1;
|
|
|
|
} else if (midVal.after(value)) {
|
|
|
|
hi = mid - 1;
|
|
|
|
} else if (midVal.match(value)) {
|
|
|
|
return mid; // value found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ~lo; // value not present
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract T getValueByInterval(long time);
|
|
|
|
|
2017-07-19 17:38:20 +02:00
|
|
|
public synchronized int size() {
|
2017-07-15 11:19:27 +02:00
|
|
|
return rawData.size();
|
|
|
|
}
|
|
|
|
|
2017-07-19 17:38:20 +02:00
|
|
|
public synchronized T get(int index) {
|
2017-07-15 11:19:27 +02:00
|
|
|
return rawData.valueAt(index);
|
|
|
|
}
|
|
|
|
|
2017-07-19 17:38:20 +02:00
|
|
|
public synchronized T getReversed(int index) {
|
2017-07-15 11:19:27 +02:00
|
|
|
return rawData.valueAt(size() - 1 - index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|