diff --git a/src/main/java/rx/Single.java b/src/main/java/rx/Single.java index b1e2ed4074..e954612b4f 100644 --- a/src/main/java/rx/Single.java +++ b/src/main/java/rx/Single.java @@ -2095,6 +2095,31 @@ public final Observable toObservable() { return asObservable(this); } + /** + * Returns a {@link Completable} that discards result of the {@link Single} (similar to + * {@link Observable#ignoreElements()}) and calls {@code onCompleted} when this source {@link Single} calls + * {@code onSuccess}. Error terminal event is propagated. + *

+ * + *

+ *
Scheduler:
+ *
{@code toCompletable} does not operate by default on a particular {@link Scheduler}.
+ *
+ * + * @return a {@link Completable} that calls {@code onCompleted} on it's subscriber when the source {@link Single} + * calls {@code onSuccess}. + * @see ReactiveX documentation: + * Completable. + * @since (if this graduates from Experimental/Beta to supported, replace this parenthetical + * with the release number). + */ + @Experimental + public final Completable toCompletable() { + return Completable.fromSingle(this); + } + /** * Returns a Single that mirrors the source Single but applies a timeout policy for its emitted item. If it * is not emitted within the specified timeout duration, the resulting Single terminates and notifies diff --git a/src/test/java/rx/SingleTest.java b/src/test/java/rx/SingleTest.java index 2952e22bfd..3760330eb4 100644 --- a/src/test/java/rx/SingleTest.java +++ b/src/test/java/rx/SingleTest.java @@ -812,6 +812,29 @@ public void testToObservable() { ts.assertCompleted(); } + @Test + public void toCompletableSuccess() { + Completable completable = Single.just("value").toCompletable(); + TestSubscriber testSubscriber = new TestSubscriber(); + completable.subscribe(testSubscriber); + + testSubscriber.assertCompleted(); + testSubscriber.assertNoValues(); + testSubscriber.assertNoErrors(); + } + + @Test + public void toCompletableError() { + TestException exception = new TestException(); + Completable completable = Single.error(exception).toCompletable(); + TestSubscriber testSubscriber = new TestSubscriber(); + completable.subscribe(testSubscriber); + + testSubscriber.assertError(exception); + testSubscriber.assertNoValues(); + testSubscriber.assertNotCompleted(); + } + @Test public void doOnErrorShouldNotCallActionIfNoErrorHasOccurred() { Action1 action = mock(Action1.class);