@Override public Single<Integer> apply(Maybe<Integer> f) throws Exception { return f.switchIfEmpty(Single.just(2)); } });
@Override public Maybe<Integer> apply(Maybe<Integer> f) throws Exception { return f.switchIfEmpty(Maybe.just(2)); } });
/** * Returns a Maybe that emits the item emitted by the source Maybe or a specified default item * if the source Maybe is empty. * <p> * Note that the result Maybe is semantically equivalent to a {@code Single}, since it's guaranteed * to emit exactly one item or an error. See {@link #toSingle(Object)} for a method with equivalent * behavior which returns a {@code Single}. * <p> * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/defaultIfEmpty.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code defaultIfEmpty} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param defaultItem * the item to emit if the source Maybe emits no items * @return a Maybe that emits either the specified default item if the source Maybe emits no * items, or the items emitted by the source Maybe * @see <a href="http://reactivex.io/documentation/operators/defaultifempty.html">ReactiveX operators documentation: DefaultIfEmpty</a> */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe<T> defaultIfEmpty(T defaultItem) { ObjectHelper.requireNonNull(defaultItem, "item is null"); return switchIfEmpty(just(defaultItem)); }
/** * Returns a Maybe that emits the item emitted by the source Maybe or a specified default item * if the source Maybe is empty. * <p> * Note that the result Maybe is semantically equivalent to a {@code Single}, since it's guaranteed * to emit exactly one item or an error. See {@link #toSingle(Object)} for a method with equivalent * behavior which returns a {@code Single}. * <p> * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/defaultIfEmpty.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code defaultIfEmpty} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param defaultItem * the item to emit if the source Maybe emits no items * @return a Maybe that emits either the specified default item if the source Maybe emits no * items, or the items emitted by the source Maybe * @see <a href="http://reactivex.io/documentation/operators/defaultifempty.html">ReactiveX operators documentation: DefaultIfEmpty</a> */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Maybe<T> defaultIfEmpty(T defaultItem) { ObjectHelper.requireNonNull(defaultItem, "item is null"); return switchIfEmpty(just(defaultItem)); }
@Test public void emptyOtherToo() { Maybe.empty().switchIfEmpty(Maybe.empty()) .test().assertResult(); }
@Test public void isDisposed() { PublishProcessor<Integer> pp = PublishProcessor.create(); TestHelper.checkDisposed(pp.singleElement().switchIfEmpty(Maybe.just(2))); }
@Test public void isDisposed() { PublishProcessor<Integer> pp = PublishProcessor.create(); TestHelper.checkDisposed(pp.singleElement().switchIfEmpty(Single.just(2))); }
@Test public void nonEmpty() { Maybe.just(1).switchIfEmpty(Maybe.just(2)).test().assertResult(1); }
@Test public void nonEmpty() { Maybe.just(1).switchIfEmpty(Single.just(2)).test().assertResult(1); }
@Test public void empty() { Maybe.<Integer>empty().switchIfEmpty(Maybe.just(2)).test().assertResult(2); }
@Test public void empty() { Maybe.<Integer>empty().switchIfEmpty(Single.just(2)).test().assertResult(2); }
@Test public void errorOther() { Maybe.empty().switchIfEmpty(Single.<Integer>error(new TestException())) .test().assertFailure(TestException.class); }
@SuppressWarnings("rawtypes") @Test public void source() { assertSame(Maybe.empty(), ((HasUpstreamMaybeSource)(Maybe.<Integer>empty().switchIfEmpty(Single.just(1)))).source() ); } }
@Test public void error() { Maybe.<Integer>error(new TestException()).switchIfEmpty(Single.just(2)) .test().assertFailure(TestException.class); }
@Test public void error() { Maybe.<Integer>error(new TestException()).switchIfEmpty(Maybe.just(2)) .test().assertFailure(TestException.class); }
@Test public void errorOther() { Maybe.empty().switchIfEmpty(Maybe.<Integer>error(new TestException())) .test().assertFailure(TestException.class); }
@Test public void emptyCancelRace() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final PublishProcessor<Integer> pp = PublishProcessor.create(); final TestObserver<Integer> to = pp.singleElement().switchIfEmpty(Maybe.just(2)).test(); Runnable r1 = new Runnable() { @Override public void run() { pp.onComplete(); } }; Runnable r2 = new Runnable() { @Override public void run() { to.cancel(); } }; TestHelper.race(r1, r2); } } }
@Test public void emptyCancelRace() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { final PublishProcessor<Integer> pp = PublishProcessor.create(); final TestObserver<Integer> to = pp.singleElement().switchIfEmpty(Single.just(2)).test(); Runnable r1 = new Runnable() { @Override public void run() { pp.onComplete(); } }; Runnable r2 = new Runnable() { @Override public void run() { to.cancel(); } }; TestHelper.race(r1, r2); } }
@Test public void dispose() { PublishProcessor<Integer> pp = PublishProcessor.create(); TestObserver<Integer> to = pp.singleElement().switchIfEmpty(Maybe.just(2)).test(); assertTrue(pp.hasSubscribers()); to.cancel(); assertFalse(pp.hasSubscribers()); }
@Test public void dispose() { PublishProcessor<Integer> pp = PublishProcessor.create(); TestObserver<Integer> to = pp.singleElement().switchIfEmpty(Single.just(2)).test(); assertTrue(pp.hasSubscribers()); to.cancel(); assertFalse(pp.hasSubscribers()); }