Shelf(int nProducts) { Suppliers.fill(this, Product.generator, nProducts); } }
FilledList(Supplier<T> gen, int size) { Suppliers.fill(this, gen, size); } public FilledList(T t, int size) {
Foo2(Supplier<T> factory) { Suppliers.fill(x, factory, 5); } @Override
public static void main(String[] args) { // Demonstrate create(): RandomList<Teller> tellers = Suppliers.create( RandomList::new, Teller::new, 4); // Demonstrate fill(): List<Customer> customers = Suppliers.fill( new ArrayList<>(), Customer::new, 12); customers.forEach(c -> serve(tellers.select(), c)); // Demonstrate assisted latent typing: Bank bank = Suppliers.fill( new Bank(), Bank::put, BankTeller::new, 3); // Can also use second version of fill(): List<Customer> customers2 = Suppliers.fill( new ArrayList<>(), List::add, Customer::new, 12); } }
public static void main(String[] args) { // Suppliers class from the Generics chapter: Set<String> set = Suppliers.create( LinkedHashSet::new, new Government(), 15); System.out.println(set); List<String> list = Suppliers.create( LinkedList::new, new Government(), 15); System.out.println(list); list = new ArrayList<>(); Suppliers.fill(list, new Government(), 15); System.out.println(list); // Or we can use Streams: set = Arrays.stream(Government.foundation) .collect(Collectors.toSet()); System.out.println(set); list = Arrays.stream(Government.foundation) .collect(Collectors.toList()); System.out.println(list); list = Arrays.stream(Government.foundation) .collect(Collectors .toCollection(LinkedList::new)); System.out.println(list); set = Arrays.stream(Government.foundation) .collect(Collectors .toCollection(LinkedHashSet::new)); System.out.println(set); } }
public static void main(String[] args) { Stream.of( Stream.generate(Shape::new).limit(2), Stream.generate(Square::new).limit(2)) .flatMap(c -> c) // flatten into one stream .peek(Shape::rotate) .forEach(s -> s.resize(7)); new FilledList<>(Shape::new, 2) .forEach(Shape::rotate); new FilledList<>(Square::new, 2) .forEach(Shape::rotate); SimpleQueue<Shape> shapeQ = Suppliers.fill( new SimpleQueue<>(), SimpleQueue::add, Shape::new, 2); Suppliers.fill(shapeQ, SimpleQueue::add, Square::new, 2); shapeQ.forEach(Shape::rotate); } }
public static void main(String[] args) throws Exception { List<Shape> shapes = Suppliers.create(ArrayList::new, Shape::new, 3); Apply.apply(shapes, Shape.class.getMethod("rotate")); Apply.apply(shapes, Shape.class.getMethod("resize", int.class), 7); List<Square> squares = Suppliers.create(ArrayList::new, Square::new, 3); Apply.apply(squares, Shape.class.getMethod("rotate")); Apply.apply(squares, Shape.class.getMethod("resize", int.class), 7); Apply.apply(new FilledList<>(Shape::new, 3), Shape.class.getMethod("rotate")); Apply.apply(new FilledList<>(Square::new, 3), Shape.class.getMethod("rotate")); SimpleQueue<Shape> shapeQ = Suppliers.fill( new SimpleQueue<>(), SimpleQueue::add, Shape::new, 3); Suppliers.fill(shapeQ, SimpleQueue::add, Square::new, 3); Apply.apply(shapeQ, Shape.class.getMethod("rotate")); } }