-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBooleanPropertyExample.java
107 lines (82 loc) · 2.5 KB
/
BooleanPropertyExample.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
package example;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.shimizukenta.property.BooleanProperty;
public class BooleanPropertyExample implements Runnable {
public BooleanPropertyExample() {
/* Nothing */
}
@Override
public void run() {
try {
System.out.println("run: " + this.getClass());
System.out.println();
/* build instance */
System.out.println("build " + false + " instance.");
final BooleanProperty p = BooleanProperty.newInstance(false);
System.out.println();
/* getter */
System.out.println("get value is " + p.booleanValue());
System.out.println();
/* setter */
System.out.println("set value: " + true);
p.set(true);
System.out.println("get value: " + p.booleanValue());
System.out.println();
System.out.println("set value: " + false);
p.set(false);
System.out.println("get value: " + p.booleanValue());
System.out.println();
/* detect changed */
System.out.println("add change listener, 1st time notify present value.");
p.addChangeListener(v -> {
System.out.println("value changed: " + v.booleanValue());
System.out.println();
});
System.out.println("set value: " + true);
p.set(true);
System.out.println("set value: " + true + ", no notify because no changed.");
p.set(true);
System.out.println();
System.out.println("set value: " + false);
p.set(false);
/* thread set value */
new Thread(() -> {
try {
System.out.println("sleep 2000 msec...");
Thread.sleep(2000L);
System.out.println("set value: " + true);
p.set(true);
}
catch ( InterruptedException ignore ) {
}
}).start();
/* wait until */
System.out.println("waiting until value is " + true);
p.waitUntil(true);
System.out.println("waiting until value is already " + true + ", pass through immediately.");
p.waitUntil(true);
System.out.println();
try {
System.out.println("waiting until value is " + false + " with timeout 2 sec...");
p.waitUntil(false, 2L, TimeUnit.SECONDS);
}
catch ( TimeoutException e ) {
System.out.println("timeout, waiting until value is " + false);
System.out.println(e);
System.out.println();
}
System.out.println("reach end.");
}
catch (InterruptedException ignore) {
}
}
public static void main(String[] args) {
try {
new BooleanPropertyExample().run();
}
catch ( Throwable t ) {
t.printStackTrace();
}
}
}