Skip to content

Commit 97390a1

Browse files
committed
DATACMNS-1447 - Streamable now exposes Collectors.
Added Streamable.toStreamable() and ….toStreamable(Collector) to allow the creation of a Streamable from streams wither using a default List-based intermediate collector (former) or providing an explicit one (latter).
1 parent daaa16f commit 97390a1

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/main/java/org/springframework/data/util/Streamable.java

+33
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121
import java.util.Set;
22+
import java.util.function.BiConsumer;
23+
import java.util.function.BinaryOperator;
2224
import java.util.function.Function;
2325
import java.util.function.Predicate;
2426
import java.util.function.Supplier;
27+
import java.util.stream.Collector;
28+
import java.util.stream.Collectors;
2529
import java.util.stream.Stream;
2630
import java.util.stream.StreamSupport;
2731

@@ -216,4 +220,33 @@ default Set<T> toSet() {
216220
default Stream<T> get() {
217221
return stream();
218222
}
223+
224+
/**
225+
* A collector to easily produce a {@link Streamable} from a {@link Stream} using {@link Collectors#toList} as
226+
* intermediate collector.
227+
*
228+
* @return
229+
* @see #toStreamable(Collector)
230+
* @since 2.2
231+
*/
232+
public static <S> Collector<S, ?, Streamable<S>> toStreamable() {
233+
return toStreamable(Collectors.toList());
234+
}
235+
236+
/**
237+
* A collector to easily produce a {@link Streamable} from a {@link Stream} and the given intermediate collector.
238+
*
239+
* @return
240+
* @since 2.2
241+
*/
242+
@SuppressWarnings("unchecked")
243+
public static <S, T extends Iterable<S>> Collector<S, ?, Streamable<S>> toStreamable(
244+
Collector<S, ?, T> intermediate) {
245+
246+
return Collector.of( //
247+
(Supplier<T>) intermediate.supplier(), //
248+
(BiConsumer<T, S>) intermediate.accumulator(), //
249+
(BinaryOperator<T>) intermediate.combiner(), //
250+
Streamable::of);
251+
}
219252
}

src/test/java/org/springframework/data/util/StreamableUnitTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import java.util.Arrays;
21+
import java.util.stream.Collectors;
2122
import java.util.stream.Stream;
2223

2324
import org.junit.Test;
@@ -53,4 +54,16 @@ public void concatenatesVarargs() {
5354
public void concatenatesStreamable() {
5455
assertThat(Streamable.of(1, 2).and(Streamable.of(3, 4))).containsExactly(1, 2, 3, 4);
5556
}
57+
58+
@Test // DATACMNS-1447
59+
public void usesStreamableCollectors() {
60+
61+
assertThat(Streamable.of(1, 2).stream() //
62+
.collect(Streamable.toStreamable())) //
63+
.containsExactly(1, 2);
64+
65+
assertThat(Streamable.of(1, 2, 2).stream() //
66+
.collect(Streamable.toStreamable(Collectors.toSet()))) //
67+
.containsExactlyInAnyOrder(1, 2);
68+
}
5669
}

0 commit comments

Comments
 (0)