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