@@ -763,6 +763,8 @@ public static <T> Maybe<T> fromSingle(SingleSource<T> singleSource) {
763
763
* @param <T>
764
764
* the type of the item emitted by the {@link Maybe}.
765
765
* @return a new Maybe instance
766
+ * @see #defer(Supplier)
767
+ * @see #fromSupplier(Supplier)
766
768
*/
767
769
@ CheckReturnValue
768
770
@ NonNull
@@ -865,6 +867,51 @@ public static <T> Maybe<T> fromRunnable(final Runnable run) {
865
867
return RxJavaPlugins .onAssembly (new MaybeFromRunnable <T >(run ));
866
868
}
867
869
870
+ /**
871
+ * Returns a {@link Maybe} that invokes the given {@link Supplier} for each individual {@link MaybeObserver} that
872
+ * subscribes and emits the resulting non-null item via {@code onSuccess} while
873
+ * considering a {@code null} result from the {@code Supplier} as indication for valueless completion
874
+ * via {@code onComplete}.
875
+ * <p>
876
+ * This operator allows you to defer the execution of the given {@code Supplier} until a {@code MaybeObserver}
877
+ * subscribes to the returned {@link Maybe}. In other terms, this source operator evaluates the given
878
+ * {@code Supplier} "lazily".
879
+ * <p>
880
+ * Note that the {@code null} handling of this operator differs from the similar source operators in the other
881
+ * {@link io.reactivex base reactive classes}. Those operators signal a {@code NullPointerException} if the value returned by their
882
+ * {@code Supplier} is {@code null} while this {@code fromSupplier} considers it to indicate the
883
+ * returned {@code Maybe} is empty.
884
+ * <dl>
885
+ * <dt><b>Scheduler:</b></dt>
886
+ * <dd>{@code fromSupplier} does not operate by default on a particular {@link Scheduler}.</dd>
887
+ * <dt><b>Error handling:</b></dt>
888
+ * <dd>Any non-fatal exception thrown by {@link Supplier#get()} will be forwarded to {@code onError},
889
+ * except if the {@code MaybeObserver} disposed the subscription in the meantime. In this latter case,
890
+ * the exception is forwarded to the global error handler via
891
+ * {@link io.reactivex.plugins.RxJavaPlugins#onError(Throwable)} wrapped into a
892
+ * {@link io.reactivex.exceptions.UndeliverableException UndeliverableException}.
893
+ * Fatal exceptions are rethrown and usually will end up in the executing thread's
894
+ * {@link java.lang.Thread.UncaughtExceptionHandler#uncaughtException(Thread, Throwable)} handler.</dd>
895
+ * </dl>
896
+ *
897
+ * @param supplier
898
+ * a {@link Supplier} instance whose execution should be deferred and performed for each individual
899
+ * {@code MaybeObserver} that subscribes to the returned {@link Maybe}.
900
+ * @param <T>
901
+ * the type of the item emitted by the {@link Maybe}.
902
+ * @return a new Maybe instance
903
+ * @see #defer(Supplier)
904
+ * @see #fromCallable(Callable)
905
+ * @since 3.0.0
906
+ */
907
+ @ CheckReturnValue
908
+ @ NonNull
909
+ @ SchedulerSupport (SchedulerSupport .NONE )
910
+ public static <T > Maybe <T > fromSupplier (@ NonNull final Supplier <? extends T > supplier ) {
911
+ ObjectHelper .requireNonNull (supplier , "supplier is null" );
912
+ return RxJavaPlugins .onAssembly (new MaybeFromSupplier <T >(supplier ));
913
+ }
914
+
868
915
/**
869
916
* Returns a {@code Maybe} that emits a specified item.
870
917
* <p>
0 commit comments