-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegerPropertyExample.java
140 lines (109 loc) · 3.21 KB
/
IntegerPropertyExample.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package example;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.shimizukenta.property.BooleanCompution;
import com.shimizukenta.property.IntegerProperty;
public class IntegerPropertyExample implements Runnable {
public IntegerPropertyExample() {
/* Nothing */
}
@Override
public void run() {
try {
System.out.println("run: " + this.getClass());
System.out.println();
/* build instance */
System.out.println("build IntegerProperty 0 instance.");
final IntegerProperty p = IntegerProperty.newInstance(0);
System.out.println();
/* getter */
System.out.println("get value: " + p.intValue());
System.out.println();
/* setter */
System.out.println("set value: " + 2);
p.set(2);;
System.out.println("get value: " + p.intValue());
System.out.println();
/* detect value changed */
System.out.println("add change listener, 1st time notify present value.");
p.addChangeListener(v -> {
System.out.println("value changed: " + v);
System.out.println();
});
System.out.println("set value: " + 3);
p.set(3);;
System.out.println("set value: " + 3 + ", no notify because no changed.");
p.set(3);
System.out.println();
/* Threading */
new Thread(() -> {
try {
System.out.println("sleep 2000 msec...");
Thread.sleep(2000L);
System.out.println("set value: " + 10);
p.set(10);
}
catch ( InterruptedException ignore ) {
}
}).start();
/* wait until */
System.out.println("waiting until >5");
p.waitUntilGreaterThan(5);
System.out.println("waiting until already <15, pass through immediately.");
p.waitUntilLessThan(15);
System.out.println();
new Thread(() -> {
try {
System.out.println("sleep 2000 msec...");
Thread.sleep(2000L);
System.out.println("set value: " + -10);
p.set(-10);
}
catch ( InterruptedException ignore ) {
}
}).start();
System.out.println("waiting until <0");
p.waitUntilLessThanZero();
try {
System.out.println("waiting until ==0 with timeout 2 sec...");
p.waitUntilEqualToZero(2L, TimeUnit.SECONDS);
}
catch ( TimeoutException e ) {
System.out.println("timeout, waiting until ==0");
System.out.println(e);
System.out.println();
}
/* compution */
BooleanCompution eq = p.computeIsEqualTo(0);
eq.addChangeListener(v -> {
System.out.println("is ==0: " + v);
});
BooleanCompution lt = p.computeIsLessThan(0);
lt.addChangeListener(v -> {
System.out.println("is <0: " + v);
});
BooleanCompution ge = p.computeIsGreaterThanOrEqualTo(0);
ge.addChangeListener(v -> {
System.out.println("is >=0: " + v);
});
System.out.println();
System.out.println("set value: " + 0);
p.set(0);
System.out.println();
System.out.println("set value: " + 10 + ", no notify because no change.");
p.set(10);
System.out.println();
System.out.println("reach end");
}
catch ( InterruptedException ignore ) {
}
}
public static void main(String[] args) {
try {
new IntegerPropertyExample().run();
}
catch ( Throwable t ) {
t.printStackTrace();
}
}
}