TriggerAnd, TriggerOr

This commit is contained in:
Milos Kozak 2018-09-18 10:38:12 +02:00
parent d317ec94ad
commit 4ba1adabf4
5 changed files with 150 additions and 0 deletions

View file

@ -0,0 +1,6 @@
package info.nightscout.androidaps.plugins.general.automation.actions;
public abstract class Trigger {
abstract boolean shouldRun();
}

View file

@ -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<Trigger> 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);
}
}

View file

@ -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<Trigger> 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);
}
}

View file

@ -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());
}
}

View file

@ -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());
}
}