From 1194ac073361571557e3d98eff6d0eb3a78b7654 Mon Sep 17 00:00:00 2001 From: jimi-21 Date: Tue, 17 Nov 2026 23:40:29 +0800 Subject: [PATCH] 4.x: Add Streamable collectInto operator --- .../io/reactivex/rxjava4/core/Streamable.java | 15 +++++ .../streamable/StreamableCollectIntoTest.java | 60 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/test/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableCollectIntoTest.java diff --git a/src/main/java/io/reactivex/rxjava4/core/Streamable.java b/src/main/java/io/reactivex/rxjava4/core/Streamable.java index b54e8c01fd..b3a311f1b0 100644 --- a/src/main/java/io/reactivex/rxjava4/core/Streamable.java +++ b/src/main/java/io/reactivex/rxjava4/core/Streamable.java @@ -989,6 +989,21 @@ default T blockingLast(@NonNull T defaultItem, @NonNull StreamerCancellation can return StreamableBlocking.blockingLast(this, defaultItem, cancellation); } + /** + * Collects all upstream items into the given mutable data structure and emits it as a {@code Single}. + * @param the accumulator and output type + * @param initialItem the mutable data structure that will collect the items + * @param collector the callback that modifies the data structure with each upstream item + * @return the new {@code Single} instance + * @throws NullPointerException if {@code initialItem} or {@code collector} is {@code null} + */ + @CheckReturnValue + @NonNull + default <@NonNull U> Single collectInto(@NonNull U initialItem, @NonNull BiConsumer collector) { + Objects.requireNonNull(initialItem, "initialItem is null"); + return collect(() -> initialItem, collector).lastOrError(); + } + /** * Counts all upstream items and emits the count as a {@code Single}. * @return the new {@code Single} instance diff --git a/src/test/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableCollectIntoTest.java b/src/test/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableCollectIntoTest.java new file mode 100644 index 0000000000..3275ddecaa --- /dev/null +++ b/src/test/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableCollectIntoTest.java @@ -0,0 +1,60 @@ +/* + * 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.rxjava4.internal.operators.streamable; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.Test; + +import io.reactivex.rxjava4.core.Streamable; +import io.reactivex.rxjava4.exceptions.TestException; + +public class StreamableCollectIntoTest extends StreamableBaseTest { + + @Test + public void multipleItems() throws Throwable { + Streamable.range(1, 5) + .collectInto(new ArrayList(), (list, item) -> list.add(item)) + .test() + .awaitDone(5, TimeUnit.SECONDS) + .assertResult(new ArrayList<>(List.of(1, 2, 3, 4, 5))); + } + + @Test + public void empty() throws Throwable { + Streamable.empty() + .collectInto(new ArrayList(), (list, item) -> list.add(item)) + .test() + .awaitDone(5, TimeUnit.SECONDS) + .assertResult(new ArrayList()); + } + + @Test + public void error() throws Throwable { + Streamable.error(new TestException("error")) + .collectInto(new ArrayList(), (list, item) -> list.add(item)) + .test() + .awaitDone(5, TimeUnit.SECONDS) + .assertFailure(TestException.class); + } + + @Test + public void nullArguments() { + assertThrows(NullPointerException.class, () -> Streamable.empty().collectInto((ArrayList) null, (list, item) -> list.add(item))); + assertThrows(NullPointerException.class, () -> Streamable.empty().collectInto(new ArrayList(), null)); + } +}