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-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) {
|
|
|
|
this.value = value;
|
|
|
|
reason(reason);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-20 22:09:22 +01:00
|
|
|
public Constraint<T> setIfSmaller(T value, String reason) {
|
|
|
|
if (value.compareTo(this.value) < 0) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
if (value.compareTo(this.originalValue) < 0) {
|
|
|
|
reason(reason);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Constraint<T> setIfGreater(T value, String reason) {
|
|
|
|
if (value.compareTo(this.value) > 0) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
if (value.compareTo(this.originalValue) > 0) {
|
|
|
|
reason(reason);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-19 13:11:25 +01:00
|
|
|
public Constraint reason(String reason) {
|
|
|
|
reasons.add(reason);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-19 13:11:25 +01:00
|
|
|
}
|