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:
Oliver Drotbohm
2018-12-12 10:36:40 +01:00
parent daaa16f195
commit 97390a1eef
2 changed files with 46 additions and 0 deletions

View File

@@ -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);
}
}