Created
May 14, 2015 11:52
-
-
Save akarnokd/158be6da8d981e3fde7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package blog; | |
import java.util.List; | |
import rx.Observable; | |
public class WidgetAndArticles { | |
static final class Widget { | |
String articleName; | |
String articleUrl; | |
int type; | |
public Widget(int type) { | |
this.type = type; | |
} | |
@Override | |
public String toString() { | |
return "Widget [articleName=" + articleName + ", articleUrl=" | |
+ articleUrl + ", type=" + type + "]"; | |
} | |
} | |
static final class Article { | |
@Override | |
public String toString() { | |
return "Article [parent=" + parent + ", name=" + name + ", url=" | |
+ url + "]"; | |
} | |
Widget parent; | |
String name; | |
String url; | |
} | |
static Observable<List<Widget>> getWidgets(String token) { | |
return Observable.range(1, 10).map(Widget::new).toList(); | |
} | |
static Observable<List<Article>> getArticles(String token, int type) { | |
return Observable.range(1, 3).map(i -> { | |
Article a = new Article(); | |
a.name = "Article " + i + " for type " + type; | |
a.url = "http://.../" + type + "/" + i; | |
return a; | |
}).toList(); | |
} | |
public static void main(String[] args) { | |
getWidgets("token") | |
.flatMapIterable(w -> w) | |
.flatMap(w -> | |
getArticles("token", w.type) | |
.flatMapIterable(a -> a) | |
.doOnNext(a -> System.out.println("Inserting " + a)) | |
.doOnNext(a -> { | |
w.articleName = a.name; | |
w.articleUrl = a.url; | |
}) | |
.takeLast(1) | |
.map(a -> w) | |
) | |
.toList() | |
.subscribe(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment