-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListPropertyExample.java
171 lines (131 loc) · 3.91 KB
/
ListPropertyExample.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
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.ListProperty;
public class ListPropertyExample implements Runnable {
public ListPropertyExample() {
/* Nothing */
}
@Override
public void run() {
try {
System.out.println("run: " + this.getClass());
System.out.println();
/* build instance */
System.out.println("build ListProperty<String> empty instance.");
final ListProperty<String> p = ListProperty.newInstance();
System.out.println();
/* getter */
System.out.println("get value is " + p);
System.out.println();
/* setter */
System.out.println("add value: " + "A");
p.add("A");
System.out.println("get value: " + p);
System.out.println();
System.out.println("add value: " + "B");
p.add("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("add value: " + "C");
p.add("C");
System.out.println("add value: " + "C");
p.add("C");
System.out.println();
System.out.println("remove value: " + "B");
p.remove("B");
System.out.println("removeIf equals: " + "C");
p.removeIf(s -> s.equals("C"));
/* 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 is 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("add value: " + "D");
p.add("D");
}
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("add value: " + "E");
p.add("E");
}
catch ( InterruptedException ignore ) {
}
}).start();
System.out.println("waiting until value is contains: " + "E");
p.waitUntilContains("E");
try {
System.out.println("waiting until conatins " + "F" + " with timeout 2 sec...");
p.waitUntilContains("F", 2L, TimeUnit.SECONDS);
}
catch ( TimeoutException e ) {
System.out.println("timeout, waiting until contains " + "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 containsE = p.computeContains("E");
containsE.addChangeListener(v -> {
System.out.println("contains(E): " + v);
});
IntegerCompution size = p.computeSize();
size.addChangeListener(v -> {
System.out.println("size(): " + v);
});
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 ListPropertyExample().run();
}
catch ( Throwable t ) {
t.printStackTrace();
}
}
}