2018-03-19 18:21:02 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mike on 19.03.2018.
|
|
|
|
*/
|
|
|
|
|
2018-03-20 22:09:22 +01:00
|
|
|
public class Constraint<T extends Comparable> {
|
|
|
|
private static Logger log = LoggerFactory.getLogger(Constraint.class);
|
|
|
|
|
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<>();
|
2018-03-23 17:36:49 +01:00
|
|
|
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-03-19 17:38:48 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-21 22:06:28 +01:00
|
|
|
public Constraint<T> set(T value, String reason, Object from) {
|
2018-03-19 17:38:48 +01:00
|
|
|
this.value = value;
|
2018-03-23 17:36:49 +01:00
|
|
|
addReason(reason, from);
|
|
|
|
addMostLimingReason(reason, from);
|
2018-03-19 17:38:48 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-21 22:06:28 +01:00
|
|
|
public Constraint<T> setIfSmaller(T value, String reason, Object from) {
|
2018-03-20 22:09:22 +01:00
|
|
|
if (value.compareTo(this.value) < 0) {
|
|
|
|
this.value = value;
|
2018-03-23 17:36:49 +01:00
|
|
|
mostLimiting.clear();
|
|
|
|
addMostLimingReason(reason, from);
|
2018-03-20 22:09:22 +01:00
|
|
|
}
|
|
|
|
if (value.compareTo(this.originalValue) < 0) {
|
2018-03-23 17:36:49 +01:00
|
|
|
addReason(reason, from);
|
2018-03-20 22:09:22 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-21 22:06:28 +01: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) {
|
|
|
|
this.value = value;
|
2018-03-23 17:36:49 +01:00
|
|
|
mostLimiting.clear();
|
|
|
|
addMostLimingReason(reason, from);
|
2018-03-20 22:09:22 +01:00
|
|
|
}
|
|
|
|
if (value.compareTo(this.originalValue) > 0) {
|
2018-03-23 17:36:49 +01:00
|
|
|
addReason(reason, from);
|
2018-03-20 22:09:22 +01:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:36:49 +01:00
|
|
|
public Constraint addReason(String reason, Object from) {
|
2018-03-23 09:59:07 +01:00
|
|
|
reasons.add(from.getClass().getSimpleName().replace("Plugin", "") + ": " + reason);
|
2018-03-19 13:11:25 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:36:49 +01:00
|
|
|
public Constraint addMostLimingReason(String reason, Object from) {
|
|
|
|
mostLimiting.add(from.getClass().getSimpleName().replace("Plugin", "") + ": " + 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-03-20 22:09:22 +01: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
|
|
|
|
2018-03-21 22:06:28 +01:00
|
|
|
public List<String> getReasonList() {
|
|
|
|
return reasons;
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:36:49 +01:00
|
|
|
public String getMostLimitedReasons() {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
int count = 0;
|
|
|
|
for (String r : mostLimiting) {
|
|
|
|
if (count++ != 0) sb.append("\n");
|
|
|
|
sb.append(r);
|
|
|
|
}
|
|
|
|
log.debug("Limiting origial value: " + originalValue + " to " + value + ". Reason: " + sb.toString());
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getMostLimitedReasonList() {
|
|
|
|
return mostLimiting;
|
|
|
|
}
|
|
|
|
|
2018-03-21 22:06:28 +01:00
|
|
|
public void copyReasons(Constraint<?> another) {
|
|
|
|
for (String s: another.getReasonList()) {
|
|
|
|
reasons.add(s);
|
|
|
|
}
|
|
|
|
}
|
2018-03-19 13:11:25 +01:00
|
|
|
}
|