Skip to content

Commit f1a795d

Browse files
slisaasquatchakarnokd
authored andcommitted
3.x: Swap Maybe.flatMapSingle and Maybe.flatMapSingleElement (#6891)
* Deleted Maybe.flatMapSingle And replaced tests with flatMapSingle().toSingle() * Renamed Maybe.flatMapSingleElement to flatMapSingle * Deleted unused MaybeFlatMapSingle operator * Renamed operator MaybeFlatMapSingleElement to MaybeFlatMapSingle * Add Ignore to XFlatMapTest.maybeSingle
1 parent b3516b2 commit f1a795d

File tree

6 files changed

+41
-192
lines changed

6 files changed

+41
-192
lines changed

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

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,7 +2932,7 @@ public final Completable concatMapCompletable(@NonNull Function<? super T, ? ext
29322932
* <p>
29332933
* <img width="640" height="315" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.concatMapSingle.png" alt="">
29342934
* <p>
2935-
* This operator is an alias for {@link #flatMapSingleElement(Function)}.
2935+
* This operator is an alias for {@link #flatMapSingle(Function)}.
29362936
* <dl>
29372937
* <dt><b>Scheduler:</b></dt>
29382938
* <dd>{@code concatMapSingle} does not operate by default on a particular {@link Scheduler}.</dd>
@@ -2951,7 +2951,7 @@ public final Completable concatMapCompletable(@NonNull Function<? super T, ? ext
29512951
@NonNull
29522952
@SchedulerSupport(SchedulerSupport.NONE)
29532953
public final <R> Maybe<R> concatMapSingle(@NonNull Function<? super T, ? extends SingleSource<? extends R>> mapper) {
2954-
return flatMapSingleElement(mapper);
2954+
return flatMapSingle(mapper);
29552955
}
29562956

29572957
/**
@@ -3832,33 +3832,6 @@ public final <R> Flowable<R> flatMapPublisher(@NonNull Function<? super T, ? ext
38323832
return RxJavaPlugins.onAssembly(new MaybeFlatMapPublisher<>(this, mapper));
38333833
}
38343834

3835-
/**
3836-
* Returns a {@link Single} based on applying a specified function to the item emitted by the
3837-
* current {@code Maybe}, where that function returns a {@code Single}.
3838-
* When this {@code Maybe} completes a {@link NoSuchElementException} will be thrown.
3839-
* <p>
3840-
* <img width="640" height="346" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.flatMapSingle3.png" alt="">
3841-
* <dl>
3842-
* <dt><b>Scheduler:</b></dt>
3843-
* <dd>{@code flatMapSingle} does not operate by default on a particular {@link Scheduler}.</dd>
3844-
* </dl>
3845-
*
3846-
* @param <R> the result value type
3847-
* @param mapper
3848-
* a function that, when applied to the item emitted by the current {@code Maybe}, returns a
3849-
* {@code Single}
3850-
* @return the new {@code Single} instance
3851-
* @throws NullPointerException if {@code mapper} is {@code null}
3852-
* @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a>
3853-
*/
3854-
@CheckReturnValue
3855-
@NonNull
3856-
@SchedulerSupport(SchedulerSupport.NONE)
3857-
public final <R> Single<R> flatMapSingle(@NonNull Function<? super T, ? extends SingleSource<? extends R>> mapper) {
3858-
Objects.requireNonNull(mapper, "mapper is null");
3859-
return RxJavaPlugins.onAssembly(new MaybeFlatMapSingle<>(this, mapper));
3860-
}
3861-
38623835
/**
38633836
* Returns a {@code Maybe} based on applying a specified function to the item emitted by the
38643837
* current {@code Maybe}, where that function returns a {@link Single}.
@@ -3867,7 +3840,7 @@ public final <R> Single<R> flatMapSingle(@NonNull Function<? super T, ? extends
38673840
* <img width="640" height="357" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.flatMapSingle.png" alt="">
38683841
* <dl>
38693842
* <dt><b>Scheduler:</b></dt>
3870-
* <dd>{@code flatMapSingleElement} does not operate by default on a particular {@link Scheduler}.</dd>
3843+
* <dd>{@code flatMapSingle} does not operate by default on a particular {@link Scheduler}.</dd>
38713844
* </dl>
38723845
*
38733846
* <p>History: 2.0.2 - experimental
@@ -3883,9 +3856,9 @@ public final <R> Single<R> flatMapSingle(@NonNull Function<? super T, ? extends
38833856
@CheckReturnValue
38843857
@NonNull
38853858
@SchedulerSupport(SchedulerSupport.NONE)
3886-
public final <R> Maybe<R> flatMapSingleElement(@NonNull Function<? super T, ? extends SingleSource<? extends R>> mapper) {
3859+
public final <R> Maybe<R> flatMapSingle(@NonNull Function<? super T, ? extends SingleSource<? extends R>> mapper) {
38873860
Objects.requireNonNull(mapper, "mapper is null");
3888-
return RxJavaPlugins.onAssembly(new MaybeFlatMapSingleElement<>(this, mapper));
3861+
return RxJavaPlugins.onAssembly(new MaybeFlatMapSingle<>(this, mapper));
38893862
}
38903863

38913864
/**

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingle.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
package io.reactivex.rxjava3.internal.operators.maybe;
1515

16-
import java.util.NoSuchElementException;
1716
import java.util.Objects;
1817
import java.util.concurrent.atomic.AtomicReference;
1918

@@ -25,10 +24,12 @@
2524

2625
/**
2726
* Maps the success value of the source MaybeSource into a Single.
27+
* <p>History: 2.0.2 - experimental
2828
* @param <T> the input value type
2929
* @param <R> the result value type
30+
* @since 2.1
3031
*/
31-
public final class MaybeFlatMapSingle<T, R> extends Single<R> {
32+
public final class MaybeFlatMapSingle<T, R> extends Maybe<R> {
3233

3334
final MaybeSource<T> source;
3435

@@ -40,7 +41,7 @@ public MaybeFlatMapSingle(MaybeSource<T> source, Function<? super T, ? extends S
4041
}
4142

4243
@Override
43-
protected void subscribeActual(SingleObserver<? super R> downstream) {
44+
protected void subscribeActual(MaybeObserver<? super R> downstream) {
4445
source.subscribe(new FlatMapMaybeObserver<>(downstream, mapper));
4546
}
4647

@@ -50,11 +51,11 @@ static final class FlatMapMaybeObserver<T, R>
5051

5152
private static final long serialVersionUID = 4827726964688405508L;
5253

53-
final SingleObserver<? super R> downstream;
54+
final MaybeObserver<? super R> downstream;
5455

5556
final Function<? super T, ? extends SingleSource<? extends R>> mapper;
5657

57-
FlatMapMaybeObserver(SingleObserver<? super R> actual, Function<? super T, ? extends SingleSource<? extends R>> mapper) {
58+
FlatMapMaybeObserver(MaybeObserver<? super R> actual, Function<? super T, ? extends SingleSource<? extends R>> mapper) {
5859
this.downstream = actual;
5960
this.mapper = mapper;
6061
}
@@ -88,9 +89,7 @@ public void onSuccess(T value) {
8889
return;
8990
}
9091

91-
if (!isDisposed()) {
92-
ss.subscribe(new FlatMapSingleObserver<R>(this, downstream));
93-
}
92+
ss.subscribe(new FlatMapSingleObserver<R>(this, downstream));
9493
}
9594

9695
@Override
@@ -100,17 +99,17 @@ public void onError(Throwable e) {
10099

101100
@Override
102101
public void onComplete() {
103-
downstream.onError(new NoSuchElementException());
102+
downstream.onComplete();
104103
}
105104
}
106105

107106
static final class FlatMapSingleObserver<R> implements SingleObserver<R> {
108107

109108
final AtomicReference<Disposable> parent;
110109

111-
final SingleObserver<? super R> downstream;
110+
final MaybeObserver<? super R> downstream;
112111

113-
FlatMapSingleObserver(AtomicReference<Disposable> parent, SingleObserver<? super R> downstream) {
112+
FlatMapSingleObserver(AtomicReference<Disposable> parent, MaybeObserver<? super R> downstream) {
114113
this.parent = parent;
115114
this.downstream = downstream;
116115
}

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleElement.java

Lines changed: 0 additions & 132 deletions
This file was deleted.

src/test/java/io/reactivex/rxjava3/core/XFlatMapTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ public Completable apply(Integer v) throws Exception {
505505
}
506506

507507
@Test
508+
@Ignore
508509
public void maybeSingle() throws Exception {
509510
List<Throwable> errors = TestHelper.trackPluginErrors();
510511
try {
@@ -517,6 +518,7 @@ public Single<Integer> apply(Integer v) throws Exception {
517518
return Single.<Integer>error(new TestException());
518519
}
519520
})
521+
.toSingle()
520522
.test();
521523

522524
cb.await();

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleElementTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
public class MaybeFlatMapSingleElementTest extends RxJavaTest {
2424
@Test
25-
public void flatMapSingleElementValue() {
26-
Maybe.just(1).flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
25+
public void flatMapSingleValue() {
26+
Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
2727
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
2828
if (integer == 1) {
2929
return Single.just(2);
@@ -37,8 +37,8 @@ public void flatMapSingleElementValue() {
3737
}
3838

3939
@Test
40-
public void flatMapSingleElementValueDifferentType() {
41-
Maybe.just(1).flatMapSingleElement(new Function<Integer, SingleSource<String>>() {
40+
public void flatMapSingleValueDifferentType() {
41+
Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<String>>() {
4242
@Override public SingleSource<String> apply(final Integer integer) throws Exception {
4343
if (integer == 1) {
4444
return Single.just("2");
@@ -52,8 +52,8 @@ public void flatMapSingleElementValueDifferentType() {
5252
}
5353

5454
@Test
55-
public void flatMapSingleElementValueNull() {
56-
Maybe.just(1).flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
55+
public void flatMapSingleValueNull() {
56+
Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
5757
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
5858
return null;
5959
}
@@ -65,8 +65,8 @@ public void flatMapSingleElementValueNull() {
6565
}
6666

6767
@Test
68-
public void flatMapSingleElementValueErrorThrown() {
69-
Maybe.just(1).flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
68+
public void flatMapSingleValueErrorThrown() {
69+
Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
7070
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
7171
throw new RuntimeException("something went terribly wrong!");
7272
}
@@ -78,10 +78,10 @@ public void flatMapSingleElementValueErrorThrown() {
7878
}
7979

8080
@Test
81-
public void flatMapSingleElementError() {
81+
public void flatMapSingleError() {
8282
RuntimeException exception = new RuntimeException("test");
8383

84-
Maybe.error(exception).flatMapSingleElement(new Function<Object, SingleSource<Object>>() {
84+
Maybe.error(exception).flatMapSingle(new Function<Object, SingleSource<Object>>() {
8585
@Override public SingleSource<Object> apply(final Object integer) throws Exception {
8686
return Single.just(new Object());
8787
}
@@ -91,8 +91,8 @@ public void flatMapSingleElementError() {
9191
}
9292

9393
@Test
94-
public void flatMapSingleElementEmpty() {
95-
Maybe.<Integer>empty().flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
94+
public void flatMapSingleEmpty() {
95+
Maybe.<Integer>empty().flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
9696
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
9797
return Single.just(2);
9898
}
@@ -104,7 +104,7 @@ public void flatMapSingleElementEmpty() {
104104

105105
@Test
106106
public void dispose() {
107-
TestHelper.checkDisposed(Maybe.just(1).flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
107+
TestHelper.checkDisposed(Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
108108
@Override
109109
public SingleSource<Integer> apply(final Integer integer) throws Exception {
110110
return Single.just(2);
@@ -117,7 +117,7 @@ public void doubleOnSubscribe() {
117117
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Integer>, Maybe<Integer>>() {
118118
@Override
119119
public Maybe<Integer> apply(Maybe<Integer> m) throws Exception {
120-
return m.flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
120+
return m.flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
121121
@Override
122122
public SingleSource<Integer> apply(final Integer integer) throws Exception {
123123
return Single.just(2);
@@ -130,7 +130,7 @@ public SingleSource<Integer> apply(final Integer integer) throws Exception {
130130
@Test
131131
public void singleErrors() {
132132
Maybe.just(1)
133-
.flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
133+
.flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
134134
@Override
135135
public SingleSource<Integer> apply(final Integer integer) throws Exception {
136136
return Single.error(new TestException());

0 commit comments

Comments
 (0)