jtulach@75: jtulach@75: package api; jtulach@75: jtulach@75: // BEGIN: instanceof.class.InstanceProvider2 jtulach@210: jtulach@210: import java.util.Arrays; jtulach@210: import java.util.HashSet; jtulach@210: import java.util.Set; jtulach@210: import java.util.concurrent.Callable; jtulach@210: jtulach@209: public final class InstanceProvider { jtulach@210: private final Callable instance; jtulach@210: private final Set types; jtulach@191: jtulach@210: public InstanceProvider(Callable instance) { jtulach@75: this.instance = instance; jtulach@210: this.types = null; jtulach@210: } jtulach@210: /** Specifies not only a factory for creating objects, but jtulach@210: * also additional information about them. jtulach@210: * @param instance the factory to create the object jtulach@210: * @param type the class that the create object will be instance of jtulach@210: * @since 2.0 jtulach@210: */ jtulach@210: public InstanceProvider(Callable instance, String... types) { jtulach@210: this.instance = instance; jtulach@210: this.types = new HashSet(); jtulach@210: this.types.addAll(Arrays.asList(types)); jtulach@75: } jtulach@75: jtulach@210: public Class instanceClass() throws Exception { jtulach@210: return instance.call().getClass(); jtulach@75: } jtulach@210: public Object instanceCreate() throws Exception { jtulach@210: return instance.call(); jtulach@75: } jtulach@75: jtulach@210: /** Allows to find out if the InstanceProvider creates object of given jtulach@210: * type. This check can be done without loading the actual object or jtulach@210: * its implementation class into memory. jtulach@210: * jtulach@210: * @param c class to test jtulach@210: * @return if the instances produced by this provider is instance of c jtulach@210: * @since 2.0 jtulach@210: */ jtulach@210: public boolean isInstanceOf(Class c) throws Exception { jtulach@210: if (types != null) { jtulach@210: return types.contains(c.getName()); jtulach@75: } else { jtulach@75: // fallback jtulach@75: return c.isAssignableFrom(instanceClass()); jtulach@75: } jtulach@75: } jtulach@75: jtulach@75: jtulach@75: } jtulach@75: // END: instanceof.class.InstanceProvider2