From b9e50885a876e3e668c32bb26d5b1055fd04d008 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 7 Mar 2021 14:22:50 +0100 Subject: [PATCH] Constraint -> kt --- .../interfaces/ConstraintsCheckerTest.kt | 11 ++ .../constraints/safety/SafetyPluginTest.kt | 6 +- .../storage/StorageConstraintPluginTest.kt | 4 + .../androidaps/interfaces/Constraint.java | 129 ------------------ .../androidaps/interfaces/Constraint.kt | 116 ++++++++++++++++ .../data/OverlappingIntervalsTest.kt | 2 +- .../pump/danaR/comm/MsgBolusStartTest.kt | 2 +- .../danaR/comm/MsgBolusStartWithSpeedTest.kt | 2 +- 8 files changed, 137 insertions(+), 135 deletions(-) delete mode 100644 core/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java create mode 100644 core/src/main/java/info/nightscout/androidaps/interfaces/Constraint.kt diff --git a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt index 0cb295d9b6..47d6a9626e 100644 --- a/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/interfaces/ConstraintsCheckerTest.kt @@ -42,6 +42,8 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.Mockito.`when` +import org.mockito.Mockito.anyInt +import org.mockito.Mockito.anyString import org.powermock.core.classloader.annotations.PrepareForTest import org.powermock.modules.junit4.PowerMockRunner import java.util.* @@ -119,6 +121,15 @@ class ConstraintsCheckerTest : TestBaseWithProfile() { `when`(resourceHelper.gs(R.string.limitingiob)).thenReturn("Limiting IOB to %.1f U because of %s") `when`(resourceHelper.gs(R.string.limitingbasalratio)).thenReturn("Limiting max basal rate to %1\$.2f U/h because of %2\$s") `when`(resourceHelper.gs(R.string.limitingpercentrate)).thenReturn("Limiting max percent rate to %1\$d%% because of %2\$s") + `when`(resourceHelper.gs(R.string.itmustbepositivevalue)).thenReturn("it must be positive value") + `when`(resourceHelper.gs(R.string.smbnotallowedinopenloopmode)).thenReturn("SMB not allowed in open loop mode") + `when`(resourceHelper.gs(R.string.pumplimit)).thenReturn("pump limit") + `when`(resourceHelper.gs(R.string.smbalwaysdisabled)).thenReturn("SMB always and after carbs disabled because active BG source doesn\\'t support advanced filtering") + `when`(resourceHelper.gs(R.string.limitingpercentrate, 0, "it must be positive value")).thenReturn("") + `when`(resourceHelper.gs(R.string.limitingbolus, 3.0, "pump limit")).thenReturn("") + `when`(resourceHelper.gs(R.string.limitingbolus, 6.0, "pump limit")).thenReturn("") + `when`(resourceHelper.gs(R.string.limitingbasalratio, 0.8, "pump limit")).thenReturn("") + `when`(resourceHelper.gs(R.string.limitingpercentrate, 200, "pump limit")).thenReturn("") // RS constructor `when`(sp.getString(R.string.key_danars_address, "")).thenReturn("") diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPluginTest.kt index 75b01f02e6..9f5bb89f01 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/safety/SafetyPluginTest.kt @@ -49,7 +49,7 @@ class SafetyPluginTest : TestBaseWithProfile() { private lateinit var safetyPlugin: SafetyPlugin val injector = HasAndroidInjector { AndroidInjector { } } - val pumpDescription = PumpDescription() + private val pumpDescription = PumpDescription() @Before fun prepare() { @@ -198,7 +198,7 @@ class SafetyPluginTest : TestBaseWithProfile() { `when`(sp.getString(R.string.key_age, "")).thenReturn("child") var d = Constraint(Constants.REALLYHIGHBOLUS) d = safetyPlugin.applyBolusConstraints(d) - Assert.assertEquals(3.0, d.value()!!, 0.01) + Assert.assertEquals(3.0, d.value(), 0.01) Assert.assertEquals(""" Safety: Limiting bolus to 3.0 U because of max value in preferences Safety: Limiting bolus to 5.0 U because of hard limit @@ -240,7 +240,7 @@ class SafetyPluginTest : TestBaseWithProfile() { // Apply all limits var d = Constraint(Constants.REALLYHIGHIOB) d = safetyPlugin.applyMaxIOBConstraints(d) - Assert.assertEquals(1.5, d.value()!!, 0.01) + Assert.assertEquals(1.5, d.value(), 0.01) Assert.assertEquals(""" Safety: Limiting IOB to 1.5 U because of max value in preferences """.trimIndent(), d.getReasons(aapsLogger)) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/storage/StorageConstraintPluginTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/storage/StorageConstraintPluginTest.kt index 90c1199dac..6be468634c 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/constraints/storage/StorageConstraintPluginTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/constraints/storage/StorageConstraintPluginTest.kt @@ -12,6 +12,9 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.Mockito.anyInt +import org.mockito.Mockito.anyLong import org.powermock.modules.junit4.PowerMockRunner @RunWith(PowerMockRunner::class) @@ -24,6 +27,7 @@ class StorageConstraintPluginTest : TestBase() { @Before fun prepareMock() { storageConstraintPlugin = StorageConstraintPlugin({ AndroidInjector { } }, aapsLogger, resourceHelper, rxBusWrapper) + `when`(resourceHelper.gs(anyInt(), anyLong())).thenReturn("") } class MockedStorageConstraintPlugin constructor( diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java b/core/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java deleted file mode 100644 index 696b3657d6..0000000000 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/Constraint.java +++ /dev/null @@ -1,129 +0,0 @@ -package info.nightscout.androidaps.interfaces; - -import java.util.ArrayList; -import java.util.List; - -import info.nightscout.androidaps.logging.AAPSLogger; -import info.nightscout.androidaps.logging.LTag; - -/** - * Created by mike on 19.03.2018. - */ - -public class Constraint> { - private T value; - private T originalValue; - - private final List reasons = new ArrayList<>(); - private final List mostLimiting = new ArrayList<>(); - - public Constraint(T value) { - this.value = value; - this.originalValue = value; - } - - public T value() { - return value; - } - - public T originalValue() { - return originalValue; - } - - public Constraint set(AAPSLogger aapsLogger, T value) { - this.value = value; - this.originalValue = value; - aapsLogger.debug(LTag.CONSTRAINTS, "Setting value " + value); - return this; - } - - public Constraint set(AAPSLogger aapsLogger, T value, String reason, Object from) { - aapsLogger.debug(LTag.CONSTRAINTS, "Setting value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]"); - this.value = value; - addReason(reason, from); - addMostLimingReason(reason, from); - return this; - } - - public Constraint setIfDifferent(AAPSLogger aapsLogger, T value, String reason, Object from) { - if (!this.value.equals(value)) { - aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of different value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]"); - this.value = value; - addReason(reason, from); - addMostLimingReason(reason, from); - } - return this; - } - - public Constraint setIfSmaller(AAPSLogger aapsLogger, T value, String reason, Object from) { - if (value.compareTo(this.value) < 0) { - aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of smaller value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]"); - this.value = value; - mostLimiting.clear(); - addMostLimingReason(reason, from); - } - if (value.compareTo(this.originalValue) < 0) { - addReason(reason, from); - } - return this; - } - - public Constraint setIfGreater(AAPSLogger aapsLogger, T value, String reason, Object from) { - if (value.compareTo(this.value) > 0) { - aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of greater value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]"); - this.value = value; - mostLimiting.clear(); - addMostLimingReason(reason, from); - } - if (value.compareTo(this.originalValue) > 0) { - addReason(reason, from); - } - return this; - } - - private String translateFrom(Object from) { - return from.getClass().getSimpleName().replace("Plugin", ""); - } - - public void addReason(String reason, Object from) { - reasons.add(translateFrom(from) + ": " + reason); - } - - private void addMostLimingReason(String reason, Object from) { - mostLimiting.add(translateFrom(from) + ": " + reason); - } - - public String getReasons(AAPSLogger aapsLogger) { - StringBuilder sb = new StringBuilder(); - int count = 0; - for (String r : reasons) { - if (count++ != 0) sb.append("\n"); - sb.append(r); - } - aapsLogger.debug(LTag.CONSTRAINTS, "Limiting original value: " + originalValue + " to " + value + ". Reason: " + sb.toString()); - return sb.toString(); - } - - public List getReasonList() { - return reasons; - } - - public String getMostLimitedReasons(AAPSLogger aapsLogger) { - StringBuilder sb = new StringBuilder(); - int count = 0; - for (String r : mostLimiting) { - if (count++ != 0) sb.append("\n"); - sb.append(r); - } - aapsLogger.debug(LTag.CONSTRAINTS, "Limiting original value: " + originalValue + " to " + value + ". Reason: " + sb.toString()); - return sb.toString(); - } - - public List getMostLimitedReasonList() { - return mostLimiting; - } - - public void copyReasons(Constraint another) { - reasons.addAll(another.getReasonList()); - } -} diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/Constraint.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/Constraint.kt new file mode 100644 index 0000000000..997e136e66 --- /dev/null +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/Constraint.kt @@ -0,0 +1,116 @@ +package info.nightscout.androidaps.interfaces + +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.logging.LTag +import java.util.* + +class Constraint>(private var value: T) { + + private var originalValue: T + private val reasons: MutableList = ArrayList() + private val mostLimiting: MutableList = ArrayList() + fun value(): T { + return value + } + + fun originalValue(): T { + return originalValue + } + + operator fun set(aapsLogger: AAPSLogger, value: T): Constraint { + this.value = value + originalValue = value + aapsLogger.debug(LTag.CONSTRAINTS, "Setting value $value") + return this + } + + operator fun set(aapsLogger: AAPSLogger, value: T, reason: String, from: Any): Constraint { + aapsLogger.debug(LTag.CONSTRAINTS, "Setting value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]") + this.value = value + addReason(reason, from) + addMostLimingReason(reason, from) + return this + } + + fun setIfDifferent(aapsLogger: AAPSLogger, value: T, reason: String, from: Any): Constraint { + if (this.value != value) { + aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of different value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]") + this.value = value + addReason(reason, from) + addMostLimingReason(reason, from) + } + return this + } + + fun setIfSmaller(aapsLogger: AAPSLogger, value: T, reason: String, from: Any): Constraint { + if (value < this.value) { + aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of smaller value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]") + this.value = value + mostLimiting.clear() + addMostLimingReason(reason, from) + } + if (value < originalValue) { + addReason(reason, from) + } + return this + } + + fun setIfGreater(aapsLogger: AAPSLogger, value: T, reason: String, from: Any): Constraint { + if (value > this.value) { + aapsLogger.debug(LTag.CONSTRAINTS, "Setting because of greater value " + this.value + " -> " + value + " (" + reason + ")[" + translateFrom(from) + "]") + this.value = value + mostLimiting.clear() + addMostLimingReason(reason, from) + } + if (value > originalValue) { + addReason(reason, from) + } + return this + } + + private fun translateFrom(from: Any): String { + return from.javaClass.simpleName.replace("Plugin", "") + } + + fun addReason(reason: String, from: Any) { + reasons.add(translateFrom(from) + ": " + reason) + } + + private fun addMostLimingReason(reason: String, from: Any) { + mostLimiting.add(translateFrom(from) + ": " + reason) + } + + fun getReasons(aapsLogger: AAPSLogger): String { + val sb = StringBuilder() + for ((count, r) in reasons.withIndex()) { + if (count != 0) sb.append("\n") + sb.append(r) + } + aapsLogger.debug(LTag.CONSTRAINTS, "Limiting original value: $originalValue to $value. Reason: $sb") + return sb.toString() + } + + val reasonList: List + get() = reasons + + fun getMostLimitedReasons(aapsLogger: AAPSLogger): String { + val sb = StringBuilder() + for ((count, r) in mostLimiting.withIndex()) { + if (count != 0) sb.append("\n") + sb.append(r) + } + aapsLogger.debug(LTag.CONSTRAINTS, "Limiting original value: $originalValue to $value. Reason: $sb") + return sb.toString() + } + + val mostLimitedReasonList: List + get() = mostLimiting + + fun copyReasons(another: Constraint<*>) { + reasons.addAll(another.reasonList) + } + + init { + originalValue = value + } +} \ No newline at end of file diff --git a/core/src/test/java/info/nightscout/androidaps/data/OverlappingIntervalsTest.kt b/core/src/test/java/info/nightscout/androidaps/data/OverlappingIntervalsTest.kt index 71e469d682..9973fb69dc 100644 --- a/core/src/test/java/info/nightscout/androidaps/data/OverlappingIntervalsTest.kt +++ b/core/src/test/java/info/nightscout/androidaps/data/OverlappingIntervalsTest.kt @@ -53,7 +53,7 @@ class OverlappingIntervalsTest { list.reset() list.add(someList) Assert.assertEquals(startDate, list[0]?.data?.timestamp) - Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversed(0)?.data?.timestamp) + Assert.assertEquals(startDate + T.hours(1).msecs(), list.getReversed(0).data.timestamp) Assert.assertEquals(startDate + T.hours(1).msecs(), list.reversedList[0].data.timestamp) } } \ No newline at end of file diff --git a/danar/src/test/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStartTest.kt b/danar/src/test/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStartTest.kt index da5a814bad..ce02b1fe6f 100644 --- a/danar/src/test/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStartTest.kt +++ b/danar/src/test/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStartTest.kt @@ -15,7 +15,7 @@ import org.powermock.modules.junit4.PowerMockRunner class MsgBolusStartTest : DanaRTestBase() { @Test fun runTest() { - `when`(constraintChecker.applyBolusConstraints(Constraint(anyObject()))).thenReturn(Constraint(0.0)) + `when`(constraintChecker.applyBolusConstraints(anyObject())).thenReturn(Constraint(0.0)) val packet = MsgBolusStart(injector, 1.0) // test message decoding diff --git a/danar/src/test/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStartWithSpeedTest.kt b/danar/src/test/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStartWithSpeedTest.kt index 6c89a58db0..6048350c87 100644 --- a/danar/src/test/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStartWithSpeedTest.kt +++ b/danar/src/test/java/info/nightscout/androidaps/plugins/pump/danaR/comm/MsgBolusStartWithSpeedTest.kt @@ -15,7 +15,7 @@ import org.powermock.modules.junit4.PowerMockRunner class MsgBolusStartWithSpeedTest : DanaRTestBase() { @Test fun runTest() { - Mockito.`when`(constraintChecker.applyBolusConstraints(Constraint(anyObject()))).thenReturn(Constraint(0.0)) + Mockito.`when`(constraintChecker.applyBolusConstraints(anyObject())).thenReturn(Constraint(0.0)) val packet = MsgBolusStartWithSpeed(injector, 0.0, 0) // test message decoding