Created
September 11, 2015 08:19
-
-
Save r4zzz4k/2df68a7079586b04aa7e to your computer and use it in GitHub Desktop.
Different RxJava utilities
This file contains 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 com.ebooks.ebookreader.utils.rx; | |
import rx.Observable; | |
public class TransformerMapCatch<T, R> implements Observable.Transformer<T, R> { | |
private FuncThrowable1<? super T, ? extends R> mFunc; | |
public TransformerMapCatch(FuncThrowable1<? super T, ? extends R> func) { | |
mFunc = func; | |
} | |
@Override | |
public Observable<R> call(Observable<T> rObservable) { | |
return rObservable.flatMap(object -> { | |
try { | |
return Observable.just(mFunc.call(object)); | |
} catch (Throwable e) { | |
return Observable.error(e); | |
} | |
}); | |
} | |
} |
This file contains 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 example; | |
import rx.Observable; | |
import rx.schedulers.Schedulers; | |
import static com.ebooks.ebookreader.utils.rx.UtilsRx.mapCatch; | |
... | |
Observable.just(node) | |
.compose(mapCatch(node -> new FileOutputStream(pair.second))) | |
... |
This file contains 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 com.ebooks.ebookreader.utils.rx; | |
import android.util.Pair; | |
import rx.Observable; | |
public final class UtilsRx { | |
private UtilsRx() {} | |
public static <T, R> Observable.Transformer<T, R> mapCatch(FuncThrowable1<? super T, ? extends R> func) { | |
return new TransformerMapCatch<>(func); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment