-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectPropertyExample.java
161 lines (124 loc) · 3.74 KB
/
ObjectPropertyExample.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package example;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.shimizukenta.property.BooleanCompution;
import com.shimizukenta.property.ObjectProperty;
public class ObjectPropertyExample implements Runnable {
public ObjectPropertyExample() {
/* Nothing */
}
@Override
public void run() {
try {
System.out.println("run: " + this.getClass());
System.out.println();
/* build instance */
System.out.println("build ObjectProperty<String> null instance.");
final ObjectProperty<String> p = ObjectProperty.newInstance(null);
System.out.println();
/* getter */
System.out.println("get value is " + p);
System.out.println();
/* setter */
System.out.println("set value: " + "A");
p.set("A");
System.out.println("get value: " + p);
System.out.println();
System.out.println("set value: " + "B");
p.set("B");
System.out.println("get value: " + p);
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);
System.out.println();
});
System.out.println("set value: " + "C");
p.set("C");
System.out.println("set value: " + "C" + ", no notify because no changed.");
p.set("C");
System.out.println();
/* thread set value */
new Thread(() -> {
try {
System.out.println("sleep 2000 msec...");
Thread.sleep(2000L);
System.out.println("set: " + null);
p.set(null);
}
catch ( InterruptedException ignore ) {
}
}).start();
/* wait until */
System.out.println("waiting until is null.");
p.waitUntilNull();
System.out.println("waiting until is already null, pass through immediately.");
p.waitUntilNull();
System.out.println();
new Thread(() -> {
try {
System.out.println("sleep 2000 msec...");
Thread.sleep(2000L);
System.out.println("set value: " + "D");
p.set("D");
}
catch ( InterruptedException ignore ) {
}
}).start();
System.out.println("waiting until is not null and get value.");
String r = p.waitUntilNotNullAndGet();
System.out.println("return value: " + r);
System.out.println();
new Thread(() -> {
try {
System.out.println("sleep 2000 msec...");
Thread.sleep(2000L);
System.out.println("set value: " + "E");
p.set("E");
}
catch ( InterruptedException ignore ) {
}
}).start();
System.out.println("waiting until is equal to " + "E");
p.waitUntilEqualTo("E");
try {
System.out.println("waiting until is equal to " + "F" + " with timeout 2 sec...");
p.waitUntilEqualTo("F", 2L, TimeUnit.SECONDS);
}
catch ( TimeoutException e ) {
System.out.println("timeout, waiting until is equal to " + "F");
System.out.println(e);
System.out.println();
}
/* compution */
BooleanCompution isNull = p.computeIsNull();
isNull.addChangeListener(v -> {
System.out.println("isNull(): " + v);
});
BooleanCompution isNotNull = p.computeIsNotNull();
isNotNull.addChangeListener(v -> {
System.out.println("isNotNull(): " + v);
});
BooleanCompution isEqualToE = p.computeIsEqualTo("E");
isEqualToE.addChangeListener(v -> {
System.out.println("isEqualTo(E): " + v);
});
System.out.println();
System.out.println("set: " + null);
p.set(null);
System.out.println();
System.out.println("reach end.");
}
catch (InterruptedException ignore) {
}
}
public static void main(String[] args) {
try {
new ObjectPropertyExample().run();
}
catch ( Throwable t ) {
t.printStackTrace();
}
}
}