Observable { @NonNull public static <T> Observable<T> defer(@NonNull final Supplier<Observable<T>> func) { return Observable.create(new Function<Observer<T>, Subscription>() { @NonNull @Override public Subscription apply(@NonNull Observer<T> observer) { return func.apply().subscribe(observer); } }); } 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);  }
@Test public void testDefer() throws Exception { final ArrayList<Integer> ints = new ArrayList<>(Arrays.asList(1, 2, 3)); Observable<Integer> deferred = Observable.defer(new Supplier<Observable<Integer>>() { @NonNull @Override public Observable<Integer> apply() { return Observable.from(ints); } }); ints.add(4); validateObservable(deferred, Arrays.asList(1, 2, 3, 4), 4, 1, 0); }