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