diff -r 1c999569643b -r acf2c31e22d4 samples/instanceofclass/src-api2.0/api/InstanceProvider.java --- a/samples/instanceofclass/src-api2.0/api/InstanceProvider.java Sat Jun 14 10:04:51 2008 +0200 +++ b/samples/instanceofclass/src-api2.0/api/InstanceProvider.java Sat Jun 14 10:04:53 2008 +0200 @@ -2,24 +2,50 @@ package api; // BEGIN: instanceof.class.InstanceProvider2 + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.Callable; + public final class InstanceProvider { - private final Object instance; + private final Callable instance; + private final Set types; - public InstanceProvider(Object instance) { + public InstanceProvider(Callable instance) { this.instance = instance; + this.types = null; + } + /** Specifies not only a factory for creating objects, but + * also additional information about them. + * @param instance the factory to create the object + * @param type the class that the create object will be instance of + * @since 2.0 + */ + public InstanceProvider(Callable instance, String... types) { + this.instance = instance; + this.types = new HashSet(); + this.types.addAll(Arrays.asList(types)); } - public Class instanceClass() { - return instance.getClass(); + public Class instanceClass() throws Exception { + return instance.call().getClass(); } - public Object instanceCreate() { - return instance; + public Object instanceCreate() throws Exception { + return instance.call(); } - /** @since 2.0 */ - public boolean isInstanceOf(Class c) { - if (Helper.knownHowToDoItBetter()) { - return Helper.computeTheResultOfIsInstanceOfInSomeBetterWay(c); + /** Allows to find out if the InstanceProvider creates object of given + * type. This check can be done without loading the actual object or + * its implementation class into memory. + * + * @param c class to test + * @return if the instances produced by this provider is instance of c + * @since 2.0 + */ + public boolean isInstanceOf(Class c) throws Exception { + if (types != null) { + return types.contains(c.getName()); } else { // fallback return c.isAssignableFrom(instanceClass()); @@ -29,12 +55,3 @@ } // END: instanceof.class.InstanceProvider2 - -class Helper { - static boolean computeTheResultOfIsInstanceOfInSomeBetterWay(Class c) { - return false; - } - static boolean knownHowToDoItBetter() { - return false; - } -} \ No newline at end of file