Replace constants by comparator enum
This commit is contained in:
parent
3eee5b13c7
commit
e32050d565
|
@ -7,13 +7,54 @@ import info.nightscout.androidaps.R;
|
||||||
|
|
||||||
public abstract class Trigger {
|
public abstract class Trigger {
|
||||||
|
|
||||||
protected static final int ISLOWER = -2;
|
public enum Comparator {
|
||||||
protected static final int ISEQUALORLOWER = -1;
|
IS_LOWER,
|
||||||
protected static final int ISEQUAL = 0;
|
IS_EQUAL_OR_LOWER,
|
||||||
protected static final int ISEQUALORGREATER = 1;
|
IS_EQUAL,
|
||||||
protected static final int ISGREATER = 2;
|
IS_EQUAL_OR_GREATER,
|
||||||
|
IS_GREATER,
|
||||||
|
IS_NOT_AVAILABLE;
|
||||||
|
|
||||||
protected static final int ISNOTAVAILABLE = 10;
|
public int getStringRes() {
|
||||||
|
switch (this) {
|
||||||
|
case IS_LOWER:
|
||||||
|
return R.string.islower;
|
||||||
|
case IS_EQUAL_OR_LOWER:
|
||||||
|
return R.string.isequalorlower;
|
||||||
|
case IS_EQUAL:
|
||||||
|
return R.string.isequal;
|
||||||
|
case IS_EQUAL_OR_GREATER:
|
||||||
|
return R.string.isequalorgreater;
|
||||||
|
case IS_GREATER:
|
||||||
|
return R.string.isgreater;
|
||||||
|
case IS_NOT_AVAILABLE:
|
||||||
|
return R.string.isnotavailable;
|
||||||
|
default:
|
||||||
|
return R.string.unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends Comparable> boolean check(T obj1, T obj2) {
|
||||||
|
if (obj1 == null || obj2 == null)
|
||||||
|
return this.equals(Comparator.IS_NOT_AVAILABLE);
|
||||||
|
|
||||||
|
int comparison = obj1.compareTo(obj2);
|
||||||
|
switch (this) {
|
||||||
|
case IS_LOWER:
|
||||||
|
return comparison < 0;
|
||||||
|
case IS_EQUAL_OR_LOWER:
|
||||||
|
return comparison <= 0;
|
||||||
|
case IS_EQUAL:
|
||||||
|
return comparison == 0;
|
||||||
|
case IS_EQUAL_OR_GREATER:
|
||||||
|
return comparison >= 0;
|
||||||
|
case IS_GREATER:
|
||||||
|
return comparison > 0;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Trigger() {
|
Trigger() {
|
||||||
}
|
}
|
||||||
|
@ -43,22 +84,4 @@ public abstract class Trigger {
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int toComparatorString(int comparator) {
|
|
||||||
switch (comparator) {
|
|
||||||
case ISLOWER:
|
|
||||||
return R.string.islower;
|
|
||||||
case ISEQUALORLOWER:
|
|
||||||
return R.string.isequalorlower;
|
|
||||||
case ISEQUAL:
|
|
||||||
return R.string.isequal;
|
|
||||||
case ISEQUALORGREATER:
|
|
||||||
return R.string.isequalorgreater;
|
|
||||||
case ISGREATER:
|
|
||||||
return R.string.isgreater;
|
|
||||||
case ISNOTAVAILABLE:
|
|
||||||
return R.string.isnotavailable;
|
|
||||||
}
|
|
||||||
return R.string.unknown;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,32 +12,20 @@ import info.nightscout.utils.JsonHelper;
|
||||||
|
|
||||||
public class TriggerBg extends Trigger {
|
public class TriggerBg extends Trigger {
|
||||||
|
|
||||||
double threshold;
|
protected double threshold;
|
||||||
int comparator = ISEQUAL;
|
protected Comparator comparator = Comparator.IS_EQUAL;
|
||||||
String units = ProfileFunctions.getInstance().getProfileUnits();
|
protected String units = ProfileFunctions.getInstance().getProfileUnits();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
synchronized boolean shouldRun() {
|
synchronized boolean shouldRun() {
|
||||||
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
|
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
|
||||||
|
|
||||||
if (glucoseStatus == null && comparator == ISNOTAVAILABLE)
|
if (glucoseStatus == null && comparator.equals(Comparator.IS_NOT_AVAILABLE))
|
||||||
return true;
|
return true;
|
||||||
if (glucoseStatus == null)
|
if (glucoseStatus == null)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch (comparator) {
|
return comparator.check(glucoseStatus.glucose, Profile.toMgdl(threshold, units));
|
||||||
case ISLOWER:
|
|
||||||
return glucoseStatus.glucose < Profile.toMgdl(threshold, units);
|
|
||||||
case ISEQUALORLOWER:
|
|
||||||
return glucoseStatus.glucose <= Profile.toMgdl(threshold, units);
|
|
||||||
case ISEQUAL:
|
|
||||||
return glucoseStatus.glucose == Profile.toMgdl(threshold, units);
|
|
||||||
case ISEQUALORGREATER:
|
|
||||||
return glucoseStatus.glucose >= Profile.toMgdl(threshold, units);
|
|
||||||
case ISGREATER:
|
|
||||||
return glucoseStatus.glucose > Profile.toMgdl(threshold, units);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -47,7 +35,7 @@ public class TriggerBg extends Trigger {
|
||||||
o.put("type", TriggerBg.class.getName());
|
o.put("type", TriggerBg.class.getName());
|
||||||
JSONObject data = new JSONObject();
|
JSONObject data = new JSONObject();
|
||||||
data.put("threshold", threshold);
|
data.put("threshold", threshold);
|
||||||
data.put("comparator", comparator);
|
data.put("comparator", comparator.toString());
|
||||||
data.put("units", units);
|
data.put("units", units);
|
||||||
o.put("data", data.toString());
|
o.put("data", data.toString());
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
|
@ -61,7 +49,7 @@ public class TriggerBg extends Trigger {
|
||||||
try {
|
try {
|
||||||
JSONObject d = new JSONObject(data);
|
JSONObject d = new JSONObject(data);
|
||||||
threshold = JsonHelper.safeGetDouble(d, "threshold");
|
threshold = JsonHelper.safeGetDouble(d, "threshold");
|
||||||
comparator = JsonHelper.safeGetInt(d, "comparator");
|
comparator = Comparator.valueOf(JsonHelper.safeGetString(d, "comparator"));
|
||||||
units = JsonHelper.safeGetString(d, "units");
|
units = JsonHelper.safeGetString(d, "units");
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -76,10 +64,10 @@ public class TriggerBg extends Trigger {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String friendlyDescription() {
|
String friendlyDescription() {
|
||||||
if (comparator == Trigger.ISNOTAVAILABLE)
|
if (comparator.equals(Comparator.IS_NOT_AVAILABLE))
|
||||||
return MainApp.gs(R.string.glucoseisnotavailable);
|
return MainApp.gs(R.string.glucoseisnotavailable);
|
||||||
else
|
else
|
||||||
return MainApp.gs(R.string.glucosecompared, Trigger.toComparatorString(comparator), threshold, units);
|
return MainApp.gs(R.string.glucosecompared, comparator.getStringRes(), threshold, units);
|
||||||
}
|
}
|
||||||
|
|
||||||
TriggerBg threshold(double threshold) {
|
TriggerBg threshold(double threshold) {
|
||||||
|
@ -87,7 +75,7 @@ public class TriggerBg extends Trigger {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
TriggerBg comparator(int comparator) {
|
TriggerBg comparator(Comparator comparator) {
|
||||||
this.comparator = comparator;
|
this.comparator = comparator;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue