| 9 | import java.util.stream.*; |
| 10 | |
| 11 | public class Suppliers { |
| 12 | // Create a collection and fill it: |
| 13 | public static <T, C extends Collection<T>> C |
| 14 | create(Supplier<C> factory, Supplier<T> gen, int n) { |
| 15 | return Stream.generate(gen) |
| 16 | .limit(n) |
| 17 | .collect(factory, C::add, C::addAll); |
| 18 | } |
| 19 | // Fill an existing collection: |
| 20 | public static <T, C extends Collection<T>> |
| 21 | C fill(C coll, Supplier<T> gen, int n) { |
| 22 | Stream.generate(gen) |
| 23 | .limit(n) |
| 24 | .forEach(coll::add); |
| 25 | return coll; |
| 26 | } |
| 27 | // Use an unbound method reference to |
| 28 | // produce a more general method: |
| 29 | public static <H, A> H fill(H holder, |
| 30 | BiConsumer<H, A> adder, Supplier<A> gen, int n) { |
| 31 | Stream.generate(gen) |
| 32 | .limit(n) |
| 33 | .forEach(a -> adder.accept(holder, a)); |
| 34 | return holder; |
| 35 | } |
| 36 | } |
nothing calls this directly
no outgoing calls
no test coverage detected