-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapPropertyExample.java
180 lines (139 loc) · 4.28 KB
/
MapPropertyExample.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package example;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.shimizukenta.property.BooleanCompution;
import com.shimizukenta.property.IntegerCompution;
import com.shimizukenta.property.MapProperty;
import com.shimizukenta.property.SetCompution;
public class MapPropertyExample implements Runnable {
public MapPropertyExample() {
/* Nothing */
}
@Override
public void run() {
try {
System.out.println("run: " + this.getClass());
System.out.println();
/* build instance */
System.out.println("build MapProperty<String, Integer> empty instance.");
final MapProperty<String, Integer> p = MapProperty.newInstance();
System.out.println();
/* getter */
System.out.println("get value is " + p);
System.out.println();
/* setter */
System.out.println("put value: " + "{A, 1}");
p.put("A", 1);
System.out.println("get value: " + p);
System.out.println();
System.out.println("put value: " + "{B, 2}");
p.put("B", 2);
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("put value: " + "{C, 3}");
p.put("C", 3);
System.out.println("put value: " + "{C, 3}" + ", no notify because no changed.");
p.put("C", 3);
System.out.println();
System.out.println("remove key: " + "B");
p.remove("B");
/* thread set value */
new Thread(() -> {
try {
System.out.println("sleep 2000 msec...");
Thread.sleep(2000L);
System.out.println("clear");
p.clear();
}
catch ( InterruptedException ignore ) {
}
}).start();
/* wait until */
System.out.println("waiting until empty");
p.waitUntilIsEmpty();
System.out.println("waiting until already empty, pass through immediately.");
p.waitUntilIsEmpty();
System.out.println();
new Thread(() -> {
try {
System.out.println("sleep 2000 msec...");
Thread.sleep(2000L);
System.out.println("put value: " + "{D, 4}");
p.put("D", 4);
}
catch ( InterruptedException ignore ) {
}
}).start();
System.out.println("waiting until is not empty");
p.waitUntilIsNotEmpty();
new Thread(() -> {
try {
System.out.println("sleep 2000 msec...");
Thread.sleep(2000L);
System.out.println("put value: " + "{E, 5}");
p.put("E", 5);
}
catch ( InterruptedException ignore ) {
}
}).start();
System.out.println("waiting until containsKeyAndGet: " + "E");
Integer r = p.waitUntilContainsKeyAndGet("E");
System.out.println("value E: " + r);
System.out.println();
try {
System.out.println("waiting until conatinsKeyAndGet " + "F" + " with timeout 2 sec...");
p.waitUntilContainsKeyAndGet("F", 2L, TimeUnit.SECONDS);
}
catch ( TimeoutException e ) {
System.out.println("timeout, waiting until containsKey " + "F");
System.out.println(e);
System.out.println();
}
/* compution */
BooleanCompution isEmpty = p.computeIsEmpty();
isEmpty.addChangeListener(v -> {
System.out.println("isEmpty: " + v);
});
BooleanCompution isNotEmpty = p.computeIsNotEmpty();
isNotEmpty.addChangeListener(v -> {
System.out.println("isNotEmpty: " + v);
});
BooleanCompution containsKeyD = p.computeContainsKey("D");
containsKeyD.addChangeListener(v -> {
System.out.println("containsKeyD: " + v);
});
SetCompution<String> keySet = p.computeKeySet();
keySet.addChangeListener(v -> {
System.out.println("keySet: " + v);
});
IntegerCompution size = p.computeSize();
size.addChangeListener(v -> {
System.out.println("size: " + v);
});
System.out.println();
System.out.println("remove D");
p.remove("D");
System.out.println();
System.out.println("clear");
p.clear();
System.out.println();
System.out.println("reach end");
}
catch ( InterruptedException ignore ) {
}
}
public static void main(String[] args) {
try {
new MapPropertyExample().run();
}
catch ( Throwable t ) {
t.printStackTrace();
}
}
}