From 4ba1adabf41e26154597e55144ddaaa7f310f5df Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 18 Sep 2018 10:38:12 +0200 Subject: [PATCH] TriggerAnd, TriggerOr --- .../general/automation/actions/Trigger.java | 6 +++ .../automation/actions/TriggerAnd.java | 35 ++++++++++++++++++ .../general/automation/actions/TriggerOr.java | 35 ++++++++++++++++++ .../automation/actions/TriggerAndTest.java | 37 +++++++++++++++++++ .../automation/actions/TriggerOrTest.java | 37 +++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Trigger.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerAnd.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerOr.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerAndTest.java create mode 100644 app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerOrTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Trigger.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Trigger.java new file mode 100644 index 0000000000..911986b377 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/Trigger.java @@ -0,0 +1,6 @@ +package info.nightscout.androidaps.plugins.general.automation.actions; + +public abstract class Trigger { + + abstract boolean shouldRun(); +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerAnd.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerAnd.java new file mode 100644 index 0000000000..27c8305d86 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerAnd.java @@ -0,0 +1,35 @@ +package info.nightscout.androidaps.plugins.general.automation.actions; + +import java.util.ArrayList; +import java.util.List; + +public class TriggerAnd extends Trigger { + + private List list = new ArrayList<>(); + + @Override + synchronized boolean shouldRun() { + boolean result = true; + + for (Trigger t : list) { + result = result && t.shouldRun(); + } + return result; + } + + synchronized void add(Trigger t) { + list.add(t); + } + + synchronized boolean remove(Trigger t) { + return list.remove(t); + } + + int size() { + return list.size(); + } + + Trigger get(int i) { + return list.get(i); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerOr.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerOr.java new file mode 100644 index 0000000000..630d967f1f --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerOr.java @@ -0,0 +1,35 @@ +package info.nightscout.androidaps.plugins.general.automation.actions; + +import java.util.ArrayList; +import java.util.List; + +public class TriggerOr extends Trigger { + + private List list = new ArrayList<>(); + + @Override + synchronized boolean shouldRun() { + boolean result = false; + + for (Trigger t : list) { + result = result || t.shouldRun(); + } + return result; + } + + synchronized void add(Trigger t) { + list.add(t); + } + + synchronized boolean remove(Trigger t) { + return list.remove(t); + } + + int size() { + return list.size(); + } + + Trigger get(int i) { + return list.get(i); + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerAndTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerAndTest.java new file mode 100644 index 0000000000..0dc001ccb9 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerAndTest.java @@ -0,0 +1,37 @@ +package info.nightscout.androidaps.plugins.general.automation.actions; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({}) +public class TriggerAndTest { + + @Test + public void doTests() { + TriggerAnd t = new TriggerAnd(); + TriggerAnd t2 = new TriggerAnd(); + TriggerAnd t3 = new TriggerAnd(); + + Assert.assertTrue(t.size() == 0); + + t.add(t2); + Assert.assertTrue(t.size() == 1); + Assert.assertEquals(t2, t.get(0)); + + t.add(t3); + Assert.assertTrue(t.size() == 2); + Assert.assertEquals(t2, t.get(0)); + Assert.assertEquals(t3, t.get(1)); + + t.remove(t2); + Assert.assertTrue(t.size() == 1); + Assert.assertEquals(t3, t.get(0)); + + Assert.assertTrue(t.shouldRun()); + } +} diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerOrTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerOrTest.java new file mode 100644 index 0000000000..13322680e6 --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/TriggerOrTest.java @@ -0,0 +1,37 @@ +package info.nightscout.androidaps.plugins.general.automation.actions; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({}) +public class TriggerOrTest { + + @Test + public void doTests() { + TriggerOr t = new TriggerOr(); + TriggerOr t2 = new TriggerOr(); + TriggerOr t3 = new TriggerOr(); + + Assert.assertTrue(t.size() == 0); + + t.add(t2); + Assert.assertTrue(t.size() == 1); + Assert.assertEquals(t2, t.get(0)); + + t.add(t3); + Assert.assertTrue(t.size() == 2); + Assert.assertEquals(t2, t.get(0)); + Assert.assertEquals(t3, t.get(1)); + + t.remove(t2); + Assert.assertTrue(t.size() == 1); + Assert.assertEquals(t3, t.get(0)); + + Assert.assertFalse(t.shouldRun()); + } +}