Replace constants by comparator enum

This commit is contained in:
Nico Schmitz 2018-09-19 21:13:02 +02:00
parent 3eee5b13c7
commit e32050d565
2 changed files with 57 additions and 46 deletions

View file

@ -7,13 +7,54 @@ import info.nightscout.androidaps.R;
public abstract class Trigger {
protected static final int ISLOWER = -2;
protected static final int ISEQUALORLOWER = -1;
protected static final int ISEQUAL = 0;
protected static final int ISEQUALORGREATER = 1;
protected static final int ISGREATER = 2;
public enum Comparator {
IS_LOWER,
IS_EQUAL_OR_LOWER,
IS_EQUAL,
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() {
}
@ -43,22 +84,4 @@ public abstract class Trigger {
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;
}
}

View file

@ -12,32 +12,20 @@ import info.nightscout.utils.JsonHelper;
public class TriggerBg extends Trigger {
double threshold;
int comparator = ISEQUAL;
String units = ProfileFunctions.getInstance().getProfileUnits();
protected double threshold;
protected Comparator comparator = Comparator.IS_EQUAL;
protected String units = ProfileFunctions.getInstance().getProfileUnits();
@Override
synchronized boolean shouldRun() {
GlucoseStatus glucoseStatus = GlucoseStatus.getGlucoseStatusData();
if (glucoseStatus == null && comparator == ISNOTAVAILABLE)
if (glucoseStatus == null && comparator.equals(Comparator.IS_NOT_AVAILABLE))
return true;
if (glucoseStatus == null)
return false;
switch (comparator) {
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;
return comparator.check(glucoseStatus.glucose, Profile.toMgdl(threshold, units));
}
@Override
@ -47,7 +35,7 @@ public class TriggerBg extends Trigger {
o.put("type", TriggerBg.class.getName());
JSONObject data = new JSONObject();
data.put("threshold", threshold);
data.put("comparator", comparator);
data.put("comparator", comparator.toString());
data.put("units", units);
o.put("data", data.toString());
} catch (JSONException e) {
@ -61,7 +49,7 @@ public class TriggerBg extends Trigger {
try {
JSONObject d = new JSONObject(data);
threshold = JsonHelper.safeGetDouble(d, "threshold");
comparator = JsonHelper.safeGetInt(d, "comparator");
comparator = Comparator.valueOf(JsonHelper.safeGetString(d, "comparator"));
units = JsonHelper.safeGetString(d, "units");
} catch (JSONException e) {
e.printStackTrace();
@ -76,10 +64,10 @@ public class TriggerBg extends Trigger {
@Override
String friendlyDescription() {
if (comparator == Trigger.ISNOTAVAILABLE)
if (comparator.equals(Comparator.IS_NOT_AVAILABLE))
return MainApp.gs(R.string.glucoseisnotavailable);
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) {
@ -87,7 +75,7 @@ public class TriggerBg extends Trigger {
return this;
}
TriggerBg comparator(int comparator) {
TriggerBg comparator(Comparator comparator) {
this.comparator = comparator;
return this;
}