Skip to content

Commit ca68c46

Browse files
authored
4.x: Add Streamable collectInto operator (#8397)
1 parent dea167e commit ca68c46

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

‎src/main/java/io/reactivex/rxjava4/core/Streamable.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,21 @@ default T blockingLast(@NonNull T defaultItem, @NonNull StreamerCancellation can
989989
return StreamableBlocking.blockingLast(this, defaultItem, cancellation);
990990
}
991991

992+
/**
993+
* Collects all upstream items into the given mutable data structure and emits it as a {@code Single}.
994+
* @param <U> the accumulator and output type
995+
* @param initialItem the mutable data structure that will collect the items
996+
* @param collector the callback that modifies the data structure with each upstream item
997+
* @return the new {@code Single} instance
998+
* @throws NullPointerException if {@code initialItem} or {@code collector} is {@code null}
999+
*/
1000+
@CheckReturnValue
1001+
@NonNull
1002+
default <@NonNull U> Single<U> collectInto(@NonNull U initialItem, @NonNull BiConsumer<? super U, ? super T> collector) {
1003+
Objects.requireNonNull(initialItem, "initialItem is null");
1004+
return collect(() -> initialItem, collector).lastOrError();
1005+
}
1006+
9921007
/**
9931008
* Counts all upstream items and emits the count as a {@code Single}.
9941009
* @return the new {@code Single} instance
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2016-present, RxJava Contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex.rxjava4.internal.operators.streamable;
15+
16+
import static org.junit.jupiter.api.Assertions.assertThrows;
17+
18+
import java.util.*;
19+
import java.util.concurrent.TimeUnit;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import io.reactivex.rxjava4.core.Streamable;
24+
import io.reactivex.rxjava4.exceptions.TestException;
25+
26+
public class StreamableCollectIntoTest extends StreamableBaseTest {
27+
28+
@Test
29+
public void multipleItems() throws Throwable {
30+
Streamable.range(1, 5)
31+
.collectInto(new ArrayList<Integer>(), (list, item) -> list.add(item))
32+
.test()
33+
.awaitDone(5, TimeUnit.SECONDS)
34+
.assertResult(new ArrayList<>(List.of(1, 2, 3, 4, 5)));
35+
}
36+
37+
@Test
38+
public void empty() throws Throwable {
39+
Streamable.<Integer>empty()
40+
.collectInto(new ArrayList<Integer>(), (list, item) -> list.add(item))
41+
.test()
42+
.awaitDone(5, TimeUnit.SECONDS)
43+
.assertResult(new ArrayList<Integer>());
44+
}
45+
46+
@Test
47+
public void error() throws Throwable {
48+
Streamable.<Integer>error(new TestException("error"))
49+
.collectInto(new ArrayList<Integer>(), (list, item) -> list.add(item))
50+
.test()
51+
.awaitDone(5, TimeUnit.SECONDS)
52+
.assertFailure(TestException.class);
53+
}
54+
55+
@Test
56+
public void nullArguments() {
57+
assertThrows(NullPointerException.class, () -> Streamable.<Integer>empty().collectInto((ArrayList<Integer>) null, (list, item) -> list.add(item)));
58+
assertThrows(NullPointerException.class, () -> Streamable.empty().collectInto(new ArrayList<Integer>(), null));
59+
}
60+
}

0 commit comments

Comments
 (0)