Skip to content

Commit 6a44e5d

Browse files
ZacSweersakarnokd
authored andcommitted
2.x: Implement as() (#5729)
* Implement Observable.as() * Implement Single.as() * Implement Maybe.as() * Implement Flowable.as() * Implement Completable.as() * Add Experimental annotations * Add throws doc * Fix docs and validation errors * Add @SInCE 2.1.7 - experimental * ParallelFlowable.as() * Start ConverterTest * Fix tests and update validator * Remove exceptions from signatures * Remove exception signature from implementations * Assert the full execution of extend() tests * Use test() helpers
1 parent 9521512 commit 6a44e5d

23 files changed

+826
-11
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,28 @@ public final Completable andThen(CompletableSource next) {
908908
return concatWith(next);
909909
}
910910

911+
/**
912+
* Calls the specified converter function during assembly time and returns its resulting value.
913+
* <p>
914+
* This allows fluent conversion to any other type.
915+
* <dl>
916+
* <dt><b>Scheduler:</b></dt>
917+
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
918+
* </dl>
919+
*
920+
* @param <R> the resulting object type
921+
* @param converter the function that receives the current Completable instance and returns a value
922+
* @return the converted value
923+
* @throws NullPointerException if converter is null
924+
* @since 2.1.7 - experimental
925+
*/
926+
@Experimental
927+
@CheckReturnValue
928+
@SchedulerSupport(SchedulerSupport.NONE)
929+
public final <R> R as(@NonNull CompletableConverter<? extends R> converter) {
930+
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
931+
}
932+
911933
/**
912934
* Subscribes to and awaits the termination of this Completable instance in a blocking manner and
913935
* rethrows any exception emitted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) 2016-present, RxJava Contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex;
15+
16+
import io.reactivex.annotations.*;
17+
18+
/**
19+
* Convenience interface and callback used by the {@link Completable#as} operator to turn a Completable into another
20+
* value fluently.
21+
*
22+
* @param <R> the output type
23+
* @since 2.1.7 - experimental
24+
*/
25+
@Experimental
26+
public interface CompletableConverter<R> {
27+
/**
28+
* Applies a function to the upstream Completable and returns a converted value of type {@code R}.
29+
*
30+
* @param upstream the upstream Completable instance
31+
* @return the converted value
32+
*/
33+
@NonNull
34+
R apply(@NonNull Completable upstream);
35+
}

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

+25
Original file line numberDiff line numberDiff line change
@@ -5237,6 +5237,31 @@ public final Single<Boolean> any(Predicate<? super T> predicate) {
52375237
return RxJavaPlugins.onAssembly(new FlowableAnySingle<T>(this, predicate));
52385238
}
52395239

5240+
/**
5241+
* Calls the specified converter function during assembly time and returns its resulting value.
5242+
* <p>
5243+
* This allows fluent conversion to any other type.
5244+
* <dl>
5245+
* <dt><b>Backpressure:</b></dt>
5246+
* <dd>The backpressure behavior depends on what happens in the {@code converter} function.</dd>
5247+
* <dt><b>Scheduler:</b></dt>
5248+
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
5249+
* </dl>
5250+
*
5251+
* @param <R> the resulting object type
5252+
* @param converter the function that receives the current Flowable instance and returns a value
5253+
* @return the converted value
5254+
* @throws NullPointerException if converter is null
5255+
* @since 2.1.7 - experimental
5256+
*/
5257+
@Experimental
5258+
@CheckReturnValue
5259+
@BackpressureSupport(BackpressureKind.SPECIAL)
5260+
@SchedulerSupport(SchedulerSupport.NONE)
5261+
public final <R> R as(@NonNull FlowableConverter<T, ? extends R> converter) {
5262+
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
5263+
}
5264+
52405265
/**
52415266
* Returns the first item emitted by this {@code Flowable}, or throws
52425267
* {@code NoSuchElementException} if it emits no items.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2016-present, RxJava Contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex;
15+
16+
import io.reactivex.annotations.*;
17+
18+
/**
19+
* Convenience interface and callback used by the {@link Flowable#as} operator to turn a Flowable into another
20+
* value fluently.
21+
*
22+
* @param <T> the upstream type
23+
* @param <R> the output type
24+
* @since 2.1.7 - experimental
25+
*/
26+
@Experimental
27+
public interface FlowableConverter<T, R> {
28+
/**
29+
* Applies a function to the upstream Flowable and returns a converted value of type {@code R}.
30+
*
31+
* @param upstream the upstream Flowable instance
32+
* @return the converted value
33+
*/
34+
@NonNull
35+
R apply(@NonNull Flowable<T> upstream);
36+
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,28 @@ public final Maybe<T> ambWith(MaybeSource<? extends T> other) {
19891989
return ambArray(this, other);
19901990
}
19911991

1992+
/**
1993+
* Calls the specified converter function during assembly time and returns its resulting value.
1994+
* <p>
1995+
* This allows fluent conversion to any other type.
1996+
* <dl>
1997+
* <dt><b>Scheduler:</b></dt>
1998+
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
1999+
* </dl>
2000+
*
2001+
* @param <R> the resulting object type
2002+
* @param converter the function that receives the current Maybe instance and returns a value
2003+
* @return the converted value
2004+
* @throws NullPointerException if converter is null
2005+
* @since 2.1.7 - experimental
2006+
*/
2007+
@Experimental
2008+
@CheckReturnValue
2009+
@SchedulerSupport(SchedulerSupport.NONE)
2010+
public final <R> R as(@NonNull MaybeConverter<T, ? extends R> converter) {
2011+
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
2012+
}
2013+
19922014
/**
19932015
* Waits in a blocking fashion until the current Maybe signals a success value (which is returned),
19942016
* null if completed or an exception (which is propagated).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2016-present, RxJava Contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex;
15+
16+
import io.reactivex.annotations.*;
17+
18+
/**
19+
* Convenience interface and callback used by the {@link Maybe#as} operator to turn a Maybe into another
20+
* value fluently.
21+
*
22+
* @param <T> the upstream type
23+
* @param <R> the output type
24+
* @since 2.1.7 - experimental
25+
*/
26+
@Experimental
27+
public interface MaybeConverter<T, R> {
28+
/**
29+
* Applies a function to the upstream Maybe and returns a converted value of type {@code R}.
30+
*
31+
* @param upstream the upstream Maybe instance
32+
* @return the converted value
33+
*/
34+
@NonNull
35+
R apply(@NonNull Maybe<T> upstream);
36+
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -4800,6 +4800,28 @@ public final Single<Boolean> any(Predicate<? super T> predicate) {
48004800
return RxJavaPlugins.onAssembly(new ObservableAnySingle<T>(this, predicate));
48014801
}
48024802

4803+
/**
4804+
* Calls the specified converter function during assembly time and returns its resulting value.
4805+
* <p>
4806+
* This allows fluent conversion to any other type.
4807+
* <dl>
4808+
* <dt><b>Scheduler:</b></dt>
4809+
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
4810+
* </dl>
4811+
*
4812+
* @param <R> the resulting object type
4813+
* @param converter the function that receives the current Observable instance and returns a value
4814+
* @return the converted value
4815+
* @throws NullPointerException if converter is null
4816+
* @since 2.1.7 - experimental
4817+
*/
4818+
@Experimental
4819+
@CheckReturnValue
4820+
@SchedulerSupport(SchedulerSupport.NONE)
4821+
public final <R> R as(@NonNull ObservableConverter<T, ? extends R> converter) {
4822+
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
4823+
}
4824+
48034825
/**
48044826
* Returns the first item emitted by this {@code Observable}, or throws
48054827
* {@code NoSuchElementException} if it emits no items.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2016-present, RxJava Contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex;
15+
16+
import io.reactivex.annotations.*;
17+
18+
/**
19+
* Convenience interface and callback used by the {@link Observable#as} operator to turn an Observable into another
20+
* value fluently.
21+
*
22+
* @param <T> the upstream type
23+
* @param <R> the output type
24+
* @since 2.1.7 - experimental
25+
*/
26+
@Experimental
27+
public interface ObservableConverter<T, R> {
28+
/**
29+
* Applies a function to the upstream Observable and returns a converted value of type {@code R}.
30+
*
31+
* @param upstream the upstream Observable instance
32+
* @return the converted value
33+
*/
34+
@NonNull
35+
R apply(@NonNull Observable<T> upstream);
36+
}

src/main/java/io/reactivex/Single.java

+22
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,28 @@ public final Single<T> ambWith(SingleSource<? extends T> other) {
15221522
return ambArray(this, other);
15231523
}
15241524

1525+
/**
1526+
* Calls the specified converter function during assembly time and returns its resulting value.
1527+
* <p>
1528+
* This allows fluent conversion to any other type.
1529+
* <dl>
1530+
* <dt><b>Scheduler:</b></dt>
1531+
* <dd>{@code as} does not operate by default on a particular {@link Scheduler}.</dd>
1532+
* </dl>
1533+
*
1534+
* @param <R> the resulting object type
1535+
* @param converter the function that receives the current Single instance and returns a value
1536+
* @return the converted value
1537+
* @throws NullPointerException if converter is null
1538+
* @since 2.1.7 - experimental
1539+
*/
1540+
@Experimental
1541+
@CheckReturnValue
1542+
@SchedulerSupport(SchedulerSupport.NONE)
1543+
public final <R> R as(@NonNull SingleConverter<T, ? extends R> converter) {
1544+
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
1545+
}
1546+
15251547
/**
15261548
* Hides the identity of the current Single, including the Disposable that is sent
15271549
* to the downstream via {@code onSubscribe()}.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2016-present, RxJava Contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex;
15+
16+
import io.reactivex.annotations.*;
17+
18+
/**
19+
* Convenience interface and callback used by the {@link Single#as} operator to turn a Single into another
20+
* value fluently.
21+
*
22+
* @param <T> the upstream type
23+
* @param <R> the output type
24+
* @since 2.1.7 - experimental
25+
*/
26+
@Experimental
27+
public interface SingleConverter<T, R> {
28+
/**
29+
* Applies a function to the upstream Single and returns a converted value of type {@code R}.
30+
*
31+
* @param upstream the upstream Single instance
32+
* @return the converted value
33+
*/
34+
@NonNull
35+
R apply(@NonNull Single<T> upstream);
36+
}

src/main/java/io/reactivex/parallel/ParallelFlowable.java

+18
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ public static <T> ParallelFlowable<T> from(@NonNull Publisher<? extends T> sourc
122122
return RxJavaPlugins.onAssembly(new ParallelFromPublisher<T>(source, parallelism, prefetch));
123123
}
124124

125+
/**
126+
* Calls the specified converter function during assembly time and returns its resulting value.
127+
* <p>
128+
* This allows fluent conversion to any other type.
129+
*
130+
* @param <R> the resulting object type
131+
* @param converter the function that receives the current ParallelFlowable instance and returns a value
132+
* @return the converted value
133+
* @throws NullPointerException if converter is null
134+
* @since 2.1.7 - experimental
135+
*/
136+
@Experimental
137+
@CheckReturnValue
138+
@NonNull
139+
public final <R> R as(@NonNull ParallelFlowableConverter<T, R> converter) {
140+
return ObjectHelper.requireNonNull(converter, "converter is null").apply(this);
141+
}
142+
125143
/**
126144
* Maps the source values on each 'rail' to another value.
127145
* <p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2016-present, RxJava Contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex.parallel;
15+
16+
import io.reactivex.annotations.*;
17+
18+
/**
19+
* Convenience interface and callback used by the {@link ParallelFlowable#as} operator to turn a ParallelFlowable into
20+
* another value fluently.
21+
*
22+
* @param <T> the upstream type
23+
* @param <R> the output type
24+
* @since 2.1.7 - experimental
25+
*/
26+
@Experimental
27+
public interface ParallelFlowableConverter<T, R> {
28+
/**
29+
* Applies a function to the upstream ParallelFlowable and returns a converted value of type {@code R}.
30+
*
31+
* @param upstream the upstream ParallelFlowable instance
32+
* @return the converted value
33+
*/
34+
@NonNull
35+
R apply(@NonNull ParallelFlowable<T> upstream);
36+
}

0 commit comments

Comments
 (0)