From 12aa0dc9af14fcef1415de61975cda25e4ba463e Mon Sep 17 00:00:00 2001 From: akarnokd Date: Mon, 27 Jan 2020 12:37:25 +0100 Subject: [PATCH] 3.x: Add Completable.sequenceEqual --- .../reactivex/rxjava3/core/Completable.java | 25 ++++++++++- .../CompletableSequenceEqualTest.java | 43 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableSequenceEqualTest.java diff --git a/src/main/java/io/reactivex/rxjava3/core/Completable.java b/src/main/java/io/reactivex/rxjava3/core/Completable.java index 253610ef80..e1787b9b46 100644 --- a/src/main/java/io/reactivex/rxjava3/core/Completable.java +++ b/src/main/java/io/reactivex/rxjava3/core/Completable.java @@ -29,7 +29,7 @@ import io.reactivex.rxjava3.internal.operators.completable.*; import io.reactivex.rxjava3.internal.operators.maybe.*; import io.reactivex.rxjava3.internal.operators.mixed.*; -import io.reactivex.rxjava3.internal.operators.single.SingleDelayWithCompletable; +import io.reactivex.rxjava3.internal.operators.single.*; import io.reactivex.rxjava3.observers.TestObserver; import io.reactivex.rxjava3.plugins.RxJavaPlugins; import io.reactivex.rxjava3.schedulers.Schedulers; @@ -414,6 +414,29 @@ public static Completable create(@NonNull CompletableOnSubscribe source) { return RxJavaPlugins.onAssembly(new CompletableCreate(source)); } + /** + * Compares two {@link CompletableSource}s and emits {@code true} via a {@link Single} if both complete. + *

+ * + *

+ *
Scheduler:
+ *
{@code sequenceEqual} does not operate by default on a particular {@link Scheduler}.
+ *
+ * @param source1 the first {@code CompletableSource} instance + * @param source2 the second {@code CompletableSource} instance + * @return the new {@code Single} instance + * @throws NullPointerException if {@code source1} or {@code source2} is {@code null} + * @since 3.0.0 + */ + @CheckReturnValue + @NonNull + @SchedulerSupport(SchedulerSupport.NONE) + public static Single sequenceEqual(@NonNull CompletableSource source1, @NonNull CompletableSource source2) { // NOPMD + Objects.requireNonNull(source1, "source1 is null"); + Objects.requireNonNull(source2, "source2 is null"); + return mergeArrayDelayError(source1, source2).andThen(Single.just(true)); + } + /** * Constructs a {@code Completable} instance by wrapping the given source callback * without any safeguards; you should manage the lifecycle and response diff --git a/src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableSequenceEqualTest.java b/src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableSequenceEqualTest.java new file mode 100644 index 0000000000..b7b68ad321 --- /dev/null +++ b/src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableSequenceEqualTest.java @@ -0,0 +1,43 @@ +/** + * 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.completable; + +import org.junit.Test; + +import io.reactivex.rxjava3.core.Completable; +import io.reactivex.rxjava3.exceptions.TestException; + +public class CompletableSequenceEqualTest { + + @Test + public void bothComplete() { + Completable.sequenceEqual(Completable.complete(), Completable.complete()) + .test() + .assertResult(true); + } + + @Test + public void firstFails() { + Completable.sequenceEqual(Completable.error(new TestException()), Completable.complete()) + .test() + .assertFailure(TestException.class); + } + + @Test + public void secondFails() { + Completable.sequenceEqual(Completable.complete(), Completable.error(new TestException())) + .test() + .assertFailure(TestException.class); + } +}