Jesse wanted to have better example of InstanceProvider. Done, but it is also more complicated: http://wiki.apidesign.org/index.php/A_Method_Addition_Lover's_Heaven
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:03:34 +0200
changeset 191c8a7c6621b5f
parent 190 8b87a7d88b31
child 192 78bf0774975d
Jesse wanted to have better example of InstanceProvider. Done, but it is also more complicated: http://wiki.apidesign.org/index.php/A_Method_Addition_Lover's_Heaven
samples/instanceof/src/api/InstanceProvider.java
samples/instanceofclass/src-api1.0/api/InstanceProvider.java
samples/instanceofclass/src-api2.0/api/InstanceProvider.java
     1.1 --- a/samples/instanceof/src/api/InstanceProvider.java	Sat Jun 14 10:03:27 2008 +0200
     1.2 +++ b/samples/instanceof/src/api/InstanceProvider.java	Sat Jun 14 10:03:34 2008 +0200
     1.3 @@ -3,7 +3,7 @@
     1.4  
     1.5  // BEGIN: instanceof.InstanceProvider
     1.6  public interface InstanceProvider {
     1.7 -    public Class<?> instanceClass();
     1.8 -    public Object instanceCreate();
     1.9 +    public Class<?> instanceClass() throws Exception;
    1.10 +    public Object instanceCreate() throws Exception;
    1.11  }
    1.12  // END: instanceof.InstanceProvider
     2.1 --- a/samples/instanceofclass/src-api1.0/api/InstanceProvider.java	Sat Jun 14 10:03:27 2008 +0200
     2.2 +++ b/samples/instanceofclass/src-api1.0/api/InstanceProvider.java	Sat Jun 14 10:03:34 2008 +0200
     2.3 @@ -2,18 +2,21 @@
     2.4  package api;
     2.5  
     2.6  // BEGIN: instanceof.class.InstanceProvider1
     2.7 +
     2.8 +import java.util.concurrent.Callable;
     2.9 +
    2.10  public final class InstanceProvider {
    2.11 -    private final Object instance;
    2.12 +    private final Callable<Object> instance;
    2.13  
    2.14 -    public InstanceProvider(Object instance) {
    2.15 +    public InstanceProvider(Callable<Object> instance) {
    2.16          this.instance = instance;
    2.17      }
    2.18      
    2.19 -    public Class<?> instanceClass() {
    2.20 -        return instance.getClass();
    2.21 +    public Class<?> instanceClass() throws Exception {
    2.22 +        return instance.call().getClass();
    2.23      }
    2.24 -    public Object instanceCreate() {
    2.25 -        return instance;
    2.26 +    public Object instanceCreate() throws Exception {
    2.27 +        return instance.call();
    2.28      }
    2.29  }
    2.30  // END: instanceof.class.InstanceProvider1
     3.1 --- a/samples/instanceofclass/src-api2.0/api/InstanceProvider.java	Sat Jun 14 10:03:27 2008 +0200
     3.2 +++ b/samples/instanceofclass/src-api2.0/api/InstanceProvider.java	Sat Jun 14 10:03:34 2008 +0200
     3.3 @@ -2,24 +2,50 @@
     3.4  package api;
     3.5  
     3.6  // BEGIN: instanceof.class.InstanceProvider2
     3.7 +
     3.8 +import java.util.Arrays;
     3.9 +import java.util.HashSet;
    3.10 +import java.util.Set;
    3.11 +import java.util.concurrent.Callable;
    3.12 +
    3.13  public final class InstanceProvider {
    3.14 -    private final Object instance;
    3.15 +    private final Callable<Object> instance;
    3.16 +    private final Set<String> types;
    3.17  
    3.18 -    public InstanceProvider(Object instance) {
    3.19 +    public InstanceProvider(Callable<Object> instance) {
    3.20          this.instance = instance;
    3.21 +        this.types = null;
    3.22 +    }
    3.23 +    /** Specifies not only a factory for creating objects, but
    3.24 +     * also additional information about them.
    3.25 +     * @param instance the factory to create the object
    3.26 +     * @param type the class that the create object will be instance of
    3.27 +     * @since 2.0 
    3.28 +     */
    3.29 +    public InstanceProvider(Callable<Object> instance, String... types) {
    3.30 +        this.instance = instance;
    3.31 +        this.types = new HashSet<String>();
    3.32 +        this.types.addAll(Arrays.asList(types));
    3.33      }
    3.34      
    3.35 -    public Class<?> instanceClass() {
    3.36 -        return instance.getClass();
    3.37 +    public Class<?> instanceClass() throws Exception {
    3.38 +        return instance.call().getClass();
    3.39      }
    3.40 -    public Object instanceCreate() {
    3.41 -        return instance;
    3.42 +    public Object instanceCreate() throws Exception {
    3.43 +        return instance.call();
    3.44      }
    3.45      
    3.46 -    /** @since 2.0 */
    3.47 -    public boolean isInstanceOf(Class<?> c) {
    3.48 -        if (Helper.knownHowToDoItBetter()) {
    3.49 -            return Helper.computeTheResultOfIsInstanceOfInSomeBetterWay(c);
    3.50 +    /** Allows to find out if the InstanceProvider creates object of given
    3.51 +     * type. This check can be done without loading the actual object or
    3.52 +     * its implementation class into memory.
    3.53 +     * 
    3.54 +     * @param c class to test 
    3.55 +     * @return if the instances produced by this provider is instance of c
    3.56 +     * @since 2.0 
    3.57 +     */
    3.58 +    public boolean isInstanceOf(Class<?> c) throws Exception {
    3.59 +        if (types != null) {
    3.60 +            return types.contains(c.getName());
    3.61          } else {
    3.62              // fallback
    3.63              return c.isAssignableFrom(instanceClass());
    3.64 @@ -29,12 +55,3 @@
    3.65      
    3.66  }
    3.67  // END: instanceof.class.InstanceProvider2
    3.68 -
    3.69 -class Helper {
    3.70 -    static boolean computeTheResultOfIsInstanceOfInSomeBetterWay(Class<?> c) {
    3.71 -        return false;
    3.72 -    }
    3.73 -    static boolean knownHowToDoItBetter() {
    3.74 -        return false;
    3.75 -    }
    3.76 -}
    3.77 \ No newline at end of file