samples/instanceofclass/src-api2.0/api/InstanceProvider.java
changeset 210 acf2c31e22d4
parent 209 1c999569643b
     1.1 --- a/samples/instanceofclass/src-api2.0/api/InstanceProvider.java	Sat Jun 14 10:04:51 2008 +0200
     1.2 +++ b/samples/instanceofclass/src-api2.0/api/InstanceProvider.java	Sat Jun 14 10:04:53 2008 +0200
     1.3 @@ -2,24 +2,50 @@
     1.4  package api;
     1.5  
     1.6  // BEGIN: instanceof.class.InstanceProvider2
     1.7 +
     1.8 +import java.util.Arrays;
     1.9 +import java.util.HashSet;
    1.10 +import java.util.Set;
    1.11 +import java.util.concurrent.Callable;
    1.12 +
    1.13  public final class InstanceProvider {
    1.14 -    private final Object instance;
    1.15 +    private final Callable<Object> instance;
    1.16 +    private final Set<String> types;
    1.17  
    1.18 -    public InstanceProvider(Object instance) {
    1.19 +    public InstanceProvider(Callable<Object> instance) {
    1.20          this.instance = instance;
    1.21 +        this.types = null;
    1.22 +    }
    1.23 +    /** Specifies not only a factory for creating objects, but
    1.24 +     * also additional information about them.
    1.25 +     * @param instance the factory to create the object
    1.26 +     * @param type the class that the create object will be instance of
    1.27 +     * @since 2.0 
    1.28 +     */
    1.29 +    public InstanceProvider(Callable<Object> instance, String... types) {
    1.30 +        this.instance = instance;
    1.31 +        this.types = new HashSet<String>();
    1.32 +        this.types.addAll(Arrays.asList(types));
    1.33      }
    1.34      
    1.35 -    public Class<?> instanceClass() {
    1.36 -        return instance.getClass();
    1.37 +    public Class<?> instanceClass() throws Exception {
    1.38 +        return instance.call().getClass();
    1.39      }
    1.40 -    public Object instanceCreate() {
    1.41 -        return instance;
    1.42 +    public Object instanceCreate() throws Exception {
    1.43 +        return instance.call();
    1.44      }
    1.45      
    1.46 -    /** @since 2.0 */
    1.47 -    public boolean isInstanceOf(Class<?> c) {
    1.48 -        if (Helper.knownHowToDoItBetter()) {
    1.49 -            return Helper.computeTheResultOfIsInstanceOfInSomeBetterWay(c);
    1.50 +    /** Allows to find out if the InstanceProvider creates object of given
    1.51 +     * type. This check can be done without loading the actual object or
    1.52 +     * its implementation class into memory.
    1.53 +     * 
    1.54 +     * @param c class to test 
    1.55 +     * @return if the instances produced by this provider is instance of c
    1.56 +     * @since 2.0 
    1.57 +     */
    1.58 +    public boolean isInstanceOf(Class<?> c) throws Exception {
    1.59 +        if (types != null) {
    1.60 +            return types.contains(c.getName());
    1.61          } else {
    1.62              // fallback
    1.63              return c.isAssignableFrom(instanceClass());
    1.64 @@ -29,12 +55,3 @@
    1.65      
    1.66  }
    1.67  // END: instanceof.class.InstanceProvider2
    1.68 -
    1.69 -class Helper {
    1.70 -    static boolean computeTheResultOfIsInstanceOfInSomeBetterWay(Class<?> c) {
    1.71 -        return false;
    1.72 -    }
    1.73 -    static boolean knownHowToDoItBetter() {
    1.74 -        return false;
    1.75 -    }
    1.76 -}
    1.77 \ No newline at end of file