Skip to content
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
15 changes: 15 additions & 0 deletions src/main/java/io/reactivex/rxjava4/core/Streamable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <U> 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<U> collectInto(@NonNull U initialItem, @NonNull BiConsumer<? super U, ? super T> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Integer>(), (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.<Integer>empty()
.collectInto(new ArrayList<Integer>(), (list, item) -> list.add(item))
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertResult(new ArrayList<Integer>());
}

@Test
public void error() throws Throwable {
Streamable.<Integer>error(new TestException("error"))
.collectInto(new ArrayList<Integer>(), (list, item) -> list.add(item))
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertFailure(TestException.class);
}

@Test
public void nullArguments() {
assertThrows(NullPointerException.class, () -> Streamable.<Integer>empty().collectInto((ArrayList<Integer>) null, (list, item) -> list.add(item)));
assertThrows(NullPointerException.class, () -> Streamable.empty().collectInto(new ArrayList<Integer>(), null));
}
}
Loading