samples/instanceofclass/src-api2.0/api/InstanceProvider.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:03:34 +0200
changeset 191 c8a7c6621b5f
parent 75 aa07c59612ac
child 209 1c999569643b
permissions -rw-r--r--
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
jtulach@75
     1
jtulach@75
     2
package api;
jtulach@75
     3
jtulach@75
     4
// BEGIN: instanceof.class.InstanceProvider2
jtulach@191
     5
jtulach@191
     6
import java.util.Arrays;
jtulach@191
     7
import java.util.HashSet;
jtulach@191
     8
import java.util.Set;
jtulach@191
     9
import java.util.concurrent.Callable;
jtulach@191
    10
jtulach@75
    11
public final class InstanceProvider {
jtulach@191
    12
    private final Callable<Object> instance;
jtulach@191
    13
    private final Set<String> types;
jtulach@75
    14
jtulach@191
    15
    public InstanceProvider(Callable<Object> instance) {
jtulach@75
    16
        this.instance = instance;
jtulach@191
    17
        this.types = null;
jtulach@191
    18
    }
jtulach@191
    19
    /** Specifies not only a factory for creating objects, but
jtulach@191
    20
     * also additional information about them.
jtulach@191
    21
     * @param instance the factory to create the object
jtulach@191
    22
     * @param type the class that the create object will be instance of
jtulach@191
    23
     * @since 2.0 
jtulach@191
    24
     */
jtulach@191
    25
    public InstanceProvider(Callable<Object> instance, String... types) {
jtulach@191
    26
        this.instance = instance;
jtulach@191
    27
        this.types = new HashSet<String>();
jtulach@191
    28
        this.types.addAll(Arrays.asList(types));
jtulach@75
    29
    }
jtulach@75
    30
    
jtulach@191
    31
    public Class<?> instanceClass() throws Exception {
jtulach@191
    32
        return instance.call().getClass();
jtulach@75
    33
    }
jtulach@191
    34
    public Object instanceCreate() throws Exception {
jtulach@191
    35
        return instance.call();
jtulach@75
    36
    }
jtulach@75
    37
    
jtulach@191
    38
    /** Allows to find out if the InstanceProvider creates object of given
jtulach@191
    39
     * type. This check can be done without loading the actual object or
jtulach@191
    40
     * its implementation class into memory.
jtulach@191
    41
     * 
jtulach@191
    42
     * @param c class to test 
jtulach@191
    43
     * @return if the instances produced by this provider is instance of c
jtulach@191
    44
     * @since 2.0 
jtulach@191
    45
     */
jtulach@191
    46
    public boolean isInstanceOf(Class<?> c) throws Exception {
jtulach@191
    47
        if (types != null) {
jtulach@191
    48
            return types.contains(c.getName());
jtulach@75
    49
        } else {
jtulach@75
    50
            // fallback
jtulach@75
    51
            return c.isAssignableFrom(instanceClass());
jtulach@75
    52
        }
jtulach@75
    53
    }
jtulach@75
    54
jtulach@75
    55
    
jtulach@75
    56
}
jtulach@75
    57
// END: instanceof.class.InstanceProvider2