@@ -1597,6 +1597,30 @@ public final void unsafeSubscribe(Subscriber<? super T> subscriber) {
15971597 }
15981598 }
15991599
1600+ /**
1601+ * Subscribes an Observer to this single and returns a Subscription that allows
1602+ * unsubscription.
1603+ *
1604+ * @param observer the Observer to subscribe
1605+ * @return the Subscription that allows unsubscription
1606+ */
1607+ public final Subscription subscribe (final Observer <? super T > observer ) {
1608+ if (observer == null ) {
1609+ throw new NullPointerException ("observer is null" );
1610+ }
1611+ return subscribe (new SingleSubscriber <T >() {
1612+ @ Override
1613+ public void onSuccess (T value ) {
1614+ observer .onNext (value );
1615+ observer .onCompleted ();
1616+ }
1617+ @ Override
1618+ public void onError (Throwable error ) {
1619+ observer .onError (error );
1620+ }
1621+ });
1622+ }
1623+
16001624 /**
16011625 * Subscribes to a Single and provides a Subscriber that implements functions to handle the item the Single
16021626 * emits or any error notification it issues.
@@ -2541,4 +2565,75 @@ public final Single<T> retryWhen(final Func1<Observable<? extends Throwable>, ?
25412565 return toObservable ().retryWhen (notificationHandler ).toSingle ();
25422566 }
25432567
2568+ /**
2569+ * Constructs an Single that creates a dependent resource object which is disposed of on unsubscription.
2570+ * <p>
2571+ * <img width="640" height="400" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/using.png" alt="">
2572+ * <dl>
2573+ * <dt><b>Scheduler:</b></dt>
2574+ * <dd>{@code using} does not operate by default on a particular {@link Scheduler}.</dd>
2575+ * </dl>
2576+ *
2577+ * @param resourceFactory
2578+ * the factory function to create a resource object that depends on the Single
2579+ * @param singleFactory
2580+ * the factory function to create a Single
2581+ * @param disposeAction
2582+ * the function that will dispose of the resource
2583+ * @return the Single whose lifetime controls the lifetime of the dependent resource object
2584+ * @see <a href="http://reactivex.io/documentation/operators/using.html">ReactiveX operators documentation: Using</a>
2585+ */
2586+ @ Experimental
2587+ public static <T , Resource > Single <T > using (
2588+ final Func0 <Resource > resourceFactory ,
2589+ final Func1 <? super Resource , ? extends Single <? extends T >> observableFactory ,
2590+ final Action1 <? super Resource > disposeAction ) {
2591+ return using (resourceFactory , observableFactory , disposeAction , false );
2592+ }
2593+
2594+ /**
2595+ * Constructs an Single that creates a dependent resource object which is disposed of just before
2596+ * termination if you have set {@code disposeEagerly} to {@code true} and unsubscription does not occur
2597+ * before termination. Otherwise resource disposal will occur on unsubscription. Eager disposal is
2598+ * particularly appropriate for a synchronous Single that resuses resources. {@code disposeAction} will
2599+ * only be called once per subscription.
2600+ * <p>
2601+ * <img width="640" height="400" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/using.png" alt="">
2602+ * <dl>
2603+ * <dt><b>Scheduler:</b></dt>
2604+ * <dd>{@code using} does not operate by default on a particular {@link Scheduler}.</dd>
2605+ * </dl>
2606+ *
2607+ * @warn "Backpressure Support" section missing from javadoc
2608+ * @param resourceFactory
2609+ * the factory function to create a resource object that depends on the Single
2610+ * @param singleFactory
2611+ * the factory function to create a Single
2612+ * @param disposeAction
2613+ * the function that will dispose of the resource
2614+ * @param disposeEagerly
2615+ * if {@code true} then disposal will happen either on unsubscription or just before emission of
2616+ * a terminal event ({@code onComplete} or {@code onError}).
2617+ * @return the Single whose lifetime controls the lifetime of the dependent resource object
2618+ * @see <a href="http://reactivex.io/documentation/operators/using.html">ReactiveX operators documentation: Using</a>
2619+ * @Experimental The behavior of this can change at any time.
2620+ * @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number)
2621+ */
2622+ @ Experimental
2623+ public static <T , Resource > Single <T > using (
2624+ final Func0 <Resource > resourceFactory ,
2625+ final Func1 <? super Resource , ? extends Single <? extends T >> singleFactory ,
2626+ final Action1 <? super Resource > disposeAction , boolean disposeEagerly ) {
2627+ if (resourceFactory == null ) {
2628+ throw new NullPointerException ("resourceFactory is null" );
2629+ }
2630+ if (singleFactory == null ) {
2631+ throw new NullPointerException ("singleFactory is null" );
2632+ }
2633+ if (disposeAction == null ) {
2634+ throw new NullPointerException ("disposeAction is null" );
2635+ }
2636+ return create (new SingleOnSubscribeUsing <T , Resource >(resourceFactory , singleFactory , disposeAction , disposeEagerly ));
2637+ }
2638+
25442639}
0 commit comments