Skip to content

Commit a672013

Browse files
authored
3.x: Merge as() into to() (#6514)
* 3.x: Merge as() into to() * Fix as() mentioned in the javadocs
1 parent cb03724 commit a672013

26 files changed

+157
-310
lines changed

docs/What's-different-in-3.0.md

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,56 @@ TBD.
55

66
### API signature changes
77

8+
#### as() and to() operators
9+
10+
In 2.x, the `to()` operator used the generic `Function` to allow assembly-time conversion of flows into arbitrary types. The drawback of this
11+
approach was that each base reactive type had the same `Function` interface in their method signature,
12+
thus it was impossible to implement multiple converters for different reactive types within the same class.
13+
To work around this issue, the `as` operator and `XConverter` interfaces have been introduced
14+
in 2.x, which interfaces are distinct and can be implemented on the same class. Changing the signature of `to` in 2.x was not possible due
15+
to the pledged binary compatibility of the library.
16+
17+
From 3.x, the `as()` methods have been removed and the `to()` methods now each work with their respective `XConverer` interfaces:
18+
19+
- `Flowable.to(Function<Flowable<T>, R>)` is now `Flowable.to(FlowableConverter<T, R>)`
20+
- `Observable.to(Function<Observable<T>, R>)` is now `Observable.to(ObservableConverter<T, R>)`
21+
- `Maybe.to(Function<Flowable<T>, R>)` is now `Maybe.to(MaybeConverter<T, R>)`
22+
- `Single.to(Function<Flowable<T>, R>)` is now `Maybe.to(SingleConverter<T, R>)`
23+
- `Completable.to(Function<Completable, R>)` is now `Completable.to(CompletableConverter<R>)`
24+
- `ParallelFlowable.to(Function<ParallelFlowable<T>, R)` is now `ParallelFlowable.to(ParallelFlowableConverter<T, R>)`
25+
26+
If one was using these methods with a lambda expression, only a recompilation is needed:
27+
28+
```java
29+
// before
30+
source.to(flowable -> flowable.blockingFirst());
31+
32+
// after
33+
source.to(flowable -> flowable.blockingFirst());
34+
```
35+
36+
If one was implementing a Function interface (typically anonymously), the interface type, type arguments and the `throws` clause have to be adjusted
37+
38+
```java
39+
// before
40+
source.to(new Function<Flowable<Integer>, Integer>() {
41+
@Override
42+
public Integer apply(Flowable<Integer> t) throws Exception {
43+
return t.blockingFirst();
44+
}
45+
});
46+
47+
// after
48+
source.to(new FlowableConverter<Integer, Integer>() {
49+
@Override
50+
public Integer apply(Flowable<Integer> t) {
51+
return t.blockingFirst();
52+
}
53+
});
54+
```
55+
856
TBD.
957

10-
- as() merged into to()
1158
- some operators returning a more appropriate Single or Maybe
1259
- functional interfaces throws widening to Throwable
1360
- standard methods removed

src/main/java/io/reactivex/Completable.java

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import io.reactivex.internal.operators.maybe.*;
2828
import io.reactivex.internal.operators.mixed.*;
2929
import io.reactivex.internal.operators.single.*;
30-
import io.reactivex.internal.util.ExceptionHelper;
3130
import io.reactivex.observers.TestObserver;
3231
import io.reactivex.plugins.RxJavaPlugins;
3332
import io.reactivex.schedulers.Schedulers;
@@ -1182,29 +1181,6 @@ public final Completable andThen(CompletableSource next) {
11821181
return RxJavaPlugins.onAssembly(new CompletableAndThenCompletable(this, next));
11831182
}
11841183

1185-
/**
1186-
* Calls the specified converter function during assembly time and returns its resulting value.
1187-
* <p>
1188-
* <img width="640" height="751" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Completable.as.png" alt="">
1189-
* <p>
1190-
* This allows fluent conversion to any other type.
1191-
* <dl>
1192-
* <dt><b>Scheduler:</b></dt>
1193-
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
1194-
* </dl>
1195-
* <p>History: 2.1.7 - experimental
1196-
* @param <R> the resulting object type
1197-
* @param converter the function that receives the current Completable instance and returns a value
1198-
* @return the converted value
1199-
* @throws NullPointerException if converter is null
1200-
* @since 2.2
1201-
*/
1202-
@CheckReturnValue
1203-
@SchedulerSupport(SchedulerSupport.NONE)
1204-
public final <R> R as(@NonNull CompletableConverter<? extends R> converter) {
1205-
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
1206-
}
1207-
12081184
/**
12091185
* Subscribes to and awaits the termination of this Completable instance in a blocking manner and
12101186
* rethrows any exception emitted.
@@ -2578,27 +2554,26 @@ private Completable timeout0(long timeout, TimeUnit unit, Scheduler scheduler, C
25782554
}
25792555

25802556
/**
2581-
* Allows fluent conversion to another type via a function callback.
2557+
* Calls the specified converter function during assembly time and returns its resulting value.
25822558
* <p>
25832559
* <img width="640" height="751" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Completable.to.png" alt="">
2560+
* <p>
2561+
* This allows fluent conversion to any other type.
25842562
* <dl>
25852563
* <dt><b>Scheduler:</b></dt>
25862564
* <dd>{@code to} does not operate by default on a particular {@link Scheduler}.</dd>
25872565
* </dl>
2588-
* @param <U> the output type
2589-
* @param converter the function called with this which should return some other value.
2566+
* <p>History: 2.1.7 - experimental
2567+
* @param <R> the resulting object type
2568+
* @param converter the function that receives the current Completable instance and returns a value
25902569
* @return the converted value
25912570
* @throws NullPointerException if converter is null
2571+
* @since 2.2
25922572
*/
25932573
@CheckReturnValue
25942574
@SchedulerSupport(SchedulerSupport.NONE)
2595-
public final <U> U to(Function<? super Completable, U> converter) {
2596-
try {
2597-
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
2598-
} catch (Throwable ex) {
2599-
Exceptions.throwIfFatal(ex);
2600-
throw ExceptionHelper.wrapOrThrow(ex);
2601-
}
2575+
public final <R> R to(@NonNull CompletableConverter<? extends R> converter) {
2576+
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
26022577
}
26032578

26042579
/**

src/main/java/io/reactivex/CompletableConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import io.reactivex.annotations.*;
1717

1818
/**
19-
* Convenience interface and callback used by the {@link Completable#as} operator to turn a Completable into another
19+
* Convenience interface and callback used by the {@link Completable#to} operator to turn a Completable into another
2020
* value fluently.
2121
* <p>History: 2.1.7 - experimental
2222
* @param <R> the output type

src/main/java/io/reactivex/Flowable.java

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5646,30 +5646,6 @@ public final Single<Boolean> any(Predicate<? super T> predicate) {
56465646
return RxJavaPlugins.onAssembly(new FlowableAnySingle<T>(this, predicate));
56475647
}
56485648

5649-
/**
5650-
* Calls the specified converter function during assembly time and returns its resulting value.
5651-
* <p>
5652-
* This allows fluent conversion to any other type.
5653-
* <dl>
5654-
* <dt><b>Backpressure:</b></dt>
5655-
* <dd>The backpressure behavior depends on what happens in the {@code converter} function.</dd>
5656-
* <dt><b>Scheduler:</b></dt>
5657-
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
5658-
* </dl>
5659-
* <p>History: 2.1.7 - experimental
5660-
* @param <R> the resulting object type
5661-
* @param converter the function that receives the current Flowable instance and returns a value
5662-
* @return the converted value
5663-
* @throws NullPointerException if converter is null
5664-
* @since 2.2
5665-
*/
5666-
@CheckReturnValue
5667-
@BackpressureSupport(BackpressureKind.SPECIAL)
5668-
@SchedulerSupport(SchedulerSupport.NONE)
5669-
public final <R> R as(@NonNull FlowableConverter<T, ? extends R> converter) {
5670-
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
5671-
}
5672-
56735649
/**
56745650
* Returns the first item emitted by this {@code Flowable}, or throws
56755651
* {@code NoSuchElementException} if it emits no items.
@@ -16878,20 +16854,18 @@ public final Flowable<Timed<T>> timestamp(final TimeUnit unit, final Scheduler s
1687816854
* <dt><b>Scheduler:</b></dt>
1687916855
* <dd>{@code to} does not operate by default on a particular {@link Scheduler}.</dd>
1688016856
* </dl>
16857+
* <p>History: 2.1.7 - experimental
1688116858
* @param <R> the resulting object type
1688216859
* @param converter the function that receives the current Flowable instance and returns a value
16883-
* @return the value returned by the function
16860+
* @return the converted value
16861+
* @throws NullPointerException if converter is null
16862+
* @since 2.2
1688416863
*/
1688516864
@CheckReturnValue
1688616865
@BackpressureSupport(BackpressureKind.SPECIAL)
1688716866
@SchedulerSupport(SchedulerSupport.NONE)
16888-
public final <R> R to(Function<? super Flowable<T>, R> converter) {
16889-
try {
16890-
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
16891-
} catch (Throwable ex) {
16892-
Exceptions.throwIfFatal(ex);
16893-
throw ExceptionHelper.wrapOrThrow(ex);
16894-
}
16867+
public final <R> R to(@NonNull FlowableConverter<T, ? extends R> converter) {
16868+
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
1689516869
}
1689616870

1689716871
/**

src/main/java/io/reactivex/FlowableConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import io.reactivex.annotations.*;
1717

1818
/**
19-
* Convenience interface and callback used by the {@link Flowable#as} operator to turn a Flowable into another
19+
* Convenience interface and callback used by the {@link Flowable#to} operator to turn a Flowable into another
2020
* value fluently.
2121
* <p>History: 2.1.7 - experimental
2222
* @param <T> the upstream type

src/main/java/io/reactivex/Maybe.java

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,27 +2279,6 @@ public final Maybe<T> ambWith(MaybeSource<? extends T> other) {
22792279
return ambArray(this, other);
22802280
}
22812281

2282-
/**
2283-
* Calls the specified converter function during assembly time and returns its resulting value.
2284-
* <p>
2285-
* This allows fluent conversion to any other type.
2286-
* <dl>
2287-
* <dt><b>Scheduler:</b></dt>
2288-
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
2289-
* </dl>
2290-
* <p>History: 2.1.7 - experimental
2291-
* @param <R> the resulting object type
2292-
* @param converter the function that receives the current Maybe instance and returns a value
2293-
* @return the converted value
2294-
* @throws NullPointerException if converter is null
2295-
* @since 2.2
2296-
*/
2297-
@CheckReturnValue
2298-
@SchedulerSupport(SchedulerSupport.NONE)
2299-
public final <R> R as(@NonNull MaybeConverter<T, ? extends R> converter) {
2300-
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
2301-
}
2302-
23032282
/**
23042283
* Waits in a blocking fashion until the current Maybe signals a success value (which is returned),
23052284
* null if completed or an exception (which is propagated).
@@ -3579,28 +3558,24 @@ public final <U> Maybe<U> ofType(final Class<U> clazz) {
35793558
}
35803559

35813560
/**
3582-
* Calls the specified converter function with the current Maybe instance
3583-
* during assembly time and returns its result.
3561+
* Calls the specified converter function during assembly time and returns its resulting value.
3562+
* <p>
3563+
* This allows fluent conversion to any other type.
35843564
* <dl>
35853565
* <dt><b>Scheduler:</b></dt>
35863566
* <dd>{@code to} does not operate by default on a particular {@link Scheduler}.</dd>
35873567
* </dl>
3588-
* @param <R> the result type
3589-
* @param convert the function that is called with the current Maybe instance during
3590-
* assembly time that should return some value to be the result
3591-
*
3592-
* @return the value returned by the convert function
3568+
* <p>History: 2.1.7 - experimental
3569+
* @param <R> the resulting object type
3570+
* @param converter the function that receives the current Maybe instance and returns a value
3571+
* @return the converted value
3572+
* @throws NullPointerException if converter is null
3573+
* @since 2.2
35933574
*/
35943575
@CheckReturnValue
3595-
@NonNull
35963576
@SchedulerSupport(SchedulerSupport.NONE)
3597-
public final <R> R to(Function<? super Maybe<T>, R> convert) {
3598-
try {
3599-
return ObjectHelper.requireNonNull(convert, "convert is null").apply(this);
3600-
} catch (Throwable ex) {
3601-
Exceptions.throwIfFatal(ex);
3602-
throw ExceptionHelper.wrapOrThrow(ex);
3603-
}
3577+
public final <R> R to(@NonNull MaybeConverter<T, ? extends R> converter) {
3578+
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
36043579
}
36053580

36063581
/**

src/main/java/io/reactivex/MaybeConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import io.reactivex.annotations.*;
1717

1818
/**
19-
* Convenience interface and callback used by the {@link Maybe#as} operator to turn a Maybe into another
19+
* Convenience interface and callback used by the {@link Maybe#to} operator to turn a Maybe into another
2020
* value fluently.
2121
* <p>History: 2.1.7 - experimental
2222
* @param <T> the upstream type

src/main/java/io/reactivex/Observable.java

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5076,27 +5076,6 @@ public final Single<Boolean> any(Predicate<? super T> predicate) {
50765076
return RxJavaPlugins.onAssembly(new ObservableAnySingle<T>(this, predicate));
50775077
}
50785078

5079-
/**
5080-
* Calls the specified converter function during assembly time and returns its resulting value.
5081-
* <p>
5082-
* This allows fluent conversion to any other type.
5083-
* <dl>
5084-
* <dt><b>Scheduler:</b></dt>
5085-
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
5086-
* </dl>
5087-
* <p>History: 2.1.7 - experimental
5088-
* @param <R> the resulting object type
5089-
* @param converter the function that receives the current Observable instance and returns a value
5090-
* @return the converted value
5091-
* @throws NullPointerException if converter is null
5092-
* @since 2.2
5093-
*/
5094-
@CheckReturnValue
5095-
@SchedulerSupport(SchedulerSupport.NONE)
5096-
public final <R> R as(@NonNull ObservableConverter<T, ? extends R> converter) {
5097-
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
5098-
}
5099-
51005079
/**
51015080
* Returns the first item emitted by this {@code Observable}, or throws
51025081
* {@code NoSuchElementException} if it emits no items.
@@ -13913,19 +13892,17 @@ public final Observable<Timed<T>> timestamp(final TimeUnit unit, final Scheduler
1391313892
* <dt><b>Scheduler:</b></dt>
1391413893
* <dd>{@code to} does not operate by default on a particular {@link Scheduler}.</dd>
1391513894
* </dl>
13895+
* <p>History: 2.1.7 - experimental
1391613896
* @param <R> the resulting object type
1391713897
* @param converter the function that receives the current Observable instance and returns a value
13918-
* @return the value returned by the function
13898+
* @return the converted value
13899+
* @throws NullPointerException if converter is null
13900+
* @since 2.2
1391913901
*/
1392013902
@CheckReturnValue
1392113903
@SchedulerSupport(SchedulerSupport.NONE)
13922-
public final <R> R to(Function<? super Observable<T>, R> converter) {
13923-
try {
13924-
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
13925-
} catch (Throwable ex) {
13926-
Exceptions.throwIfFatal(ex);
13927-
throw ExceptionHelper.wrapOrThrow(ex);
13928-
}
13904+
public final <R> R to(@NonNull ObservableConverter<T, ? extends R> converter) {
13905+
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
1392913906
}
1393013907

1393113908
/**

src/main/java/io/reactivex/ObservableConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import io.reactivex.annotations.*;
1717

1818
/**
19-
* Convenience interface and callback used by the {@link Observable#as} operator to turn an Observable into another
19+
* Convenience interface and callback used by the {@link Observable#to} operator to turn an Observable into another
2020
* value fluently.
2121
* <p>History: 2.1.7 - experimental
2222
* @param <T> the upstream type

0 commit comments

Comments
 (0)