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

118 lines
3.1 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;
/**
* 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<>();
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;
}
public Constraint<T> set(T value, String reason, Object 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> setIfSmaller(T value, String reason, Object from) {
2018-03-20 22:09:22 +01:00
if (value.compareTo(this.value) < 0) {
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;
}
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;
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;
}
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;
}
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
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);
}
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) {
for (String s: another.getReasonList()) {
reasons.add(s);
}
}
2018-03-19 13:11:25 +01:00
}