AndroidAPS/app/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java

145 lines
4.3 KiB
Java
Raw Normal View History

package info.nightscout.androidaps.interfaces;
2018-03-19 13:11:25 +01:00
2018-03-20 22:09:22 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2018-03-19 13:11:25 +01:00
import java.util.ArrayList;
import java.util.List;
2018-07-30 15:46:20 +02:00
import info.nightscout.androidaps.logging.L;
2018-03-19 13:11:25 +01:00
/**
* Created by mike on 19.03.2018.
*/
2018-03-20 22:09:22 +01:00
public class Constraint<T extends Comparable> {
2018-09-11 14:56:42 +02:00
private static Logger log = LoggerFactory.getLogger(L.CONSTRAINTS);
2018-03-20 22:09:22 +01:00
2018-03-19 17:38:48 +01:00
T value;
2018-03-20 22:09:22 +01:00
T originalValue;
2018-03-19 13:11:25 +01:00
List<String> reasons = new ArrayList<>();
List<String> mostLimiting = new ArrayList<>();
2018-03-19 13:11:25 +01:00
2018-03-19 17:38:48 +01:00
public Constraint(T value) {
this.value = value;
2018-03-20 22:09:22 +01:00
this.originalValue = value;
2018-03-19 17:38:48 +01:00
}
2018-03-20 22:09:22 +01:00
public T value() {
2018-03-19 17:38:48 +01:00
return value;
}
2018-03-20 22:09:22 +01:00
public T originalValue() {
return originalValue;
}
2018-03-19 17:38:48 +01:00
public Constraint<T> set(T value) {
this.value = value;
2018-03-20 22:09:22 +01:00
this.originalValue = value;
2018-09-11 14:56:42 +02:00
if (L.isEnabled(L.CONSTRAINTS))
log.debug("Setting value " + value);
2018-03-19 17:38:48 +01:00
return this;
}
public Constraint<T> set(T value, String reason, Object from) {
2018-09-11 14:56:42 +02:00
if (L.isEnabled(L.CONSTRAINTS))
log.debug("Setting value " + this.value + " -> " + value + " (" + reason + ")[" + from + "]");
2018-03-19 17:38:48 +01:00
this.value = value;
addReason(reason, from);
addMostLimingReason(reason, from);
2018-03-19 17:38:48 +01:00
return this;
}
public Constraint<T> setIfDifferent(T value, String reason, Object from) {
if (!this.value.equals(value)) {
2018-09-11 14:56:42 +02:00
if (L.isEnabled(L.CONSTRAINTS))
log.debug("Setting because of different value " + this.value + " -> " + value + " (" + reason + ")[" + from + "]");
this.value = value;
addReason(reason, from);
addMostLimingReason(reason, from);
}
return this;
}
public Constraint<T> setIfSmaller(T value, String reason, Object from) {
2018-03-20 22:09:22 +01:00
if (value.compareTo(this.value) < 0) {
2018-09-11 14:56:42 +02:00
if (L.isEnabled(L.CONSTRAINTS))
log.debug("Setting because of smaller value " + this.value + " -> " + value + " (" + reason + ")[" + from + "]");
2018-03-20 22:09:22 +01:00
this.value = value;
mostLimiting.clear();
addMostLimingReason(reason, from);
2018-03-20 22:09:22 +01:00
}
if (value.compareTo(this.originalValue) < 0) {
addReason(reason, from);
2018-03-20 22:09:22 +01:00
}
return this;
}
2018-07-30 15:46:20 +02:00
public Constraint<T> setIfGreater(T value, String reason, Object from) {
2018-03-20 22:09:22 +01:00
if (value.compareTo(this.value) > 0) {
2018-09-11 14:56:42 +02:00
if (L.isEnabled(L.CONSTRAINTS))
log.debug("Setting because of greater value " + this.value + " -> " + value + " (" + reason + ")[" + from + "]");
2018-03-20 22:09:22 +01:00
this.value = value;
mostLimiting.clear();
addMostLimingReason(reason, from);
2018-03-20 22:09:22 +01:00
}
if (value.compareTo(this.originalValue) > 0) {
addReason(reason, from);
2018-03-20 22:09:22 +01:00
}
return this;
}
2018-09-11 14:56:42 +02:00
private String translateFrom(Object from) {
return from.getClass().getSimpleName().replace("Plugin", "");
}
public Constraint addReason(String reason, Object from) {
2018-09-11 14:56:42 +02:00
reasons.add(translateFrom(from) + ": " + reason);
2018-03-19 13:11:25 +01:00
return this;
}
2018-07-30 15:46:20 +02:00
public Constraint addMostLimingReason(String reason, Object from) {
2018-09-11 14:56:42 +02:00
mostLimiting.add(translateFrom(from) + ": " + reason);
return this;
}
2018-03-19 13:11:25 +01:00
public String getReasons() {
StringBuilder sb = new StringBuilder();
int count = 0;
2018-03-20 22:09:22 +01:00
for (String r : reasons) {
2018-03-19 13:11:25 +01:00
if (count++ != 0) sb.append("\n");
sb.append(r);
}
2018-09-11 14:56:42 +02:00
if (L.isEnabled(L.CONSTRAINTS))
2018-07-30 15:46:20 +02:00
log.debug("Limiting origial value: " + originalValue + " to " + value + ". Reason: " + sb.toString());
2018-03-19 13:11:25 +01:00
return sb.toString();
}
2018-03-20 22:09:22 +01:00
public List<String> getReasonList() {
return reasons;
}
public String getMostLimitedReasons() {
StringBuilder sb = new StringBuilder();
int count = 0;
for (String r : mostLimiting) {
if (count++ != 0) sb.append("\n");
sb.append(r);
}
2018-09-11 14:56:42 +02:00
if (L.isEnabled(L.CONSTRAINTS))
log.debug("Limiting origial value: " + originalValue + " to " + value + ". Reason: " + sb.toString());
return sb.toString();
}
public List<String> getMostLimitedReasonList() {
return mostLimiting;
}
public void copyReasons(Constraint<?> another) {
2018-07-30 15:46:20 +02:00
for (String s : another.getReasonList()) {
reasons.add(s);
}
}
2018-03-19 13:11:25 +01:00
}