-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
c3_FilteringSequence.java
129 lines (114 loc) · 3.92 KB
/
c3_FilteringSequence.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
import org.junit.jupiter.api.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
/**
* Sequence may produce many elements, but we are not always interested in all of them. In this chapter we will learn
* how to filter elements from a sequence.
*
* Read first:
*
* https://projectreactor.io/docs/core/release/reference/#which.filtering
*
* Useful documentation:
*
* https://projectreactor.io/docs/core/release/reference/#which-operator
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html
*
* @author Stefan Dragisic
*/
public class c3_FilteringSequence extends FilteringSequenceBase {
/**
* Collect most popular girl names, no longer then 4 characters.
*/
@Test
public void girls_are_made_of_sugar_and_spice() {
Flux<String> shortListed = popular_girl_names_service()
//todo: change this line only
;
StepVerifier.create(shortListed)
.expectNext("Emma", "Ava", "Mia", "Luna", "Ella")
.verifyComplete();
}
/**
* `mashed_data_service()` returns sequence of generic objects.
* Without using `filter()` operator, collect only objects that are instance of `String`
*/
@Test
public void needle_in_a_haystack() {
Flux<String> strings = null;
mashed_data_service()
//todo: change this line only
;
StepVerifier.create(strings)
.expectNext("1", "String.class")
.verifyComplete();
}
/**
* This service may return duplicated data. Filter out all the duplicates from the sequence.
*/
@Test
public void economical() {
Flux<String> items = duplicated_records_service()
//todo: change this line only, use only one operator
;
StepVerifier.create(items)
.expectNext("1", "2", "3", "4", "5")
.verifyComplete();
}
/**
* This service returns many elements, but you are only interested in the first one.
* Also, service is very fragile, if you pull more than needed, you may brake it.
*
* This time no blocking. Use only one operator.
*/
@Test
public void watch_out_for_the_spiders() {
//todo: change code as you need
Mono<String> firstResult = Mono.empty();
fragile_service();
//don't change code below
StepVerifier.create(firstResult)
.expectNext("watch_out")
.verifyComplete();
}
/**
* `number_service()` returns 300 numbers, but you only need first 100 numbers.
*/
@Test
public void dont_take_more_then_you_need() {
Flux<Integer> numbers = number_service()
//todo: change this line only
;
StepVerifier.create(numbers)
.expectNextCount(100)
.verifyComplete();
}
/**
* `number_service()` returns 300 numbers, but you only need last 100 numbers.
*/
@Test
public void not_a_binary_search() {
Flux<Integer> numbers = number_service()
//todo: change this line only
;
StepVerifier.create(numbers)
.expectNextMatches(i -> i >= 200)
.expectNextCount(99)
.verifyComplete();
}
/**
* `number_service()` returns 300 numbers, but you only need 100 numbers, from the middle.
*/
@Test
public void golden_middle() {
Flux<Integer> numbers = number_service()
//todo: do your changes here
;
StepVerifier.create(numbers)
.expectNextMatches(i -> i >= 100)
.expectNextCount(99)
.verifyComplete();
}
}