From 68ff636a5ba3a13c67549e2c70ed3a6e913a44e7 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Wed, 24 Aug 2016 20:59:38 -0400 Subject: [PATCH] Add to() conversion function to all stream types. This deprecates extend() on Observable, which is a less powerful version of these functions. --- src/main/java/rx/Completable.java | 13 +++++++------ src/main/java/rx/Observable.java | 15 ++++++++++++++- src/main/java/rx/Single.java | 13 +++++++++++++ src/test/java/rx/CompletableTest.java | 17 +++++++++++++++++ src/test/java/rx/ObservableTests.java | 17 +++++++++++++++++ src/test/java/rx/SingleTest.java | 17 +++++++++++++++++ 6 files changed, 85 insertions(+), 7 deletions(-) diff --git a/src/main/java/rx/Completable.java b/src/main/java/rx/Completable.java index 6848ea05bb..ab466463ed 100644 --- a/src/main/java/rx/Completable.java +++ b/src/main/java/rx/Completable.java @@ -2222,13 +2222,14 @@ public final Completable timeout0(long timeout, TimeUnit unit, Scheduler schedul } /** - * Allows fluent conversion to another type via a function callback. - * @param the output type as determined by the converter function - * @param converter the function called with this which should return some other value. - * @return the converted value - * @throws NullPointerException if converter is null + * Calls the specified converter function during assembly time and returns its resulting value. + *

+ * This allows fluent conversion to any other type. + * @param the resulting object type + * @param converter the function that receives the current Single instance and returns a value + * @return the value returned by the function */ - public final U to(Func1 converter) { + public final R to(Func1 converter) { return converter.call(this); } diff --git a/src/main/java/rx/Observable.java b/src/main/java/rx/Observable.java index f17ab86dc2..72ecc41d99 100644 --- a/src/main/java/rx/Observable.java +++ b/src/main/java/rx/Observable.java @@ -212,7 +212,7 @@ public interface Operator extends Func1, Subscriber< * @return an instance of R created by the provided conversion function * @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number) */ - @Experimental + @Experimental @Deprecated // TODO remove method some time after 1.2.0 release. It was never a stable API. public R extend(Func1, ? extends R> conversion) { return conversion.call(new OnSubscribeExtend(this)); } @@ -309,6 +309,19 @@ public interface Transformer extends Func1, Observable> { // cover for generics insanity } + /** + * Calls the specified converter function during assembly time and returns its resulting value. + *

+ * This allows fluent conversion to any other type. + * @param the resulting object type + * @param converter the function that receives the current Observable instance and returns a value + * @return the value returned by the function + */ + @Experimental + public final R to(Func1, R> converter) { + return converter.call(this); + } + /** * Returns a Single that emits the single item emitted by the source Observable, if that Observable * emits only a single item. If the source Observable emits more than one item or no items, notify of an diff --git a/src/main/java/rx/Single.java b/src/main/java/rx/Single.java index 198dba876c..bec23882ba 100644 --- a/src/main/java/rx/Single.java +++ b/src/main/java/rx/Single.java @@ -2197,6 +2197,19 @@ public void onError(Throwable e) { } }); } + + /** + * Calls the specified converter function during assembly time and returns its resulting value. + *

+ * This allows fluent conversion to any other type. + * @param the resulting object type + * @param converter the function that receives the current Single instance and returns a value + * @return the value returned by the function + */ + @Experimental + public final R to(Func1, R> converter) { + return converter.call(this); + } /** * Converts this Single into an {@link Observable}. diff --git a/src/test/java/rx/CompletableTest.java b/src/test/java/rx/CompletableTest.java index 6545cafeef..6ae30f4293 100644 --- a/src/test/java/rx/CompletableTest.java +++ b/src/test/java/rx/CompletableTest.java @@ -4158,4 +4158,21 @@ public Boolean call(Throwable t) { Assert.assertTrue(errors.get(1).toString(), errors.get(1) instanceof TestException); Assert.assertEquals(errors.get(1).toString(), "Forced inner failure", errors.get(1).getMessage()); } + + @Test public void toFunctionReceivesObservableReturnsResult() { + Completable c = Completable.error(new RuntimeException()); + + final Object expectedResult = new Object(); + final AtomicReference completableRef = new AtomicReference(); + Object actualResult = c.to(new Func1() { + @Override + public Object call(Completable completable) { + completableRef.set(completable); + return expectedResult; + } + }); + + assertSame(expectedResult, actualResult); + assertSame(c, completableRef.get()); + } } \ No newline at end of file diff --git a/src/test/java/rx/ObservableTests.java b/src/test/java/rx/ObservableTests.java index 121eb45ea9..16443d4476 100644 --- a/src/test/java/rx/ObservableTests.java +++ b/src/test/java/rx/ObservableTests.java @@ -1423,4 +1423,21 @@ public void call() { assertTrue(list.get(1).toString(), list.get(1) instanceof TestException); assertEquals(100, list.get(2)); } + + @Test public void toFunctionReceivesObservableReturnsResult() { + Observable o = Observable.just("Hi"); + + final Object expectedResult = new Object(); + final AtomicReference> observableRef = new AtomicReference>(); + Object actualResult = o.to(new Func1, Object>() { + @Override + public Object call(Observable observable) { + observableRef.set(observable); + return expectedResult; + } + }); + + assertSame(expectedResult, actualResult); + assertSame(o, observableRef.get()); + } } diff --git a/src/test/java/rx/SingleTest.java b/src/test/java/rx/SingleTest.java index acc95c6ef6..fa68df8a16 100644 --- a/src/test/java/rx/SingleTest.java +++ b/src/test/java/rx/SingleTest.java @@ -2069,4 +2069,21 @@ public Completable call(final Integer integer) { testSubscriber.assertError(UnsupportedOperationException.class); } + + @Test public void toFunctionReceivesObservableReturnsResult() { + Single s = Single.just("Hi"); + + final Object expectedResult = new Object(); + final AtomicReference> singleRef = new AtomicReference>(); + Object actualResult = s.to(new Func1, Object>() { + @Override + public Object call(Single single) { + singleRef.set(single); + return expectedResult; + } + }); + + assertSame(expectedResult, actualResult); + assertSame(s, singleRef.get()); + } }