forked from aNNiMON/Lightweight-Stream-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamTest.java
More file actions
378 lines (317 loc) · 13.5 KB
/
StreamTest.java
File metadata and controls
378 lines (317 loc) · 13.5 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* StreamTest.java
* JMUnit based test
*
* Created on Jan 4, 2015, 10:41:57 PM
*/
package test;
import com.annimon.stream.Collector;
import com.annimon.stream.Comparator;
import com.annimon.stream.Optional;
import com.annimon.stream.Stream;
import com.annimon.stream.function.BiConsumer;
import com.annimon.stream.function.BiFunction;
import com.annimon.stream.function.Consumer;
import com.annimon.stream.function.Function;
import com.annimon.stream.function.Predicate;
import com.annimon.stream.function.Supplier;
import java.util.Random;
import java.util.Vector;
import jmunit.framework.cldc10.*;
/**
* @author aNNiMON
*/
public class StreamTest extends TestCase {
private static final Integer[] array10 = new Integer[10];
private static final Vector list10 = new Vector();
private static final Vector listRandom10 = new Vector();
private PrintConsumer pc1, pc2;
public StreamTest() {
super(14, "StreamTest");
final Random rnd = new Random();
for (int i = 0; i < 10; i++) {
array10[i] = new Integer(i);
list10.addElement(new Integer(i));
listRandom10.addElement(new Integer(rnd.nextInt(10)));
}
}
public void setUp() throws Throwable {
pc1 = new PrintConsumer();
pc2 = new PrintConsumer();
}
public void test(int testNumber) throws Throwable {
switch (testNumber) {
case 0: limit(); break;
case 1: skip(); break;
case 2: limitAndSkip(); break;
case 3: filter(); break;
case 4: map(); break;
case 5: flatMap(); break;
case 6: distinct(); break;
case 7: sorted(); break;
case 8: collect(); break;
case 9: minAndMax(); break;
case 10: count(); break;
case 11: match(); break;
case 12: reduce(); break;
case 13: findFirst(); break;
}
}
public void limit() {
Stream.of(array10).limit(2).forEach(pc1.getConsumer());
assertEquals("01", pc1.toString());
Stream.of(list10).limit(2).forEach(pc2.getConsumer());
assertEquals(pc1.out(), pc2.out());
}
public void skip() {
Stream.of(array10).skip(7).forEach(pc1.getConsumer());
assertEquals("789", pc1.toString());
Stream.of(list10).skip(7).forEach(pc2.getConsumer());
assertEquals(pc1.out(), pc2.out());
}
public void limitAndSkip() {
Stream.of(array10).skip(2).limit(5).forEach(pc1.getConsumer());
assertEquals("23456", pc1.out());
Stream.of(list10).limit(5).skip(2).forEach(pc1.getConsumer());
assertEquals("234", pc1.out());
Stream.of(list10).skip(8).limit(15).forEach(pc1.getConsumer());
assertEquals("89", pc1.out());
}
public void filter() {
Stream.of(array10).filter(pc1.getFilter(2)).forEach(pc1.getConsumer());
assertEquals("02468", pc1.toString());
Stream.of(list10).filter(pc2.getFilter(2)).forEach(pc2.getConsumer());
assertEquals("02468", pc1.toString());
assertEquals(pc1.out(), pc2.out());
Stream.of(array10).filter(Predicate.Util.or(pc1.getFilter(2), pc1.getFilter(3))).forEach(pc1.getConsumer());
assertEquals("0234689", pc1.out());
Stream.of(list10).filter(Predicate.Util.and(pc1.getFilter(2), pc1.getFilter(3))).forEach(pc1.getConsumer());
assertEquals("06", pc1.out());
Stream.of(list10).filter(pc1.getFilter(2)).filter(pc1.getFilter(3)).forEach(pc1.getConsumer());
assertEquals("06", pc1.out());
}
public void map() {
final Function mapToString = new Function() {
public Object apply(Object t) {
final int value = ((Integer) t).intValue();
return "[" + ((int) Math.sqrt(value)) + "]";
}
};
final Function mapToInt = new Function() {
public Object apply(Object o) {
String t = (String) o;
final String str = t.substring(1, t.length() - 1);
final int value = Integer.parseInt(str);
return new Integer(value * value);
}
};
Stream.of(new int[] {4, 9, 16}).map(mapToString).forEach(pc1.getConsumer());
assertEquals("[2][3][4]", pc1.out());
Stream s1 = Stream.of(new int[] {25, 64, 625});
Stream s2 = Stream.of(new String[] {"[5]", "[8]", "[25]"});
s2.forEach(pc2.getConsumer());
s1.map(mapToString).forEach(pc1.getConsumer());
assertEquals(pc1.out(), pc2.out());
s2.map(mapToInt).forEach(pc2.getConsumer());
s1.forEach(pc1.getConsumer());
assertEquals(pc1.out(), pc2.out());
final Function mapPlus1 = new Function() {
public Object apply(Object x) {
final int value = ((Integer) x).intValue();
return new Integer(value + 1);
}
};
final Function mapPlus2 = Function.Util.andThen(mapPlus1, mapPlus1);
Stream.ofRange(-10, 0)
.map(mapPlus2)
.map(mapPlus2)
.map(mapPlus2)
.map(mapPlus2)
.map(mapPlus2)
.forEach(pc1.getConsumer());
assertEquals("0123456789", pc1.out());
}
public void flatMap() {
Stream.ofRange(2, 4)
.flatMap(new Function() {
public Object apply(final Object i) {
return Stream.ofRange(2, 5)
.filter(pc1.getFilter(2))
.map(new Function() {
public Object apply(Object p) {
final int v1 = ((Integer) i).intValue();
final int v2 = ((Integer) p).intValue();
return i + "*" + p + "=" + (v1*v2) + "\n";
}
});
}
})
.forEach(pc1.getConsumer());
assertEquals("2*2=4\n2*4=8\n3*2=6\n3*4=12\n", pc1.out());
}
public void distinct() {
long count = Stream.of(new int[] {1, 1, 2, 3, 5, 3, 2, 1, 1, -1})
.distinct().filter(new Predicate() {
public boolean test(Object value) {
final int v = ((Integer) value).intValue();
return v == 1;
}
}).count();
assertEquals(1, count);
Stream.of(new int[] {1, 1, 2, 3, 5, 3, 2, 1, 1, -1})
.distinct().sorted(intComparator).forEach(pc1.getConsumer());
assertEquals("-11235", pc1.out());
}
public void sorted() {
Stream.of(new int[] {6, 3, 9, 0, -7, 19}).sorted(intComparator).forEach(pc1.getConsumer());
assertEquals("-7036919", pc1.out());
Stream.of(array10).sorted(intComparator).forEach(pc1.getConsumer());
Stream.of(list10).sorted(intComparator).forEach(pc2.getConsumer());
assertEquals(pc1.out(), pc2.out());
}
public void collect() {
final Supplier joinSupplier = new Supplier() {
public Object get() {
return new StringBuffer();
}
};
final BiConsumer joinAccumulator = new BiConsumer() {
public void accept(Object t, Object u) {
((StringBuffer) t).append(u);
}
};
final Collector join = new Collector() {
public Supplier supplier() {
return joinSupplier;
}
public BiConsumer accumulator() {
return joinAccumulator;
}
public Function finisher() {
return new Function() {
public Object apply(Object value) {
return value.toString();
}
};
}
};
String text = (String) Stream.ofRange(0, 10)
.map(new Function() {
public Object apply(Object value) {
final int v = ((Integer) value).intValue();
return Integer.toString(v);
}
})
.collect(join);
assertEquals("0123456789", text);
text = Stream.of(new String[] {"a", "b", "c", "def", "", "g"})
.collect(joinSupplier, joinAccumulator).toString();
assertEquals("abcdefg", text);
}
public void minAndMax() {
Optional min, max;
min = Stream.of(list10).min(intComparator);
max = Stream.of(list10).max(intComparator);
assertEquals(0, ((Integer) min.get()).intValue());
assertEquals(9, ((Integer) max.get()).intValue());
min = Stream.of(list10).filter(pc1.getFilter(2)).min(intComparator);
max = Stream.of(list10).filter(pc1.getFilter(2)).max(intComparator);
assertEquals(0, ((Integer) min.get()).intValue());
assertEquals(8, ((Integer) max.get()).intValue());
}
public void count() {
long count = Stream.ofRange(10000000000L, 10000010000L).count();
assertEquals(10000, count);
long count1 = Stream.of(array10).peek(pc1.getConsumer()).count();
long count2 = Stream.of(list10).peek(pc2.getConsumer()).count();
assertEquals(count1, count2);
assertEquals(pc1.out(), pc2.out());
}
public void match() {
boolean match;
match = Stream.of(array10).anyMatch(pc1.getFilter(2));
assertEquals(true, match);
match = Stream.of(new int[] {2, 3, 5, 8, 13}).anyMatch(pc1.getFilter(10));
assertEquals(false, match);
match = Stream.of(array10).allMatch(pc1.getFilter(2));
assertEquals(false, match);
match = Stream.of(new int[] {2, 4, 6, 8, 10}).allMatch(pc1.getFilter(2));
assertEquals(true, match);
match = Stream.of(array10).noneMatch(pc1.getFilter(2));
assertEquals(false, match);
match = Stream.of(new int[] {2, 3, 5, 8, 13}).noneMatch(pc1.getFilter(10));
assertEquals(true, match);
}
public void reduce() {
final BiFunction add = new BiFunction() {
public Object apply(Object value1, Object value2) {
final int x = ((Integer) value1).intValue();
final int y = ((Integer) value2).intValue();
return new Integer(x + y);
}
};
int result;
result = ((Integer) Stream.of(array10).reduce(new Integer(0), add)).intValue();
assertEquals(45, result);
result = ((Integer) Stream.of(array10).reduce(new Integer(-45), add)).intValue();
assertEquals(0, result);
Optional optional;
optional = Stream.of(array10).reduce(add);
assertTrue(optional.isPresent());
assertNotNull(optional.get());
assertEquals(45, ((Integer) optional.get()).intValue());
optional = Stream.of(new int[] {1, 3, 5, 7, 9}).filter(pc1.getFilter(2)).reduce(add);
assertFalse(optional.isPresent());
assertEquals(119, ((Integer) optional.orElse(new Integer(119))).intValue());
}
public void findFirst() {
Optional optional;
optional = Stream.of(array10).findFirst();
assertTrue(optional.isPresent());
assertNotNull(optional.get());
assertEquals(0, ((Integer) optional.get()).intValue());
optional = Stream.of(new int[] {1, 3, 5, 7, 9}).filter(pc1.getFilter(2)).findFirst();
assertFalse(optional.isPresent());
optional = Stream.ofRange(1, 1000000)
.filter(pc1.getFilter(6))
.peek(pc1.getConsumer())
.findFirst();
assertTrue(optional.isPresent());
assertNotNull(optional.get());
assertEquals(6, ((Integer) optional.get()).intValue());
assertEquals("6", pc1.out());
}
private class PrintConsumer {
private final StringBuffer out = new StringBuffer();
public Consumer getConsumer() {
return new Consumer() {
public void accept(Object value) {
out.append(value);
}
};
}
private Predicate getFilter(final int val) {
return new Predicate() {
public boolean test(Object value) {
int v = ((Integer) value).intValue();
return (v % val == 0);
}
};
}
public String out() {
String result = out.toString();
out.setLength(0);
return result;
}
public String toString() {
return out.toString();
}
}
private static final Comparator intComparator = new Comparator() {
public int compare(Object o1, Object o2) {
final int x = ((Integer) o1).intValue();
final int y = ((Integer) o2).intValue();
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
};
}