Skip to content

Commit 6efc2cf

Browse files
artem-zinnatullinzsxwing
authored andcommitted
1.x: Add Single.toCompletable() (#3866)
Closes #3865.
1 parent a429815 commit 6efc2cf

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/main/java/rx/Single.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,31 @@ public final Observable<T> toObservable() {
20952095
return asObservable(this);
20962096
}
20972097

2098+
/**
2099+
* Returns a {@link Completable} that discards result of the {@link Single} (similar to
2100+
* {@link Observable#ignoreElements()}) and calls {@code onCompleted} when this source {@link Single} calls
2101+
* {@code onSuccess}. Error terminal event is propagated.
2102+
* <p>
2103+
* <img width="640" height="295" src=
2104+
* "https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Completable.toCompletable.png"
2105+
* alt="">
2106+
* <dl>
2107+
* <dt><b>Scheduler:</b></dt>
2108+
* <dd>{@code toCompletable} does not operate by default on a particular {@link Scheduler}.</dd>
2109+
* </dl>
2110+
*
2111+
* @return a {@link Completable} that calls {@code onCompleted} on it's subscriber when the source {@link Single}
2112+
* calls {@code onSuccess}.
2113+
* @see <a href="http://reactivex.io/documentation/completable.html">ReactiveX documentation:
2114+
* Completable</a>.
2115+
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical
2116+
* with the release number).
2117+
*/
2118+
@Experimental
2119+
public final Completable toCompletable() {
2120+
return Completable.fromSingle(this);
2121+
}
2122+
20982123
/**
20992124
* Returns a Single that mirrors the source Single but applies a timeout policy for its emitted item. If it
21002125
* is not emitted within the specified timeout duration, the resulting Single terminates and notifies

src/test/java/rx/SingleTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,29 @@ public void testToObservable() {
812812
ts.assertCompleted();
813813
}
814814

815+
@Test
816+
public void toCompletableSuccess() {
817+
Completable completable = Single.just("value").toCompletable();
818+
TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>();
819+
completable.subscribe(testSubscriber);
820+
821+
testSubscriber.assertCompleted();
822+
testSubscriber.assertNoValues();
823+
testSubscriber.assertNoErrors();
824+
}
825+
826+
@Test
827+
public void toCompletableError() {
828+
TestException exception = new TestException();
829+
Completable completable = Single.error(exception).toCompletable();
830+
TestSubscriber<Object> testSubscriber = new TestSubscriber<Object>();
831+
completable.subscribe(testSubscriber);
832+
833+
testSubscriber.assertError(exception);
834+
testSubscriber.assertNoValues();
835+
testSubscriber.assertNotCompleted();
836+
}
837+
815838
@Test
816839
public void doOnErrorShouldNotCallActionIfNoErrorHasOccurred() {
817840
Action1<Throwable> action = mock(Action1.class);

0 commit comments

Comments
 (0)