forked from jhusain/learnrxjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObservableSolutions.java
More file actions
151 lines (135 loc) · 4.75 KB
/
ObservableSolutions.java
File metadata and controls
151 lines (135 loc) · 4.75 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
package learnrxjava;
import learnrxjava.types.JSON;
import learnrxjava.types.Movies;
import rx.Observable;
public class ObservableSolutions extends ObservableExercises {
/**
* Return an Observable that emits a single value "Hello World"
*
* @return "Hello World!"
*/
public Observable<String> exerciseHello() {
return Observable.just("Hello World!");
}
/**
* Transform the incoming Observable from "Hello" to "Hello [Name]" where [Name] is your name.
*
* @param "Hello Name!"
*/
public Observable<String> exerciseMap(Observable<String> hello) {
return hello.map(t -> t + " Ben!");
}
/**
* Given a stream of numbers, choose the even ones and return a stream like:
* <p>
* 2-Even
* 4-Even
* 6-Even
*/
public Observable<String> exerciseFilterMap(Observable<Integer> nums) {
return nums.filter(i -> i % 2 == 0).map(i -> i + "-Even");
}
/**
* Flatten out all video in the stream of Movies into a stream of videoIDs
*
* @param movieLists
* @return Observable of Integers of Movies.videos.id
*/
public Observable<Integer> exerciseConcatMap(Observable<Movies> movies) {
return movies.<Integer> concatMap(ml -> {
return ml.videos.map(v -> v.id);
});
}
/**
* Flatten out all video in the stream of Movies into a stream of videoIDs
*
* Use flatMap this time instead of concatMap. In Observable streams
* it is almost always flatMap that is wanted, not concatMap as flatMap
* uses merge instead of concat and allows multiple concurrent streams
* whereas concat only does one at a time.
*
* We'll see more about this later when we add concurrency.
*
* @param movieLists
* @return Observable of Integers of Movies.videos.id
*/
public Observable<Integer> exerciseFlatMap(Observable<Movies> movies) {
return movies.<Integer> flatMap(ml -> {
return ml.videos.map(v -> v.id);
});
}
/**
* Retrieve the largest number.
*
* Use reduce to select the maximum value in a list of numbers.
*/
public Observable<Integer> exerciseReduce(Observable<Integer> nums) {
return nums.reduce((max, item) -> {
if (item > max) {
return item;
} else {
return max;
}
});
}
/**
* Retrieve the id, title, and smallest box art url for every video.
*
* Now let's try combining reduce() with our other functions to build more complex queries.
*
* This is a variation of the problem we solved earlier, where we retrieved the url of the boxart with a
* width of 150px. This time we'll use reduce() instead of filter() to retrieve the _smallest_ box art in
* the boxarts list.
*
* See Exercise 19 of ComposableListExercises
*/
public Observable<JSON> exerciseMovie(Observable<Movies> movies) {
return movies.flatMap(ml -> {
return ml.videos.<JSON> flatMap(v -> {
return v.boxarts.reduce((max, box) -> {
int maxSize = max.height * max.width;
int boxSize = box.height * box.width;
if (boxSize < maxSize) {
return box;
} else {
return max;
}
}).map(maxBoxart -> {
return json("id", v.id, "title", v.title, "boxart", maxBoxart.url);
});
});
});
}
/**
* Combine 2 streams into pairs using zip.
*
* a -> "one", "two", "red", "blue"
* b -> "fish", "fish", "fish", "fish"
* output -> "one fish", "two fish", "red fish", "blue fish"
*/
public Observable<String> exerciseZip(Observable<String> a, Observable<String> b) {
return Observable.zip(a, b, (x, y) -> x + " " + y);
}
/**
* Don't modify any values in the stream but do handle the error
* and replace it with "default-value".
*/
public Observable<String> handleError(Observable<String> data) {
return data.onErrorResumeNext(Observable.just("default-value"));
}
/**
* The data stream fails intermittently so return the stream
* with retry capability.
*/
public Observable<String> retry(Observable<String> data) {
return data.retry();
}
// This function can be used to build JSON objects within an expression
private static JSON json(Object... keyOrValue) {
JSON json = new JSON();
for (int counter = 0; counter < keyOrValue.length; counter += 2) {
json.put((String) keyOrValue[counter], keyOrValue[counter + 1]);
}
return json;
}
}