I hava a Maybe<>
source and some action that I want to execute with this value if maybe is not empty:
// Maybe<T> maybe();
// Completable action(T value);
return maybe().flatMapCompletable(val -> action(val));
but when maybe is empty I want to get 'completed' completable:
return Completable.complete();
How to make this switch: if maybe is not empty get one completable, otherwise another?
Well, I have written two tests, and I think this behaviour you want is given out of the box. The maybeTest will complete without calling saveToDb. maybeTest2 will call saveToDb and flatten the value back and complete.
@Test
public void maybeTest() throws Exception {
Completable completable = Maybe.<Integer>empty()
.flatMapCompletable(o -> {
System.out.println(o);
return saveToDb(5);
});
completable.test().await().assertComplete();
}
@Test
public void maybeTest2() throws Exception {
Completable completable = Maybe.just(5)
.flatMapCompletable(o -> {
System.out.println(o);
return saveToDb(5);
});
completable.test().await().assertComplete();
}
private Completable saveToDb(long value) {
return Completable.complete();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With