diff --git a/src/main/java/io/reactivex/rxjava3/core/Maybe.java b/src/main/java/io/reactivex/rxjava3/core/Maybe.java index 37ce1e99e2..f8cbdcdbe7 100644 --- a/src/main/java/io/reactivex/rxjava3/core/Maybe.java +++ b/src/main/java/io/reactivex/rxjava3/core/Maybe.java @@ -2932,7 +2932,7 @@ public final Completable concatMapCompletable(@NonNull Function * *

- * This operator is an alias for {@link #flatMapSingleElement(Function)}. + * This operator is an alias for {@link #flatMapSingle(Function)}. *

*
Scheduler:
*
{@code concatMapSingle} does not operate by default on a particular {@link Scheduler}.
@@ -2951,7 +2951,7 @@ public final Completable concatMapCompletable(@NonNull Function Maybe concatMapSingle(@NonNull Function> mapper) { - return flatMapSingleElement(mapper); + return flatMapSingle(mapper); } /** @@ -3832,33 +3832,6 @@ public final Flowable flatMapPublisher(@NonNull Function(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. - *

- * - *

- *
Scheduler:
- *
{@code flatMapSingle} does not operate by default on a particular {@link Scheduler}.
- *
- * - * @param 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 ReactiveX operators documentation: FlatMap - */ - @CheckReturnValue - @NonNull - @SchedulerSupport(SchedulerSupport.NONE) - public final Single flatMapSingle(@NonNull Function> 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}. @@ -3867,7 +3840,7 @@ public final Single flatMapSingle(@NonNull Function *
*
Scheduler:
- *
{@code flatMapSingleElement} does not operate by default on a particular {@link Scheduler}.
+ *
{@code flatMapSingle} does not operate by default on a particular {@link Scheduler}.
*
* *

History: 2.0.2 - experimental @@ -3883,9 +3856,9 @@ public final Single flatMapSingle(@NonNull Function Maybe flatMapSingleElement(@NonNull Function> mapper) { + public final Maybe flatMapSingle(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); - return RxJavaPlugins.onAssembly(new MaybeFlatMapSingleElement<>(this, mapper)); + return RxJavaPlugins.onAssembly(new MaybeFlatMapSingle<>(this, mapper)); } /** diff --git a/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingle.java b/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingle.java index 2199ea470b..aed71dc855 100644 --- a/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingle.java +++ b/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingle.java @@ -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; @@ -25,10 +24,12 @@ /** * Maps the success value of the source MaybeSource into a Single. + *

History: 2.0.2 - experimental * @param the input value type * @param the result value type + * @since 2.1 */ -public final class MaybeFlatMapSingle extends Single { +public final class MaybeFlatMapSingle extends Maybe { final MaybeSource source; @@ -40,7 +41,7 @@ public MaybeFlatMapSingle(MaybeSource source, Function downstream) { + protected void subscribeActual(MaybeObserver downstream) { source.subscribe(new FlatMapMaybeObserver<>(downstream, mapper)); } @@ -50,11 +51,11 @@ static final class FlatMapMaybeObserver private static final long serialVersionUID = 4827726964688405508L; - final SingleObserver downstream; + final MaybeObserver downstream; final Function> mapper; - FlatMapMaybeObserver(SingleObserver actual, Function> mapper) { + FlatMapMaybeObserver(MaybeObserver actual, Function> mapper) { this.downstream = actual; this.mapper = mapper; } @@ -88,9 +89,7 @@ public void onSuccess(T value) { return; } - if (!isDisposed()) { - ss.subscribe(new FlatMapSingleObserver(this, downstream)); - } + ss.subscribe(new FlatMapSingleObserver(this, downstream)); } @Override @@ -100,7 +99,7 @@ public void onError(Throwable e) { @Override public void onComplete() { - downstream.onError(new NoSuchElementException()); + downstream.onComplete(); } } @@ -108,9 +107,9 @@ static final class FlatMapSingleObserver implements SingleObserver { final AtomicReference parent; - final SingleObserver downstream; + final MaybeObserver downstream; - FlatMapSingleObserver(AtomicReference parent, SingleObserver downstream) { + FlatMapSingleObserver(AtomicReference parent, MaybeObserver downstream) { this.parent = parent; this.downstream = downstream; } diff --git a/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleElement.java b/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleElement.java deleted file mode 100644 index 49b7100575..0000000000 --- a/src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleElement.java +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Copyright (c) 2016-present, RxJava Contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is - * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See - * the License for the specific language governing permissions and limitations under the License. - */ - -package io.reactivex.rxjava3.internal.operators.maybe; - -import java.util.Objects; -import java.util.concurrent.atomic.AtomicReference; - -import io.reactivex.rxjava3.core.*; -import io.reactivex.rxjava3.disposables.Disposable; -import io.reactivex.rxjava3.exceptions.Exceptions; -import io.reactivex.rxjava3.functions.Function; -import io.reactivex.rxjava3.internal.disposables.DisposableHelper; - -/** - * Maps the success value of the source MaybeSource into a Single. - *

History: 2.0.2 - experimental - * @param the input value type - * @param the result value type - * @since 2.1 - */ -public final class MaybeFlatMapSingleElement extends Maybe { - - final MaybeSource source; - - final Function> mapper; - - public MaybeFlatMapSingleElement(MaybeSource source, Function> mapper) { - this.source = source; - this.mapper = mapper; - } - - @Override - protected void subscribeActual(MaybeObserver downstream) { - source.subscribe(new FlatMapMaybeObserver<>(downstream, mapper)); - } - - static final class FlatMapMaybeObserver - extends AtomicReference - implements MaybeObserver, Disposable { - - private static final long serialVersionUID = 4827726964688405508L; - - final MaybeObserver downstream; - - final Function> mapper; - - FlatMapMaybeObserver(MaybeObserver actual, Function> mapper) { - this.downstream = actual; - this.mapper = mapper; - } - - @Override - public void dispose() { - DisposableHelper.dispose(this); - } - - @Override - public boolean isDisposed() { - return DisposableHelper.isDisposed(get()); - } - - @Override - public void onSubscribe(Disposable d) { - if (DisposableHelper.setOnce(this, d)) { - downstream.onSubscribe(this); - } - } - - @Override - public void onSuccess(T value) { - SingleSource ss; - - try { - ss = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null SingleSource"); - } catch (Throwable ex) { - Exceptions.throwIfFatal(ex); - onError(ex); - return; - } - - ss.subscribe(new FlatMapSingleObserver(this, downstream)); - } - - @Override - public void onError(Throwable e) { - downstream.onError(e); - } - - @Override - public void onComplete() { - downstream.onComplete(); - } - } - - static final class FlatMapSingleObserver implements SingleObserver { - - final AtomicReference parent; - - final MaybeObserver downstream; - - FlatMapSingleObserver(AtomicReference parent, MaybeObserver downstream) { - this.parent = parent; - this.downstream = downstream; - } - - @Override - public void onSubscribe(final Disposable d) { - DisposableHelper.replace(parent, d); - } - - @Override - public void onSuccess(final R value) { - downstream.onSuccess(value); - } - - @Override - public void onError(final Throwable e) { - downstream.onError(e); - } - } -} diff --git a/src/test/java/io/reactivex/rxjava3/core/XFlatMapTest.java b/src/test/java/io/reactivex/rxjava3/core/XFlatMapTest.java index 7efeee401d..6e80b57904 100644 --- a/src/test/java/io/reactivex/rxjava3/core/XFlatMapTest.java +++ b/src/test/java/io/reactivex/rxjava3/core/XFlatMapTest.java @@ -505,6 +505,7 @@ public Completable apply(Integer v) throws Exception { } @Test + @Ignore public void maybeSingle() throws Exception { List errors = TestHelper.trackPluginErrors(); try { @@ -517,6 +518,7 @@ public Single apply(Integer v) throws Exception { return Single.error(new TestException()); } }) + .toSingle() .test(); cb.await(); diff --git a/src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleElementTest.java b/src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleElementTest.java index 3d85785add..cc92e023ab 100644 --- a/src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleElementTest.java +++ b/src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleElementTest.java @@ -22,8 +22,8 @@ public class MaybeFlatMapSingleElementTest extends RxJavaTest { @Test - public void flatMapSingleElementValue() { - Maybe.just(1).flatMapSingleElement(new Function>() { + public void flatMapSingleValue() { + Maybe.just(1).flatMapSingle(new Function>() { @Override public SingleSource apply(final Integer integer) throws Exception { if (integer == 1) { return Single.just(2); @@ -37,8 +37,8 @@ public void flatMapSingleElementValue() { } @Test - public void flatMapSingleElementValueDifferentType() { - Maybe.just(1).flatMapSingleElement(new Function>() { + public void flatMapSingleValueDifferentType() { + Maybe.just(1).flatMapSingle(new Function>() { @Override public SingleSource apply(final Integer integer) throws Exception { if (integer == 1) { return Single.just("2"); @@ -52,8 +52,8 @@ public void flatMapSingleElementValueDifferentType() { } @Test - public void flatMapSingleElementValueNull() { - Maybe.just(1).flatMapSingleElement(new Function>() { + public void flatMapSingleValueNull() { + Maybe.just(1).flatMapSingle(new Function>() { @Override public SingleSource apply(final Integer integer) throws Exception { return null; } @@ -65,8 +65,8 @@ public void flatMapSingleElementValueNull() { } @Test - public void flatMapSingleElementValueErrorThrown() { - Maybe.just(1).flatMapSingleElement(new Function>() { + public void flatMapSingleValueErrorThrown() { + Maybe.just(1).flatMapSingle(new Function>() { @Override public SingleSource apply(final Integer integer) throws Exception { throw new RuntimeException("something went terribly wrong!"); } @@ -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>() { + Maybe.error(exception).flatMapSingle(new Function>() { @Override public SingleSource apply(final Object integer) throws Exception { return Single.just(new Object()); } @@ -91,8 +91,8 @@ public void flatMapSingleElementError() { } @Test - public void flatMapSingleElementEmpty() { - Maybe.empty().flatMapSingleElement(new Function>() { + public void flatMapSingleEmpty() { + Maybe.empty().flatMapSingle(new Function>() { @Override public SingleSource apply(final Integer integer) throws Exception { return Single.just(2); } @@ -104,7 +104,7 @@ public void flatMapSingleElementEmpty() { @Test public void dispose() { - TestHelper.checkDisposed(Maybe.just(1).flatMapSingleElement(new Function>() { + TestHelper.checkDisposed(Maybe.just(1).flatMapSingle(new Function>() { @Override public SingleSource apply(final Integer integer) throws Exception { return Single.just(2); @@ -117,7 +117,7 @@ public void doubleOnSubscribe() { TestHelper.checkDoubleOnSubscribeMaybe(new Function, Maybe>() { @Override public Maybe apply(Maybe m) throws Exception { - return m.flatMapSingleElement(new Function>() { + return m.flatMapSingle(new Function>() { @Override public SingleSource apply(final Integer integer) throws Exception { return Single.just(2); @@ -130,7 +130,7 @@ public SingleSource apply(final Integer integer) throws Exception { @Test public void singleErrors() { Maybe.just(1) - .flatMapSingleElement(new Function>() { + .flatMapSingle(new Function>() { @Override public SingleSource apply(final Integer integer) throws Exception { return Single.error(new TestException()); diff --git a/src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleTest.java b/src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleTest.java index c4cef0d351..4904678a61 100644 --- a/src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleTest.java +++ b/src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFlatMapSingleTest.java @@ -34,6 +34,7 @@ public void flatMapSingleValue() { return Single.just(1); } }) + .toSingle() .test() .assertResult(2); } @@ -49,6 +50,7 @@ public void flatMapSingleValueDifferentType() { return Single.just("1"); } }) + .toSingle() .test() .assertResult("2"); } @@ -60,6 +62,7 @@ public void flatMapSingleValueNull() { return null; } }) + .toSingle() .to(TestHelper.testConsumer()) .assertNoValues() .assertError(NullPointerException.class) @@ -73,6 +76,7 @@ public void flatMapSingleValueErrorThrown() { throw new RuntimeException("something went terribly wrong!"); } }) + .toSingle() .to(TestHelper.testConsumer()) .assertNoValues() .assertError(RuntimeException.class) @@ -88,6 +92,7 @@ public void flatMapSingleError() { return Single.just(new Object()); } }) + .toSingle() .test() .assertError(exception); } @@ -99,6 +104,7 @@ public void flatMapSingleEmpty() { return Single.just(2); } }) + .toSingle() .test() .assertNoValues() .assertError(NoSuchElementException.class); @@ -111,7 +117,7 @@ public void dispose() { public SingleSource apply(final Integer integer) throws Exception { return Single.just(2); } - })); + }).toSingle()); } @Test @@ -124,7 +130,7 @@ public SingleSource apply(Maybe m) throws Exception { public SingleSource apply(final Integer integer) throws Exception { return Single.just(2); } - }); + }).toSingle(); } }); } @@ -138,6 +144,7 @@ public SingleSource apply(final Integer integer) throws Exception { return Single.error(new TestException()); } }) + .toSingle() .test() .assertFailure(TestException.class); }