TT refactor OverlappingIntervals to two classes with an abstract superclass Intervals

This commit is contained in:
AdrianLxM 2017-07-15 11:19:27 +02:00
parent 0e6a93df90
commit 9eaba98dfa
9 changed files with 152 additions and 104 deletions

View file

@ -0,0 +1,91 @@
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> {
LongSparseArray<T> rawData = new LongSparseArray<>(); // oldest at index 0
public Intervals reset() {
rawData = new LongSparseArray<>();
return this;
}
public void add(T newInterval) {
rawData.put(newInterval.start(), newInterval);
merge();
}
protected abstract void merge();
public void add(List<T> list) {
for (T interval : list) {
rawData.put(interval.start(), interval);
}
merge();
}
public List<T> getList() {
List<T> list = new ArrayList<>();
for (int i = 0; i < rawData.size(); i++)
list.add(rawData.valueAt(i));
return list;
}
public 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 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 int size() {
return rawData.size();
}
public T get(int index) {
return rawData.valueAt(index);
}
public T getReversed(int index) {
return rawData.valueAt(size() - 1 - index);
}
}

View file

@ -0,0 +1,30 @@
package info.nightscout.androidaps.data;
import android.support.annotation.Nullable;
import info.nightscout.androidaps.interfaces.Interval;
/**
* Created by adrian on 15/07/17.
*/
public class NonOverlappingIntervals<T extends Interval> extends Intervals<T> {
protected void merge() {
for (int index = 0; index < rawData.size() - 1; index++) {
Interval i = rawData.valueAt(index);
long startOfNewer = rawData.valueAt(index + 1).start();
if (i.originalEnd() > startOfNewer) {
i.cutEndTo(startOfNewer);
}
}
}
@Nullable
public T getValueByInterval(long time) {
int index = binarySearch(time);
if (index >= 0) return rawData.valueAt(index);
return null;
}
}

View file

