Skip to content

Instantly share code, notes, and snippets.

@akarnokd
Created May 14, 2015 11:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akarnokd/158be6da8d981e3fde7a to your computer and use it in GitHub Desktop.
Save akarnokd/158be6da8d981e3fde7a to your computer and use it in GitHub Desktop.
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