|
19 | 19 | import java.util.Collections;
|
20 | 20 | import java.util.List;
|
21 | 21 | import java.util.Set;
|
| 22 | +import java.util.function.BiConsumer; |
| 23 | +import java.util.function.BinaryOperator; |
22 | 24 | import java.util.function.Function;
|
23 | 25 | import java.util.function.Predicate;
|
24 | 26 | import java.util.function.Supplier;
|
| 27 | +import java.util.stream.Collector; |
| 28 | +import java.util.stream.Collectors; |
25 | 29 | import java.util.stream.Stream;
|
26 | 30 | import java.util.stream.StreamSupport;
|
27 | 31 |
|
@@ -216,4 +220,33 @@ default Set<T> toSet() {
|
216 | 220 | default Stream<T> get() {
|
217 | 221 | return stream();
|
218 | 222 | }
|
| 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 | + } |
219 | 252 | }
|
0 commit comments