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).
This commit is contained in:
@@ -19,9 +19,13 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
@@ -216,4 +220,33 @@ public interface Streamable<T> extends Iterable<T>, Supplier<Stream<T>> {
|
||||
default Stream<T> get() {
|
||||
return stream();
|
||||
}
|
||||
|
||||
/**
|
||||
* A collector to easily produce a {@link Streamable} from a {@link Stream} using {@link Collectors#toList} as
|
||||
* intermediate collector.
|
||||
*
|
||||
* @return
|
||||
* @see #toStreamable(Collector)
|
||||
* @since 2.2
|
||||
*/
|
||||
public static <S> Collector<S, ?, Streamable<S>> toStreamable() {
|
||||
return toStreamable(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* A collector to easily produce a {@link Streamable} from a {@link Stream} and the given intermediate collector.
|
||||
*
|
||||
* @return
|
||||
* @since 2.2
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <S, T extends Iterable<S>> Collector<S, ?, Streamable<S>> toStreamable(
|
||||
Collector<S, ?, T> intermediate) {
|
||||
|
||||
return Collector.of( //
|
||||
(Supplier<T>) intermediate.supplier(), //
|
||||
(BiConsumer<T, S>) intermediate.accumulator(), //
|
||||
(BinaryOperator<T>) intermediate.combiner(), //
|
||||
Streamable::of);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.data.util;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -53,4 +54,16 @@ public class StreamableUnitTests {
|
||||
public void concatenatesStreamable() {
|
||||
assertThat(Streamable.of(1, 2).and(Streamable.of(3, 4))).containsExactly(1, 2, 3, 4);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1447
|
||||
public void usesStreamableCollectors() {
|
||||
|
||||
assertThat(Streamable.of(1, 2).stream() //
|
||||
.collect(Streamable.toStreamable())) //
|
||||
.containsExactly(1, 2);
|
||||
|
||||
assertThat(Streamable.of(1, 2, 2).stream() //
|
||||
.collect(Streamable.toStreamable(Collectors.toSet()))) //
|
||||
.containsExactlyInAnyOrder(1, 2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user