@ -1,41 +1,17 @@
package info.nightscout.androidaps.data; package info.nightscout.androidaps.data;
import android.support.annotation.Nullable;
import android.support.v4.util.LongSparseArray;
import java.util.ArrayList; import android.support.annotation.Nullable;
import java.util.List;
import info.nightscout.androidaps.interfaces.Interval; import info.nightscout.androidaps.interfaces.Interval;
/** /**
* Created by mike on 09.05.2017. * Created by adrian on 15/07/17.
*/ */
// Zero duration means end of interval public class OverlappingIntervals<T extends Interval> extends Intervals<T> {
public class OverlappingIntervals<T extends Interval> { protected void merge() {
private LongSparseArray<T> rawData = new LongSparseArray<>(); // oldest at index 0
public OverlappingIntervals reset() {
rawData = new LongSparseArray<>();
return this;
}
public void add(T newInterval) {
rawData.put(newInterval.start(), newInterval);
merge();
}
public void add(List<T> list) {
for (T interval : list) {
rawData.put(interval.start(), interval);
}
merge();
}
private void merge() {
for (int index = 0; index < rawData.size() - 1; index++) { for (int index = 0; index < rawData.size() - 1; index++) {
Interval i = rawData.valueAt(index); Interval i = rawData.valueAt(index);
long startOfNewer = rawData.valueAt(index + 1).start(); long startOfNewer = rawData.valueAt(index + 1).start();
@ -46,54 +22,10 @@ public class OverlappingIntervals<T extends Interval> {
} }
@Nullable @Nullable
public Interval getValueByInterval(long time) { public T getValueByInterval(long time) {
int index = binarySearch(time); int index = binarySearch(time);
if (index >= 0) return rawData.valueAt(index); if (index >= 0) return rawData.valueAt(index);
return null; return null;
} }
public List<T> getList() {
List<T> list = new ArrayList<>();
for (int i = 0; i < rawData.size(); i++)
list.add(rawData.valueAt(i));
return list;
}
public List<T> getReversedList() {
List<T> list = new ArrayList<>();
for (int i = rawData.size() -1; i>=0; i--)
list.add(rawData.valueAt(i));
return list;
}
private 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 int size() {
return rawData.size();
}
public T get(int index) {
return rawData.valueAt(index);
}
public T getReversed(int index) {
return rawData.valueAt(size() - 1 - index);
}
} }

View file

@ -10,7 +10,7 @@ import info.nightscout.androidaps.db.ProfileSwitch;
import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.db.Treatment;
import info.nightscout.androidaps.data.OverlappingIntervals; import info.nightscout.androidaps.data.Intervals;
import info.nightscout.androidaps.data.ProfileIntervals; import info.nightscout.androidaps.data.ProfileIntervals;
/** /**
@ -42,18 +42,18 @@ public interface TreatmentsInterface {
TemporaryBasal getTempBasalFromHistory(long time); TemporaryBasal getTempBasalFromHistory(long time);
double getTempBasalAbsoluteRateHistory(); double getTempBasalAbsoluteRateHistory();
double getTempBasalRemainingMinutesFromHistory(); double getTempBasalRemainingMinutesFromHistory();
OverlappingIntervals<TemporaryBasal> getTemporaryBasalsFromHistory(); Intervals<TemporaryBasal> getTemporaryBasalsFromHistory();
boolean isInHistoryExtendedBoluslInProgress(); boolean isInHistoryExtendedBoluslInProgress();
ExtendedBolus getExtendedBolusFromHistory(long time); ExtendedBolus getExtendedBolusFromHistory(long time);
OverlappingIntervals<ExtendedBolus> getExtendedBolusesFromHistory(); Intervals<ExtendedBolus> getExtendedBolusesFromHistory();
boolean addToHistoryExtendedBolus(ExtendedBolus extendedBolus); boolean addToHistoryExtendedBolus(ExtendedBolus extendedBolus);
boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo); boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo);
TempTarget getTempTargetFromHistory(long time); TempTarget getTempTargetFromHistory(long time);
OverlappingIntervals<TempTarget> getTempTargetsFromHistory(); Intervals<TempTarget> getTempTargetsFromHistory();
ProfileSwitch getProfileSwitchFromHistory(long time); ProfileSwitch getProfileSwitchFromHistory(long time);
ProfileIntervals<ProfileSwitch> getProfileSwitchesFromHistory(); ProfileIntervals<ProfileSwitch> getProfileSwitchesFromHistory();

View file

@ -23,7 +23,7 @@ import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.data.MealData;
import info.nightscout.androidaps.data.OverlappingIntervals; import info.nightscout.androidaps.data.Intervals;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.ProfileIntervals; import info.nightscout.androidaps.data.ProfileIntervals;
import info.nightscout.androidaps.data.PumpEnactResult; import info.nightscout.androidaps.data.PumpEnactResult;
@ -827,7 +827,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
} }
@Override @Override
public OverlappingIntervals<TemporaryBasal> getTemporaryBasalsFromHistory() { public Intervals<TemporaryBasal> getTemporaryBasalsFromHistory() {
return activeTreatments.getTemporaryBasalsFromHistory(); return activeTreatments.getTemporaryBasalsFromHistory();
} }
@ -874,7 +874,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
} }
@Override @Override
public OverlappingIntervals<ExtendedBolus> getExtendedBolusesFromHistory() { public Intervals<ExtendedBolus> getExtendedBolusesFromHistory() {
return activeTreatments.getExtendedBolusesFromHistory(); return activeTreatments.getExtendedBolusesFromHistory();
} }
@ -896,7 +896,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain
} }
@Override @Override
public OverlappingIntervals<TempTarget> getTempTargetsFromHistory() { public Intervals<TempTarget> getTempTargetsFromHistory() {
return activeTreatments.getTempTargetsFromHistory(); return activeTreatments.getTempTargetsFromHistory();
} }

View file

@ -8,7 +8,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import info.nightscout.androidaps.Config; import info.nightscout.androidaps.Config;
@ -19,7 +18,8 @@ import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.Iob; import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.IobTotal;
import info.nightscout.androidaps.data.MealData; import info.nightscout.androidaps.data.MealData;
import info.nightscout.androidaps.data.OverlappingIntervals; import info.nightscout.androidaps.data.Intervals;
import info.nightscout.androidaps.data.NonOverlappingIntervals;
import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.ProfileIntervals; import info.nightscout.androidaps.data.ProfileIntervals;
import info.nightscout.androidaps.db.ExtendedBolus; import info.nightscout.androidaps.db.ExtendedBolus;
@ -48,9 +48,9 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
public static IobTotal lastTempBasalsCalculation; public static IobTotal lastTempBasalsCalculation;
public static List<Treatment> treatments; public static List<Treatment> treatments;
private static OverlappingIntervals<TemporaryBasal> tempBasals = new OverlappingIntervals<>(); private static Intervals<TemporaryBasal> tempBasals = new NonOverlappingIntervals<TemporaryBasal>();
private static OverlappingIntervals<ExtendedBolus> extendedBoluses = new OverlappingIntervals<>(); private static Intervals<ExtendedBolus> extendedBoluses = new NonOverlappingIntervals<ExtendedBolus>();
private static OverlappingIntervals<TempTarget> tempTargets = new OverlappingIntervals<>(); private static Intervals<TempTarget> tempTargets = new NonOverlappingIntervals<TempTarget>();
private static ProfileIntervals<ProfileSwitch> profiles = new ProfileIntervals<>(); private static ProfileIntervals<ProfileSwitch> profiles = new ProfileIntervals<>();
private static boolean fragmentEnabled = true; private static boolean fragmentEnabled = true;
@ -347,7 +347,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
} }
@Override @Override
public OverlappingIntervals<ExtendedBolus> getExtendedBolusesFromHistory() { public Intervals<ExtendedBolus> getExtendedBolusesFromHistory() {
return extendedBoluses; return extendedBoluses;
} }
@ -380,7 +380,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
} }
@Override @Override
public OverlappingIntervals<TemporaryBasal> getTemporaryBasalsFromHistory() { public Intervals<TemporaryBasal> getTemporaryBasalsFromHistory() {
return tempBasals; return tempBasals;
} }
@ -442,7 +442,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface {
} }
@Override @Override
public OverlappingIntervals<TempTarget> getTempTargetsFromHistory() { public Intervals<TempTarget> getTempTargetsFromHistory() {
return tempTargets; return tempTargets;
} }

View file

@ -23,8 +23,6 @@ import com.squareup.otto.Subscribe;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.IobTotal;
@ -35,7 +33,7 @@ import info.nightscout.androidaps.events.EventNewBG;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.NSUpload; import info.nightscout.utils.NSUpload;
import info.nightscout.androidaps.data.OverlappingIntervals; import info.nightscout.androidaps.data.Intervals;
public class TreatmentsExtendedBolusesFragment extends Fragment { public class TreatmentsExtendedBolusesFragment extends Fragment {
@ -48,9 +46,9 @@ public class TreatmentsExtendedBolusesFragment extends Fragment {
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ExtendedBolusesViewHolder> { public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ExtendedBolusesViewHolder> {
OverlappingIntervals<ExtendedBolus> extendedBolusList; Intervals<ExtendedBolus> extendedBolusList;
RecyclerViewAdapter(OverlappingIntervals<ExtendedBolus> extendedBolusList) { RecyclerViewAdapter(Intervals<ExtendedBolus> extendedBolusList) {
this.extendedBolusList = extendedBolusList; this.extendedBolusList = extendedBolusList;
} }

View file

@ -26,11 +26,10 @@ import info.nightscout.androidaps.Services.Intents;
import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TempTarget;
import info.nightscout.androidaps.events.EventTempTargetChange; import info.nightscout.androidaps.events.EventTempTargetChange;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.NSUpload; import info.nightscout.utils.NSUpload;
import info.nightscout.androidaps.data.OverlappingIntervals; import info.nightscout.androidaps.data.Intervals;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
/** /**
@ -47,9 +46,9 @@ public class TreatmentsTempTargetFragment extends Fragment implements View.OnCli
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.TempTargetsViewHolder> { public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.TempTargetsViewHolder> {
OverlappingIntervals<TempTarget> tempTargetList; Intervals<TempTarget> tempTargetList;
RecyclerViewAdapter(OverlappingIntervals<TempTarget> TempTargetList) { RecyclerViewAdapter(Intervals<TempTarget> TempTargetList) {
this.tempTargetList = TempTargetList; this.tempTargetList = TempTargetList;
} }

View file

@ -23,8 +23,6 @@ import com.squareup.otto.Subscribe;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.Date;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.IobTotal; import info.nightscout.androidaps.data.IobTotal;
@ -35,7 +33,7 @@ import info.nightscout.androidaps.events.EventTempBasalChange;
import info.nightscout.utils.DateUtil; import info.nightscout.utils.DateUtil;
import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.DecimalFormatter;
import info.nightscout.utils.NSUpload; import info.nightscout.utils.NSUpload;
import info.nightscout.androidaps.data.OverlappingIntervals; import info.nightscout.androidaps.data.Intervals;
public class TreatmentsTemporaryBasalsFragment extends Fragment { public class TreatmentsTemporaryBasalsFragment extends Fragment {
@ -50,9 +48,9 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment {
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.TempBasalsViewHolder> { public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.TempBasalsViewHolder> {
OverlappingIntervals<TemporaryBasal> tempBasalList; Intervals<TemporaryBasal> tempBasalList;
RecyclerViewAdapter(OverlappingIntervals<TemporaryBasal> tempBasalList) { RecyclerViewAdapter(Intervals<TemporaryBasal> tempBasalList) {
this.tempBasalList = tempBasalList; this.tempBasalList = tempBasalList;
} }