Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.x: Swap Maybe.flatMapSingle and Maybe.flatMapSingleElement #6891

Merged
merged 5 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 5 additions & 32 deletions src/main/java/io/reactivex/rxjava3/core/Maybe.java
Original file line number Diff line number Diff line change
Expand Up @@ -2932,7 +2932,7 @@ public final Completable concatMapCompletable(@NonNull Function<? super T, ? ext
* <p>
* <img width="640" height="315" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.concatMapSingle.png" alt="">
* <p>
* This operator is an alias for {@link #flatMapSingleElement(Function)}.
* This operator is an alias for {@link #flatMapSingle(Function)}.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code concatMapSingle} does not operate by default on a particular {@link Scheduler}.</dd>
Expand All @@ -2951,7 +2951,7 @@ public final Completable concatMapCompletable(@NonNull Function<? super T, ? ext
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> Maybe<R> concatMapSingle(@NonNull Function<? super T, ? extends SingleSource<? extends R>> mapper) {
return flatMapSingleElement(mapper);
return flatMapSingle(mapper);
}

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

/**
* Returns a {@link Single} based on applying a specified function to the item emitted by the
* current {@code Maybe}, where that function returns a {@code Single}.
* When this {@code Maybe} completes a {@link NoSuchElementException} will be thrown.
* <p>
* <img width="640" height="346" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.flatMapSingle3.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code flatMapSingle} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param <R> the result value type
* @param mapper
* a function that, when applied to the item emitted by the current {@code Maybe}, returns a
* {@code Single}
* @return the new {@code Single} instance
* @throws NullPointerException if {@code mapper} is {@code null}
* @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a>
*/
@CheckReturnValue
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> Single<R> flatMapSingle(@NonNull Function<? super T, ? extends SingleSource<? extends R>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
return RxJavaPlugins.onAssembly(new MaybeFlatMapSingle<>(this, mapper));
}

/**
* Returns a {@code Maybe} based on applying a specified function to the item emitted by the
* current {@code Maybe}, where that function returns a {@link Single}.
Expand All @@ -3867,7 +3840,7 @@ public final <R> Single<R> flatMapSingle(@NonNull Function<? super T, ? extends
* <img width="640" height="357" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.flatMapSingle.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code flatMapSingleElement} does not operate by default on a particular {@link Scheduler}.</dd>
* <dd>{@code flatMapSingle} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* <p>History: 2.0.2 - experimental
Expand All @@ -3883,9 +3856,9 @@ public final <R> Single<R> flatMapSingle(@NonNull Function<? super T, ? extends
@CheckReturnValue
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
public final <R> Maybe<R> flatMapSingleElement(@NonNull Function<? super T, ? extends SingleSource<? extends R>> mapper) {
public final <R> Maybe<R> flatMapSingle(@NonNull Function<? super T, ? extends SingleSource<? extends R>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
return RxJavaPlugins.onAssembly(new MaybeFlatMapSingleElement<>(this, mapper));
return RxJavaPlugins.onAssembly(new MaybeFlatMapSingle<>(this, mapper));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

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

import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -25,10 +24,12 @@

/**
* Maps the success value of the source MaybeSource into a Single.
* <p>History: 2.0.2 - experimental
* @param <T> the input value type
* @param <R> the result value type
* @since 2.1
*/
public final class MaybeFlatMapSingle<T, R> extends Single<R> {
public final class MaybeFlatMapSingle<T, R> extends Maybe<R> {

final MaybeSource<T> source;

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

@Override
protected void subscribeActual(SingleObserver<? super R> downstream) {
protected void subscribeActual(MaybeObserver<? super R> downstream) {
source.subscribe(new FlatMapMaybeObserver<>(downstream, mapper));
}

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

private static final long serialVersionUID = 4827726964688405508L;

final SingleObserver<? super R> downstream;
final MaybeObserver<? super R> downstream;

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

FlatMapMaybeObserver(SingleObserver<? super R> actual, Function<? super T, ? extends SingleSource<? extends R>> mapper) {
FlatMapMaybeObserver(MaybeObserver<? super R> actual, Function<? super T, ? extends SingleSource<? extends R>> mapper) {
this.downstream = actual;
this.mapper = mapper;
}
Expand Down Expand Up @@ -88,9 +89,7 @@ public void onSuccess(T value) {
return;
}

if (!isDisposed()) {
ss.subscribe(new FlatMapSingleObserver<R>(this, downstream));
}
ss.subscribe(new FlatMapSingleObserver<R>(this, downstream));
}

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

@Override
public void onComplete() {
downstream.onError(new NoSuchElementException());
downstream.onComplete();
}
}

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

final AtomicReference<Disposable> parent;

final SingleObserver<? super R> downstream;
final MaybeObserver<? super R> downstream;

FlatMapSingleObserver(AtomicReference<Disposable> parent, SingleObserver<? super R> downstream) {
FlatMapSingleObserver(AtomicReference<Disposable> parent, MaybeObserver<? super R> downstream) {
this.parent = parent;
this.downstream = downstream;
}
Expand Down

This file was deleted.

2 changes: 2 additions & 0 deletions src/test/java/io/reactivex/rxjava3/core/XFlatMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ public Completable apply(Integer v) throws Exception {
}

@Test
@Ignore
public void maybeSingle() throws Exception {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
Expand All @@ -517,6 +518,7 @@ public Single<Integer> apply(Integer v) throws Exception {
return Single.<Integer>error(new TestException());
}
})
.toSingle()
.test();

cb.await();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

public class MaybeFlatMapSingleElementTest extends RxJavaTest {
@Test
public void flatMapSingleElementValue() {
Maybe.just(1).flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
public void flatMapSingleValue() {
Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
if (integer == 1) {
return Single.just(2);
Expand All @@ -37,8 +37,8 @@ public void flatMapSingleElementValue() {
}

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

@Test
public void flatMapSingleElementValueNull() {
Maybe.just(1).flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
public void flatMapSingleValueNull() {
Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
@Override public SingleSource<Integer> apply(final Integer integer) throws Exception {
return null;
}
Expand All @@ -65,8 +65,8 @@ public void flatMapSingleElementValueNull() {
}

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

@Test
public void flatMapSingleElementError() {
public void flatMapSingleError() {
RuntimeException exception = new RuntimeException("test");

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

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

@Test
public void dispose() {
TestHelper.checkDisposed(Maybe.just(1).flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
TestHelper.checkDisposed(Maybe.just(1).flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
@Override
public SingleSource<Integer> apply(final Integer integer) throws Exception {
return Single.just(2);
Expand All @@ -117,7 +117,7 @@ public void doubleOnSubscribe() {
TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Integer>, Maybe<Integer>>() {
@Override
public Maybe<Integer> apply(Maybe<Integer> m) throws Exception {
return m.flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
return m.flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
@Override
public SingleSource<Integer> apply(final Integer integer) throws Exception {
return Single.just(2);
Expand All @@ -130,7 +130,7 @@ public SingleSource<Integer> apply(final Integer integer) throws Exception {
@Test
public void singleErrors() {
Maybe.just(1)
.flatMapSingleElement(new Function<Integer, SingleSource<Integer>>() {
.flatMapSingle(new Function<Integer, SingleSource<Integer>>() {
@Override
public SingleSource<Integer> apply(final Integer integer) throws Exception {
return Single.error(new TestException());
Expand Down
Loading