Skip to content

Commit 743f164

Browse files
JakeWhartonakarnokd
authored andcommitted
Add to() conversion function to all stream types. (#4423)
This deprecates extend() on Observable, which is a less powerful version of these functions.
1 parent a5c9453 commit 743f164

File tree

6 files changed

+85
-7
lines changed

6 files changed

+85
-7
lines changed

src/main/java/rx/Completable.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,13 +2222,14 @@ public final Completable timeout0(long timeout, TimeUnit unit, Scheduler schedul
22222222
}
22232223

22242224
/**
2225-
* Allows fluent conversion to another type via a function callback.
2226-
* @param <U> the output type as determined by the converter function
2227-
* @param converter the function called with this which should return some other value.
2228-
* @return the converted value
2229-
* @throws NullPointerException if converter is null
2225+
* Calls the specified converter function during assembly time and returns its resulting value.
2226+
* <p>
2227+
* This allows fluent conversion to any other type.
2228+
* @param <R> the resulting object type
2229+
* @param converter the function that receives the current Single instance and returns a value
2230+
* @return the value returned by the function
22302231
*/
2231-
public final <U> U to(Func1<? super Completable, U> converter) {
2232+
public final <R> R to(Func1<? super Completable, R> converter) {
22322233
return converter.call(this);
22332234
}
22342235

src/main/java/rx/Observable.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public interface Operator<R, T> extends Func1<Subscriber<? super R>, Subscriber<
212212
* @return an instance of R created by the provided conversion function
213213
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number)
214214
*/
215-
@Experimental
215+
@Experimental @Deprecated // TODO remove method some time after 1.2.0 release. It was never a stable API.
216216
public <R> R extend(Func1<? super OnSubscribe<T>, ? extends R> conversion) {
217217
return conversion.call(new OnSubscribeExtend<T>(this));
218218
}
@@ -309,6 +309,19 @@ public interface Transformer<T, R> extends Func1<Observable<T>, Observable<R>> {
309309
// cover for generics insanity
310310
}
311311

312+
/**
313+
* Calls the specified converter function during assembly time and returns its resulting value.
314+
* <p>
315+
* This allows fluent conversion to any other type.
316+
* @param <R> the resulting object type
317+
* @param converter the function that receives the current Observable instance and returns a value
318+
* @return the value returned by the function
319+
*/
320+
@Experimental
321+
public final <R> R to(Func1<? super Observable<T>, R> converter) {
322+
return converter.call(this);
323+
}
324+
312325
/**
313326
* Returns a Single that emits the single item emitted by the source Observable, if that Observable
314327
* emits only a single item. If the source Observable emits more than one item or no items, notify of an

src/main/java/rx/Single.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,19 @@ public void onError(Throwable e) {
21972197
}
21982198
});
21992199
}
2200+
2201+
/**
2202+
* Calls the specified converter function during assembly time and returns its resulting value.
2203+
* <p>
2204+
* This allows fluent conversion to any other type.
2205+
* @param <R> the resulting object type
2206+
* @param converter the function that receives the current Single instance and returns a value
2207+
* @return the value returned by the function
2208+
*/
2209+
@Experimental
2210+
public final <R> R to(Func1<? super Single<T>, R> converter) {
2211+
return converter.call(this);
2212+
}
22002213

22012214
/**
22022215
* Converts this Single into an {@link Observable}.

src/test/java/rx/CompletableTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4158,4 +4158,21 @@ public Boolean call(Throwable t) {
41584158
Assert.assertTrue(errors.get(1).toString(), errors.get(1) instanceof TestException);
41594159
Assert.assertEquals(errors.get(1).toString(), "Forced inner failure", errors.get(1).getMessage());
41604160
}
4161+
4162+
@Test public void toFunctionReceivesObservableReturnsResult() {
4163+
Completable c = Completable.error(new RuntimeException());
4164+
4165+
final Object expectedResult = new Object();
4166+
final AtomicReference<Completable> completableRef = new AtomicReference<Completable>();
4167+
Object actualResult = c.to(new Func1<Completable, Object>() {
4168+
@Override
4169+
public Object call(Completable completable) {
4170+
completableRef.set(completable);
4171+
return expectedResult;
4172+
}
4173+
});
4174+
4175+
assertSame(expectedResult, actualResult);
4176+
assertSame(c, completableRef.get());
4177+
}
41614178
}

src/test/java/rx/ObservableTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,4 +1423,21 @@ public void call() {
14231423
assertTrue(list.get(1).toString(), list.get(1) instanceof TestException);
14241424
assertEquals(100, list.get(2));
14251425
}
1426+
1427+
@Test public void toFunctionReceivesObservableReturnsResult() {
1428+
Observable<String> o = Observable.just("Hi");
1429+
1430+
final Object expectedResult = new Object();
1431+
final AtomicReference<Observable<?>> observableRef = new AtomicReference<Observable<?>>();
1432+
Object actualResult = o.to(new Func1<Observable<String>, Object>() {
1433+
@Override
1434+
public Object call(Observable<String> observable) {
1435+
observableRef.set(observable);
1436+
return expectedResult;
1437+
}
1438+
});
1439+
1440+
assertSame(expectedResult, actualResult);
1441+
assertSame(o, observableRef.get());
1442+
}
14261443
}

src/test/java/rx/SingleTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,4 +2069,21 @@ public Completable call(final Integer integer) {
20692069

20702070
testSubscriber.assertError(UnsupportedOperationException.class);
20712071
}
2072+
2073+
@Test public void toFunctionReceivesObservableReturnsResult() {
2074+
Single<String> s = Single.just("Hi");
2075+
2076+
final Object expectedResult = new Object();
2077+
final AtomicReference<Single<?>> singleRef = new AtomicReference<Single<?>>();
2078+
Object actualResult = s.to(new Func1<Single<String>, Object>() {
2079+
@Override
2080+
public Object call(Single<String> single) {
2081+
singleRef.set(single);
2082+
return expectedResult;
2083+
}
2084+
});
2085+
2086+
assertSame(expectedResult, actualResult);
2087+
assertSame(s, singleRef.get());
2088+
}
20722089
}

0 commit comments

Comments
 (0)