将ListenableFuture链转换为等效的RxJava结构
用番石榴 假设我有以下类别:将ListenableFuture链转换为等效的RxJava结构,java,rx-java,guava,future,rx-java2,Java,Rx Java,Guava,Future,Rx Java2,用番石榴 假设我有以下类别: public class FooService { ListenableFuture<Foo> getFoo() { //code to create callable, then return listeningExecutorService.submit(fooCallable); } } public class BarService { ListenableFuture<Bar>
public class FooService {
ListenableFuture<Foo> getFoo() {
//code to create callable, then
return listeningExecutorService.submit(fooCallable);
}
}
public class BarService {
ListenableFuture<Bar> getBar(Foo foo) {
//code to create callable, then
return listeningExecutorService.submit(barCallable);
}
}
然后像这样应用转换:
AsyncFunction<Foo, Bar> fooToBar = new AsyncFunction<Foo, Bar>() {
@Override
ListenableFuture<Bar> apply(Foo resultantFoo) {
return barService.get(resultantFoo);
}
};
public ListenableFuture<Bar> combinedFooToBar() {
ListenableFuture<Foo> futureFoo = fooService.get();
return Futures.transformAsync(futureFoo, fooToBar);
}
public-ListenableFuture-combined-footobar(){
listenablefuturefoo=fooService.get();
returnfutures.transformAsync(futureFoo、fooToBar);
}
问题:如果要将这些类和转换函数转换为RxJava,它们的等效语法是什么?假设我们想要将FooService
和BarService
转换为适当的RxJava结构。假设我们希望使用调用FooService
的结果作为BarService
的参数来链接异步任务
注意:我刚刚开始学习RxJava语法。当我完成语法学习后,我将尝试自己回答这个问题。但是,在此期间,如果有人想回答,欢迎回答。番石榴代码翻译成RxJava2代码,如下所示: FooService.java
public class FooService {
Observable<Foo> getFoo() {
return Observable.fromCallable(new Callable<Foo>() {
@Override
public Foo call() throws Exception {
return new Foo();
}
});
}
}
public class BarService {
Observable<Bar> getBar(final Foo foo) {
return Observable.fromCallable(new Callable<Bar>() {
@Override
public Bar call() throws Exception {
return new Bar(foo);
}
});
}
}
public class FooBarService {
private final FooService fooService;
private final BarService barService;
public FooBarService(FooService fooService, BarService barService) {
this.fooService = fooService;
this.barService = barService;
}
Observable<Bar> getFooBar() {
return fooService.getFoo()
.concatMap(new Function<Foo, ObservableSource<? extends Bar>>() {
@Override
public ObservableSource<? extends Bar> apply(@NonNull Foo foo) throws Exception {
return barService.getBar(foo);
}
});
}
}
公共类服务{
可观测的getFoo(){
返回Observable.fromCallable(newcallable()){
@凌驾
public Foo call()引发异常{
返回新的Foo();
}
});
}
}
BarService.java
public class FooService {
Observable<Foo> getFoo() {
return Observable.fromCallable(new Callable<Foo>() {
@Override
public Foo call() throws Exception {
return new Foo();
}
});
}
}
public class BarService {
Observable<Bar> getBar(final Foo foo) {
return Observable.fromCallable(new Callable<Bar>() {
@Override
public Bar call() throws Exception {
return new Bar(foo);
}
});
}
}
public class FooBarService {
private final FooService fooService;
private final BarService barService;
public FooBarService(FooService fooService, BarService barService) {
this.fooService = fooService;
this.barService = barService;
}
Observable<Bar> getFooBar() {
return fooService.getFoo()
.concatMap(new Function<Foo, ObservableSource<? extends Bar>>() {
@Override
public ObservableSource<? extends Bar> apply(@NonNull Foo foo) throws Exception {
return barService.getBar(foo);
}
});
}
}
公共类酒吧服务{
可观察的getBar(最终Foo-Foo){
返回Observable.fromCallable(newcallable()){
@凌驾
public Bar调用()引发异常{
返回新条(foo);
}
});
}
}
FooBarService.java
public class FooService {
Observable<Foo> getFoo() {
return Observable.fromCallable(new Callable<Foo>() {
@Override
public Foo call() throws Exception {
return new Foo();
}
});
}
}
public class BarService {
Observable<Bar> getBar(final Foo foo) {
return Observable.fromCallable(new Callable<Bar>() {
@Override
public Bar call() throws Exception {
return new Bar(foo);
}
});
}
}
public class FooBarService {
private final FooService fooService;
private final BarService barService;
public FooBarService(FooService fooService, BarService barService) {
this.fooService = fooService;
this.barService = barService;
}
Observable<Bar> getFooBar() {
return fooService.getFoo()
.concatMap(new Function<Foo, ObservableSource<? extends Bar>>() {
@Override
public ObservableSource<? extends Bar> apply(@NonNull Foo foo) throws Exception {
return barService.getBar(foo);
}
});
}
}
公共类FooBarService{
私人最终餐饮服务;
私人最终酒吧服务;
公共FooBarService(FooService FooService,BarService BarService){
this.fooService=fooService;
this.barService=barService;
}
可观察的getFooBar(){
return fooService.getFoo()
.concatMap(新功能