Intervals -> kt

This commit is contained in:
Milos Kozak 2021-03-07 11:05:33 +01:00
parent 72176edfb9
commit 00ee60ff1e
3 changed files with 74 additions and 98 deletions

View file

@ -1,96 +0,0 @@
package info.nightscout.androidaps.data;
import androidx.annotation.Nullable;
import androidx.collection.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> {
LongSparseArray<T> rawData; // oldest at index 0
public Intervals() {
rawData = new LongSparseArray<T>();
}
public synchronized Intervals<T> reset() {
rawData = new LongSparseArray<T>();
return this;
}
protected abstract void merge();
/**
* The List must be sorted by `T.start()` in ascending order
*/
public synchronized void add(List<T> list) {
for (T interval : list) {
rawData.put(interval.start(), interval);
}
merge();
}
public synchronized void add(T interval) {
rawData.put(interval.start(), interval);
merge();
}
public synchronized List<T> getList() {
List<T> list = new ArrayList<>();
for (int i = 0; i < rawData.size(); i++)
list.add(rawData.valueAt(i));
return list;
}
public synchronized List<T> getReversedList() {
List<T> list = new ArrayList<>();
for (int i = rawData.size() - 1; i >= 0; i--)
list.add(rawData.valueAt(i));
return list;
}
protected synchronized int binarySearch(long value) {
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);
public synchronized int size() {
return rawData.size();
}
public synchronized T get(int index) {
return rawData.valueAt(index);
}
public synchronized T getReversed(int index) {
return rawData.valueAt(size() - 1 - index);
}
}

View file

@ -0,0 +1,72 @@
package info.nightscout.androidaps.data
import androidx.collection.LongSparseArray
import info.nightscout.androidaps.interfaces.Interval
import java.util.*
/**
* Created by mike on 09.05.2017.
*/
// Zero duration means end of interval
abstract class Intervals<T : Interval> {
var rawData: LongSparseArray<T> = LongSparseArray()// oldest at index 0
@Synchronized fun reset(): Intervals<T> {
rawData = LongSparseArray()
return this
}
protected abstract fun merge()
/**
* The List must be sorted by `T.start()` in ascending order
*/
@Synchronized fun add(list: List<T>) {
for (interval in list) {
rawData.put(interval.start(), interval)
}
merge()
}
@Synchronized fun add(interval: T) {
rawData.put(interval.start(), interval)
merge()
}
@get:Synchronized val list: List<T>
get() {
val list: MutableList<T> = ArrayList()
for (i in 0 until rawData.size()) list.add(rawData.valueAt(i))
return list
}
@get:Synchronized val reversedList: List<T>
get() {
val list: MutableList<T> = ArrayList()
for (i in rawData.size() - 1 downTo 0) list.add(rawData.valueAt(i))
return list
}
@Synchronized protected fun binarySearch(value: Long): Int {
var lo = 0
var hi = rawData.size() - 1
while (lo <= hi) {
val mid = lo + hi ushr 1
val midVal: Interval = rawData.valueAt(mid)
when {
midVal.before(value) -> lo = mid + 1
midVal.after(value) -> hi = mid - 1
midVal.match(value) -> return mid // value found
}
}
return lo.inv() // value not present
}
abstract fun getValueByInterval(time: Long): T?
@Synchronized fun size(): Int = rawData.size()
@Synchronized operator fun get(index: Int): T? = rawData.valueAt(index)
@Synchronized fun getReversed(index: Int): T? = rawData.valueAt(size() - 1 - index)
}

View file

@ -52,8 +52,8 @@ class OverlappingIntervalsTest {
someList.add(TempTargetTest().date(startDate + T.hours(1).msecs()).duration(T.hours(1).mins()).low(100.0).high(100.0)) someList.add(TempTargetTest().date(startDate + T.hours(1).msecs()).duration(T.hours(1).mins()).low(100.0).high(100.0))
list.reset() list.reset()
list.add(someList) list.add(someList)
Assert.assertEquals(startDate, list[0].data.timestamp) Assert.assertEquals(startDate, list[0]?.data?.timestamp)
Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversed(0).data.timestamp) Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversed(0)?.data?.timestamp)
Assert.assertEquals(startDate + T.hours(1).msecs(), list.reversedList[0].data.timestamp) Assert.assertEquals(startDate + T.hours(1).msecs(), list.reversedList[0].data.timestamp)
} }
} }