-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathAndGateDemo.java
More file actions
83 lines (67 loc) · 2.45 KB
/
AndGateDemo.java
File metadata and controls
83 lines (67 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package org.reactfx.inhibeans.demo;
import java.util.function.Predicate;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ObservableBooleanValue;
import javafx.beans.value.ObservableValue;
import org.reactfx.Guard;
import org.reactfx.value.SuspendableVal;
import org.reactfx.value.Val;
public class AndGateDemo {
interface AndGate {
void setInputs(boolean a, boolean b);
ObservableValue<Boolean> a();
ObservableValue<Boolean> b();
ObservableValue<Boolean> output();
}
static void test(AndGate gate) {
class Counter {
int count = 0;
public void inc() { count += 1; }
public int get() { return count; }
}
Predicate<AndGate> consistent = g ->
g.output().getValue() == (g.a().getValue() && g.b().getValue());
gate.setInputs(false, false);
assert gate.output().getValue() == false;
Counter na = new Counter();
Counter nb = new Counter();
Counter no = new Counter();
gate.a().addListener(observable -> {
assert consistent.test(gate);
na.inc();
});
gate.b().addListener(observable -> {
assert consistent.test(gate);
nb.inc();
});
gate.output().addListener(observable -> {
assert consistent.test(gate);
no.inc();
});
gate.setInputs(true, true);
assert gate.output().getValue() == true;
assert na.get() == 1;
assert nb.get() == 1;
assert no.get() == 1;
}
static class AndGateImpl implements AndGate {
private final BooleanProperty a = new SimpleBooleanProperty();
private final BooleanProperty b = new SimpleBooleanProperty();
private final SuspendableVal<Boolean> output = Val.suspendable(a.and(b));
@Override
public void setInputs(boolean a, boolean b) {
Guard guard = output.suspend();
this.a.set(a);
this.b.set(b);
guard.close();
}
@Override public ObservableBooleanValue a() { return a; }
@Override public ObservableBooleanValue b() { return b; }
@Override public ObservableValue<Boolean> output() { return output; }
}
public static void main(String[] args) {
test(new AndGateImpl());
System.out.println("AndGate implementation passed the test.");
}
}