我尝试获取并渲染以下类似数据:
原始数据类
@Data @AllArgsConstructor class Category {
String name;
List<String> items;
}
演示文稿类
@Data @AllArgsConstructor class ViewModel {
public static final int TYPE_HEADER = 0;
public static final int TYPE_ITEM = 1;
int type;
String category;
String itemName;
}
以下代码是请求并将订阅数据转换为展示对象的代码。
Observable.create(new Observable.OnSubscribe<Category>() {
@Override public void call(Subscriber<? super Category> subscriber) {
subscriber.onNext(new Category("1", Lists.newArrayList("", "a", "b")));
subscriber.onNext(new Category("2", Lists.newArrayList("")));// this data does not output
subscriber.onNext(new Category("3", Lists.newArrayList("c", "", "d")));
subscriber.onNext(new Category("4", Lists.newArrayList("e", "f", "")));
}
}).flatMap(new Func1<Category, Observable<ViewModel>>() {
@Override public Observable<ViewModel> call(Category category) {
// TODO make this block to one line
// 1. clean response data and transform to ViewModel
List<ViewModel> cleanedItems = Lists.newArrayList(
Observable.from(category.getItems()).filter(new Func1<String, Boolean>() {
@Override public Boolean call(String s) {
return s != null && !s.isEmpty();
}
}).map(new Func1<String, ViewModel>() {
@Override public ViewModel call(String item) {
return new ViewModel(ViewModel.TYPE_ITEM, null, item);
}
}).toBlocking().toIterable());
if (cleanedItems.isEmpty()) {
// 2. case : skip
return Observable.empty();
} else {
// 3. case : add header and cleaned data
return Observable.concat(
Observable.just(new ViewModel(ViewModel.TYPE_HEADER, category.getName(), null)),
Observable.from(cleanedItems));
}
}
}).subscribe(new Action1<ViewModel>() {
@Override public void call(ViewModel viewModel) {
// render data
System.out.println(viewModel.toString());
}
});
输出
ViewModel(type=0, category=1, itemName=null)
ViewModel(type=1, category=null, itemName=a)
ViewModel(type=1, category=null, itemName=b)
ViewModel(type=0, category=3, itemName=null)
ViewModel(type=1, category=null, itemName=c)
ViewModel(type=1, category=null, itemName=d)
ViewModel(type=0, category=4, itemName=null)
ViewModel(type=1, category=null, itemName=e)
ViewModel(type=1, category=null, itemName=f)
我试图将1、2、3(在注释中)的语句写成一行(或更易读的方式),但是我不知道该怎么做。
defaultIfEmpty
运算符似乎在这种情况下无法使用。
有没有人有什么想法?
cache
运算符很有用。谢谢! - Yuki Yoshida