4 // BEGIN: instanceof.class.InstanceProvider2
6 import java.util.Arrays;
7 import java.util.HashSet;
9 import java.util.concurrent.Callable;
11 public final class InstanceProvider {
12 private final Callable<Object> instance;
13 private final Set<String> types;
15 public InstanceProvider(Callable<Object> instance) {
16 this.instance = instance;
19 /** Specifies not only a factory for creating objects, but
20 * also additional information about them.
21 * @param instance the factory to create the object
22 * @param type the class that the create object will be instance of
25 public InstanceProvider(Callable<Object> instance, String... types) {
26 this.instance = instance;
27 this.types = new HashSet<String>();
28 this.types.addAll(Arrays.asList(types));
31 public Class<?> instanceClass() throws Exception {
32 return instance.call().getClass();
34 public Object instanceCreate() throws Exception {
35 return instance.call();
38 /** Allows to find out if the InstanceProvider creates object of given
39 * type. This check can be done without loading the actual object or
40 * its implementation class into memory.
42 * @param c class to test
43 * @return if the instances produced by this provider is instance of c
46 public boolean isInstanceOf(Class<?> c) throws Exception {
48 return types.contains(c.getName());
51 return c.isAssignableFrom(instanceClass());
57 // END: instanceof.class.InstanceProvider2