Skip to content

Commit 5a53462

Browse files
Merge pull request #183 from benjchristensen/groovy-cleanup
Groovy VideoExample improvements
2 parents 84d1ed9 + 8fbae2f commit 5a53462

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

language-adaptors/rxjava-groovy/src/examples/groovy/rx/lang/groovy/examples/VideoExample.groovy

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package rx.lang.groovy.examples;
1818
import rx.Observable;
1919
import rx.Observer;
2020
import rx.Subscription;
21+
import rx.subscriptions.BooleanSubscription;
2122
import rx.util.functions.Func1;
2223
import java.util.concurrent.ExecutorService;
2324
import java.util.concurrent.LinkedBlockingQueue;
@@ -66,8 +67,9 @@ static void main(String[] args) {
6667
* [id:1000, title:video-1000-title, length:5428, bookmark:0,
6768
* rating:[actual:4, average:3, predicted:0]]
6869
*/
69-
def Observable getVideoGridForDisplay(userId) {
70-
getListOfLists(userId).mapMany({ VideoList list ->
70+
Observable getVideoGridForDisplay(userId) {
71+
// take the first 5 lists
72+
getListOfLists(userId).take(5).mapMany({ VideoList list ->
7173
// for each VideoList we want to fetch the videos
7274
list.getVideos()
7375
.take(10) // we only want the first 10 of each list
@@ -103,29 +105,23 @@ def Observable getVideoGridForDisplay(userId) {
103105
*
104106
* Observable<VideoList> is the "push" equivalent to List<VideoList>
105107
*/
106-
def Observable<VideoList> getListOfLists(userId) {
108+
Observable<VideoList> getListOfLists(userId) {
107109
return Observable.create({ observer ->
108-
AtomicBoolean isRunning = new AtomicBoolean(true);
110+
BooleanSubscription subscription = new BooleanSubscription();
109111
// this will happen on a separate thread as it requires a network call
110-
executor.execute(new Runnable() {
111-
def void run() {
112+
executor.execute({
112113
// simulate network latency
113114
Thread.sleep(180);
114115
for(i in 0..15) {
115-
if(!isRunning.get()) {
116-
// we have received an unsubscribe
116+
if(subscription.isUnsubscribed()) {
117117
break;
118118
}
119119
//println("****** emitting list: " + i)
120120
observer.onNext(new VideoList(i))
121121
}
122122
observer.onCompleted();
123-
}
124123
})
125-
return Observable.createSubscription({
126-
// see https://github.com/Netflix/RxJava/issues/173 for a possibly simpler way of doing this
127-
isRunning.set(false);
128-
});
124+
return subscription;
129125
})
130126
}
131127

@@ -139,15 +135,15 @@ class VideoList {
139135
this.listPosition = position
140136
}
141137

142-
def String getListName() {
138+
String getListName() {
143139
return "ListName-" + listPosition
144140
}
145141

146-
def Integer getListPosition() {
142+
Integer getListPosition() {
147143
return listPosition
148144
}
149145

150-
def Observable<Video> getVideos() {
146+
Observable<Video> getVideos() {
151147
return Observable.create({ observer ->
152148
// we already have the videos once a list is loaded
153149
// so we won't launch another thread but return
@@ -170,7 +166,7 @@ class Video {
170166
}
171167

172168
// synchronous
173-
def Observable<Map<String, String>> getMetadata() {
169+
Observable<Map<String, String>> getMetadata() {
174170
// simulate fetching metadata from an in-memory cache
175171
// so it will not asynchronously execute on a thread but
176172
// immediately return an Observable with the data
@@ -184,14 +180,13 @@ class Video {
184180
}
185181

186182
// asynchronous
187-
def Observable<Integer> getBookmark(userId) {
183+
Observable<Integer> getBookmark(userId) {
188184
// simulate fetching the bookmark for this user
189185
// that specifies the last played position if
190186
// this video has been played before
191187
return Observable.create({ observer ->
192188
// this will happen on a separate thread as it requires a network call
193-
executor.execute(new Runnable() {
194-
def void run() {
189+
executor.execute({
195190
// simulate network latency
196191
Thread.sleep(4);
197192
if(randint(6) > 1) {
@@ -202,23 +197,20 @@ class Video {
202197
observer.onNext(randint(4000));
203198
}
204199
observer.onCompleted();
205-
}
206200
})
207201
})
208202
}
209203

210204
// asynchronous
211-
def Observable<VideoRating> getRating(userId) {
205+
Observable<VideoRating> getRating(userId) {
212206
// simulate fetching the VideoRating for this user
213207
return Observable.create({ observer ->
214208
// this will happen on a separate thread as it requires a network call
215-
executor.execute(new Runnable() {
216-
def void run() {
209+
executor.execute({
217210
// simulate network latency
218211
Thread.sleep(10);
219212
observer.onNext(new VideoRating(videoId, userId))
220213
observer.onCompleted();
221-
}
222214
})
223215
})
224216
}
@@ -231,15 +223,15 @@ class VideoRating {
231223
this.userId = userId;
232224
}
233225

234-
def Integer getPredictedStarRating() {
226+
Integer getPredictedStarRating() {
235227
return randint(5)
236228
}
237229

238-
def Integer getAverageStarRating() {
230+
Integer getAverageStarRating() {
239231
return randint(4)
240232
}
241233

242-
def Integer getActualStarRating() {
234+
Integer getActualStarRating() {
243235
return randint(5)
244236
}
245237
}

0 commit comments

Comments
 (0)