From 9eaba98dfa26bf807cf00ba4244d876360884cda Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sat, 15 Jul 2017 11:19:27 +0200 Subject: [PATCH] TT refactor OverlappingIntervals to two classes with an abstract superclass Intervals --- .../nightscout/androidaps/data/Intervals.java | 91 +++++++++++++++++++ .../data/NonOverlappingIntervals.java | 30 ++++++ .../androidaps/data/OverlappingIntervals.java | 80 ++-------------- .../interfaces/TreatmentsInterface.java | 8 +- .../ConfigBuilder/ConfigBuilderPlugin.java | 8 +- .../plugins/Treatments/TreatmentsPlugin.java | 16 ++-- .../TreatmentsExtendedBolusesFragment.java | 8 +- .../TreatmentsTempTargetFragment.java | 7 +- .../TreatmentsTemporaryBasalsFragment.java | 8 +- 9 files changed, 152 insertions(+), 104 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/data/Intervals.java create mode 100644 app/src/main/java/info/nightscout/androidaps/data/NonOverlappingIntervals.java diff --git a/app/src/main/java/info/nightscout/androidaps/data/Intervals.java b/app/src/main/java/info/nightscout/androidaps/data/Intervals.java new file mode 100644 index 0000000000..772c73b4b1 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/data/Intervals.java @@ -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 { + + LongSparseArray 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 list) { + for (T interval : list) { + rawData.put(interval.start(), interval); + } + merge(); + } + + + + public List getList() { + List list = new ArrayList<>(); + for (int i = 0; i < rawData.size(); i++) + list.add(rawData.valueAt(i)); + return list; + } + + public List getReversedList() { + List 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); + } + + + +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/data/NonOverlappingIntervals.java b/app/src/main/java/info/nightscout/androidaps/data/NonOverlappingIntervals.java new file mode 100644 index 0000000000..ca1d5abe63 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/data/NonOverlappingIntervals.java @@ -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 extends Intervals { + + 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; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java b/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java index cc03936766..182e2cf76f 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java +++ b/app/src/main/java/info/nightscout/androidaps/data/OverlappingIntervals.java @@ -1,41 +1,17 @@ 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 android.support.annotation.Nullable; 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 extends Intervals { -public class OverlappingIntervals { - - private LongSparseArray 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 list) { - for (T interval : list) { - rawData.put(interval.start(), interval); - } - merge(); - } - - private void merge() { + protected void merge() { for (int index = 0; index < rawData.size() - 1; index++) { Interval i = rawData.valueAt(index); long startOfNewer = rawData.valueAt(index + 1).start(); @@ -46,54 +22,10 @@ public class OverlappingIntervals { } @Nullable - public Interval getValueByInterval(long time) { + public T getValueByInterval(long time) { int index = binarySearch(time); if (index >= 0) return rawData.valueAt(index); return null; } - public List getList() { - List list = new ArrayList<>(); - for (int i = 0; i < rawData.size(); i++) - list.add(rawData.valueAt(i)); - return list; - } - - public List getReversedList() { - List 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); - } -} \ No newline at end of file +} diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java index d9e3742c70..5e8b4e52e8 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/TreatmentsInterface.java @@ -10,7 +10,7 @@ import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.db.TemporaryBasal; import info.nightscout.androidaps.db.Treatment; -import info.nightscout.androidaps.data.OverlappingIntervals; +import info.nightscout.androidaps.data.Intervals; import info.nightscout.androidaps.data.ProfileIntervals; /** @@ -42,18 +42,18 @@ public interface TreatmentsInterface { TemporaryBasal getTempBasalFromHistory(long time); double getTempBasalAbsoluteRateHistory(); double getTempBasalRemainingMinutesFromHistory(); - OverlappingIntervals getTemporaryBasalsFromHistory(); + Intervals getTemporaryBasalsFromHistory(); boolean isInHistoryExtendedBoluslInProgress(); ExtendedBolus getExtendedBolusFromHistory(long time); - OverlappingIntervals getExtendedBolusesFromHistory(); + Intervals getExtendedBolusesFromHistory(); boolean addToHistoryExtendedBolus(ExtendedBolus extendedBolus); boolean addToHistoryTreatment(DetailedBolusInfo detailedBolusInfo); TempTarget getTempTargetFromHistory(long time); - OverlappingIntervals getTempTargetsFromHistory(); + Intervals getTempTargetsFromHistory(); ProfileSwitch getProfileSwitchFromHistory(long time); ProfileIntervals getProfileSwitchesFromHistory(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java index 28821e0242..239318a6e4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/ConfigBuilder/ConfigBuilderPlugin.java @@ -23,7 +23,7 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.DetailedBolusInfo; import info.nightscout.androidaps.data.IobTotal; 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.ProfileIntervals; import info.nightscout.androidaps.data.PumpEnactResult; @@ -827,7 +827,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } @Override - public OverlappingIntervals getTemporaryBasalsFromHistory() { + public Intervals getTemporaryBasalsFromHistory() { return activeTreatments.getTemporaryBasalsFromHistory(); } @@ -874,7 +874,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } @Override - public OverlappingIntervals getExtendedBolusesFromHistory() { + public Intervals getExtendedBolusesFromHistory() { return activeTreatments.getExtendedBolusesFromHistory(); } @@ -896,7 +896,7 @@ public class ConfigBuilderPlugin implements PluginBase, PumpInterface, Constrain } @Override - public OverlappingIntervals getTempTargetsFromHistory() { + public Intervals getTempTargetsFromHistory() { return activeTreatments.getTempTargetsFromHistory(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index d7a2c26bc7..1c156cf146 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -8,7 +8,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; -import java.util.Date; import java.util.List; 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.IobTotal; 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.ProfileIntervals; import info.nightscout.androidaps.db.ExtendedBolus; @@ -48,9 +48,9 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { public static IobTotal lastTempBasalsCalculation; public static List treatments; - private static OverlappingIntervals tempBasals = new OverlappingIntervals<>(); - private static OverlappingIntervals extendedBoluses = new OverlappingIntervals<>(); - private static OverlappingIntervals tempTargets = new OverlappingIntervals<>(); + private static Intervals tempBasals = new NonOverlappingIntervals(); + private static Intervals extendedBoluses = new NonOverlappingIntervals(); + private static Intervals tempTargets = new NonOverlappingIntervals(); private static ProfileIntervals profiles = new ProfileIntervals<>(); private static boolean fragmentEnabled = true; @@ -347,7 +347,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { } @Override - public OverlappingIntervals getExtendedBolusesFromHistory() { + public Intervals getExtendedBolusesFromHistory() { return extendedBoluses; } @@ -380,7 +380,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { } @Override - public OverlappingIntervals getTemporaryBasalsFromHistory() { + public Intervals getTemporaryBasalsFromHistory() { return tempBasals; } @@ -442,7 +442,7 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { } @Override - public OverlappingIntervals getTempTargetsFromHistory() { + public Intervals getTempTargetsFromHistory() { return tempTargets; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsExtendedBolusesFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsExtendedBolusesFragment.java index 0acbf4b2c5..ca1cb002f8 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsExtendedBolusesFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsExtendedBolusesFragment.java @@ -23,8 +23,6 @@ import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Date; - import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; 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.DecimalFormatter; import info.nightscout.utils.NSUpload; -import info.nightscout.androidaps.data.OverlappingIntervals; +import info.nightscout.androidaps.data.Intervals; public class TreatmentsExtendedBolusesFragment extends Fragment { @@ -48,9 +46,9 @@ public class TreatmentsExtendedBolusesFragment extends Fragment { public class RecyclerViewAdapter extends RecyclerView.Adapter { - OverlappingIntervals extendedBolusList; + Intervals extendedBolusList; - RecyclerViewAdapter(OverlappingIntervals extendedBolusList) { + RecyclerViewAdapter(Intervals extendedBolusList) { this.extendedBolusList = extendedBolusList; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java index 7a74ca4a86..e47ed3c8e9 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTempTargetFragment.java @@ -26,11 +26,10 @@ import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.db.Source; import info.nightscout.androidaps.db.TempTarget; import info.nightscout.androidaps.events.EventTempTargetChange; -import info.nightscout.androidaps.data.Profile; import info.nightscout.utils.DateUtil; import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.NSUpload; -import info.nightscout.androidaps.data.OverlappingIntervals; +import info.nightscout.androidaps.data.Intervals; import info.nightscout.utils.SP; /** @@ -47,9 +46,9 @@ public class TreatmentsTempTargetFragment extends Fragment implements View.OnCli public class RecyclerViewAdapter extends RecyclerView.Adapter { - OverlappingIntervals tempTargetList; + Intervals tempTargetList; - RecyclerViewAdapter(OverlappingIntervals TempTargetList) { + RecyclerViewAdapter(Intervals TempTargetList) { this.tempTargetList = TempTargetList; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTemporaryBasalsFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTemporaryBasalsFragment.java index 8db8a9c67b..219e421c49 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTemporaryBasalsFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/fragments/TreatmentsTemporaryBasalsFragment.java @@ -23,8 +23,6 @@ import com.squareup.otto.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Date; - import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; 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.DecimalFormatter; import info.nightscout.utils.NSUpload; -import info.nightscout.androidaps.data.OverlappingIntervals; +import info.nightscout.androidaps.data.Intervals; public class TreatmentsTemporaryBasalsFragment extends Fragment { @@ -50,9 +48,9 @@ public class TreatmentsTemporaryBasalsFragment extends Fragment { public class RecyclerViewAdapter extends RecyclerView.Adapter { - OverlappingIntervals tempBasalList; + Intervals tempBasalList; - RecyclerViewAdapter(OverlappingIntervals tempBasalList) { + RecyclerViewAdapter(Intervals tempBasalList) { this.tempBasalList = tempBasalList; }