@Test public void testDefaultIfEmpty() throws Exception { Observable<Integer> one = Observable.just(1); Observable<Integer> empty = Observable.empty(); Observable<Integer> defaultOne = one.defaultIfEmpty(2); Observable<Integer> defaultEmpty = empty.defaultIfEmpty(2); validateObservable(defaultOne, Arrays.asList(1), 1, 1, 0); initializeValues(); validateObservable(defaultEmpty, Arrays.asList(2), 1, 1, 0); }
@NonNull public Observable<T> defaultIfEmpty(@NonNull final T defaultValue) { return create(new Function<Observer<T>, Subscription>() { @NonNull @Override public Subscription apply(@NonNull final Observer<T> observer) { final AtomicBoolean empty = new AtomicBoolean(true); return subscribe(new Observer<T>() { @Override public void onNext(@NonNull T value) { observer.onNext(value); empty.set(false); } @Override public void onCompleted() { if (empty.get()) { observer.onNext(defaultValue); } observer.onCompleted(); } @Override public void onError(@NonNull Exception e) { observer.onCompleted(); } }); } }); }
Observable { @NonNull public Observable<T> defaultIfEmpty(@NonNull final T defaultValue) { return create(new Function<Observer<T>, Subscription>() { @NonNull @Override public Subscription apply(@NonNull final Observer<T> observer) { final AtomicBoolean empty = new AtomicBoolean(true); return subscribe(new Observer<T>() { @Override public void onNext(@NonNull T value) { observer.onNext(value); empty.set(false); } @Override public void onCompleted() { if (empty.get()) { observer.onNext(defaultValue); } observer.onCompleted(); } @Override public void onError(@NonNull Exception e) { observer.onCompleted(); } }); } }); } }
Observable { @NonNull public Observable<T> defaultIfEmpty(@NonNull final T defaultValue) { return create(new Function<Observer<T>, Subscription>() { @NonNull @Override public Subscription apply(@NonNull final Observer<T> observer) { final AtomicBoolean empty = new AtomicBoolean(true); return subscribe(new Observer<T>() { @Override public void onNext(@NonNull T value) { observer.onNext(value); empty.set(false); } @Override public void onCompleted() { if (empty.get()) { observer.onNext(defaultValue); } observer.onCompleted(); } @Override public void onError(@NonNull Exception e) { observer.onCompleted(); } }); } }); } protected  Observable(); protected  Observable(@Nullable Function<Observer<T>, Subscription> onSubscribe); }
Observable { @NonNull public Observable<T> defaultIfEmpty(@NonNull final T defaultValue) { return create(new Function<Observer<T>, Subscription>() { @NonNull @Override public Subscription apply(@NonNull final Observer<T> observer) { final AtomicBoolean empty = new AtomicBoolean(true); return subscribe(new Observer<T>() { @Override public void onNext(@NonNull T value) { observer.onNext(value); empty.set(false); } @Override public void onCompleted() { if (empty.get()) { observer.onNext(defaultValue); } observer.onCompleted(); } @Override public void onError(@NonNull Exception e) { observer.onCompleted(); } }); } }); } protected  Observable(); protected  Observable(@Nullable Function<Observer<T>, Subscription> onSubscribe); @NonNull static Observable<T> create(@NonNull Function<Observer<T>, Subscription> func); @NonNull static Observable<T> just(@NonNull final T value); @NonNull static Observable<T> empty(); @NonNull static Observable<T> never(); @NonNull static Observable<T> error(@NonNull final Exception e); @NonNull static Observable<T> from(@NonNull final Collection<T> collection); @NonNull Subscription subscribe(@NonNull Observer<T> observer); @NonNull Observable<R> flatMap(@NonNull final Function<T, Observable<R>> func); @NonNull Observable<R> map(@NonNull final Function<T, R> func); @NonNull Observable<T> filter(@NonNull final Predicate<T> pred); @NonNull Observable<T> distinctUntilChanged(); @NonNull Observable<T> defaultIfEmpty(@NonNull final T defaultValue); @NonNull Observable<T> observeOn(@NonNull final Scheduler scheduler); @NonNull Observable<T> subscribeOn(@NonNull final Scheduler scheduler); @NonNull static Observable<T> merge(@NonNull final Observable<T> lh, @NonNull final Observable<T> rh); @NonNull static Observable<T> merge(@NonNull Collection<Observable<T>> observables); @NonNull static Observable<T> concat(@NonNull final Observable<T> lh, @NonNull final Observable<T> rh); @NonNull static Observable<T> defer(@NonNull final Supplier<Observable<T>> func); @NonNull static Observable<R> zip(@NonNull final Observable<T> lh, @NonNull final Observable<T> rh, @NonNull final BiFunction<T, T, R> func); }
Observable { @NonNull public Observable<T> defaultIfEmpty(@NonNull final T defaultValue) { return create(new Function<Observer<T>, Subscription>() { @NonNull @Override public Subscription apply(@NonNull final Observer<T> observer) { final AtomicBoolean empty = new AtomicBoolean(true); return subscribe(new Observer<T>() { @Override public void onNext(@NonNull T value) { observer.onNext(value); empty.set(false); } @Override public void onCompleted() { if (empty.get()) { observer.onNext(defaultValue); } observer.onCompleted(); } @Override public void onError(@NonNull Exception e) { observer.onCompleted(); } }); } }); } protected  Observable(); protected  Observable(@Nullable Function<Observer<T>, Subscription> onSubscribe); @NonNull static Observable<T> create(@NonNull Function<Observer<T>, Subscription> func); @NonNull static Observable<T> just(@NonNull final T value); @NonNull static Observable<T> empty(); @NonNull static Observable<T> never(); @NonNull static Observable<T> error(@NonNull final Exception e); @NonNull static Observable<T> from(@NonNull final Collection<T> collection); @NonNull Subscription subscribe(@NonNull Observer<T> observer); @NonNull Observable<R> flatMap(@NonNull final Function<T, Observable<R>> func); @NonNull Observable<R> map(@NonNull final Function<T, R> func); @NonNull Observable<T> filter(@NonNull final Predicate<T> pred); @NonNull Observable<T> distinctUntilChanged(); @NonNull Observable<T> defaultIfEmpty(@NonNull final T defaultValue); @NonNull Observable<T> observeOn(@NonNull final Scheduler scheduler); @NonNull Observable<T> subscribeOn(@NonNull final Scheduler scheduler); @NonNull static Observable<T> merge(@NonNull final Observable<T> lh, @NonNull final Observable<T> rh); @NonNull static Observable<T> merge(@NonNull Collection<Observable<T>> observables); @NonNull static Observable<T> concat(@NonNull final Observable<T> lh, @NonNull final Observable<T> rh); @NonNull static Observable<T> defer(@NonNull final Supplier<Observable<T>> func); @NonNull static Observable<R> zip(@NonNull final Observable<T> lh, @NonNull final Observable<T> rh, @NonNull final BiFunction<T, T, R> func);  }