// onjava/BasicSupplier.java // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. // Automatically create a Supplier, given a class // with a default (no-arg) constructor. package onjava; import java.util.function.*; public class BasicSupplier implements Supplier { private Class type; public BasicSupplier(Class type){ this.type = type; } @Override public T get() { try { // Assumes type is a public class: return type.newInstance(); } catch(InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } // Produce a Default generator given a type token: public static Supplier create(Class type) { return new BasicSupplier<>(type); } }