Renamed ContextualFactory to Generator. Disabed users from passing in Factory implementations.

git-svn-id: https://google-guice.googlecode.com/svn/trunk@163 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/src/com/google/inject/ContainerBuilder.java b/src/com/google/inject/ContainerBuilder.java
index 0f3b13a..dd37ae3 100644
--- a/src/com/google/inject/ContainerBuilder.java
+++ b/src/com/google/inject/ContainerBuilder.java
@@ -571,21 +571,11 @@
     }
 
     /**
-     * Binds to instances from the given factory.
+     * Binds to instances generated by the given generator.
      */
-    public BindingBuilder<T> to(
-        final ContextualFactory<? extends T> factory) {
+    public BindingBuilder<T> to(Generator<? extends T> generator) {
       ensureImplementationIsNotSet();
-      this.factory = new InternalToContextualFactoryAdapter<T>(factory);
-      return this;
-    }
-
-    /**
-     * Binds to instances from the given factory.
-     */
-    public BindingBuilder<T> to(final Factory<? extends T> factory) {
-      ensureImplementationIsNotSet();
-      this.factory = new InternalFactoryToFactoryAdapter<T>(factory);
+      this.factory = new InternalFactoryToGeneratorAdapter<T>(generator);
       return this;
     }
 
@@ -613,29 +603,30 @@
     }
 
     /**
-     * Binds to instances from the given factory.
+     * Binds to instances from the generator bound to the given type.
      */
-    public BindingBuilder<T> toFactory(
-        final Class<? extends Factory<T>> factoryClass) {
-      ensureImplementationIsNotSet();
-
-      final DelegatingFactory<T> delegatingFactory =
-          new DelegatingFactory<T>(factoryClass, errorHandler);
-      creationListeners.add(delegatingFactory);
-      this.factory = delegatingFactory;
-
-      return this;
+    public BindingBuilder<T> toGenerator(
+        final Class<? extends Generator<T>> generatorType) {
+      return toGenerator(Key.get(generatorType));
     }
 
     /**
-     * Binds to instances from the given factory.
+     * Binds to instances from the generator bound to the given type.
      */
-    public BindingBuilder<T> toContextualFactory(
-        final Class<? extends ContextualFactory<T>> factoryClass) {
+    public BindingBuilder<T> toGenerator(
+        final TypeLiteral<? extends Generator<T>> generatorType) {
+      return toGenerator(Key.get(generatorType));
+    }
+
+    /**
+     * Binds to instances from the given generator bound to the given key.
+     */
+    public BindingBuilder<T> toGenerator(
+        final Key<? extends Generator<T>> generatorKey) {
       ensureImplementationIsNotSet();
 
-      final ContextualDelegatingFactory<T> delegatingFactory =
-          new ContextualDelegatingFactory<T>(factoryClass, errorHandler);
+      final BoundGenerator<T> delegatingFactory =
+          new BoundGenerator<T>(generatorKey, errorHandler);
       creationListeners.add(delegatingFactory);
       this.factory = delegatingFactory;
 
@@ -732,69 +723,40 @@
     }
   }
 
-  private static class DelegatingFactorySupport<F> implements CreationListener {
+  /**
+   * Delegates to a custom generator which is also bound in the container.
+   */
+  private static class BoundGenerator<T>
+      implements InternalFactory<T>, CreationListener {
 
-    final Class<? extends F> factoryClass;
+    final Key<? extends Generator<? extends T>> generatorKey;
     final ErrorHandler errorHandler;
-    private InternalFactory<? extends F> factoryFactory;
+    private InternalFactory<? extends Generator<? extends T>> generatorFactory;
 
-    public DelegatingFactorySupport(Class<? extends F> factoryClass,
+    public BoundGenerator(
+        Key<? extends Generator<? extends T>> generatorKey,
         ErrorHandler errorHandler) {
-      this.factoryClass = factoryClass;
+      this.generatorKey = generatorKey;
       this.errorHandler = errorHandler;
     }
 
     public void notify(final ContainerImpl container) {
       container.withErrorHandler(errorHandler, new Runnable() {
         public void run() {
-          factoryFactory = container.getInternalFactory(
-              null, Key.get(factoryClass));
+          generatorFactory = container.getInternalFactory(
+              null, generatorKey);
         }
       });
     }
 
-    protected InternalFactory<? extends F> getFactoryFactory() {
-      return factoryFactory;
-    }
-
     public String toString() {
-      return factoryClass.getName();
-    }
-  }
-
-  /**
-   * Delegates to a custom factory which is also bound in the container.
-   */
-  private static class DelegatingFactory<T>
-      extends DelegatingFactorySupport<Factory<? extends T>>
-      implements InternalFactory<T> {
-
-    public DelegatingFactory(
-        Class<? extends Factory<? extends T>> factoryClass,
-        ErrorHandler errorHandler) {
-      super(factoryClass, errorHandler);
+      return generatorKey.toString();
     }
 
     public T get(InternalContext context) {
-      return getFactoryFactory().get(context).get();
-    }
-  }
-
-  /**
-   * Delegates to a custom factory which is also bound in the container.
-   */
-  private static class ContextualDelegatingFactory<T>
-      extends DelegatingFactorySupport<ContextualFactory<? extends T>>
-      implements InternalFactory<T> {
-
-    public ContextualDelegatingFactory(
-        Class<? extends ContextualFactory<? extends T>> factoryClass,
-        ErrorHandler errorHandler) {
-      super(factoryClass, errorHandler);
-    }
-
-    public T get(InternalContext context) {
-      return getFactoryFactory().get(context).get(context.getExternalContext());
+      return generatorFactory
+          .get(context)
+          .generate(context.getExternalContext());
     }
   }
 
diff --git a/src/com/google/inject/ContextualFactory.java b/src/com/google/inject/Generator.java
similarity index 75%
rename from src/com/google/inject/ContextualFactory.java
rename to src/com/google/inject/Generator.java
index 2664b68..cfd3623 100644
--- a/src/com/google/inject/ContextualFactory.java
+++ b/src/com/google/inject/Generator.java
@@ -17,16 +17,16 @@
 package com.google.inject;
 
 /**
- * Gets instances of {@code T}.
+ * Generates instances of {@code T} for a binding.
  *
  * @author crazybob@google.com (Bob Lee)
  */
-public interface ContextualFactory<T> {
+public interface Generator<T> {
 
   /**
-   * Gets an instance of {@code T}.
+   * Generates an instance of {@code T} given the context of the injection.
    *
-   * @param context of this call
+   * @param context of this injection
    */
-  T get(Context context);
+  T generate(Context context);
 }
diff --git a/src/com/google/inject/InternalToContextualFactoryAdapter.java b/src/com/google/inject/InternalFactoryToGeneratorAdapter.java
similarity index 74%
rename from src/com/google/inject/InternalToContextualFactoryAdapter.java
rename to src/com/google/inject/InternalFactoryToGeneratorAdapter.java
index 6256829..afd9bfb 100644
--- a/src/com/google/inject/InternalToContextualFactoryAdapter.java
+++ b/src/com/google/inject/InternalFactoryToGeneratorAdapter.java
@@ -19,17 +19,17 @@
 /**
  * @author crazybob@google.com (Bob Lee)
 */
-class InternalToContextualFactoryAdapter<T> implements InternalFactory<T> {
+class InternalFactoryToGeneratorAdapter<T> implements InternalFactory<T> {
 
-  private final ContextualFactory<? extends T> factory;
+  private final Generator<? extends T> factory;
 
-  public InternalToContextualFactoryAdapter(
-      ContextualFactory<? extends T> factory) {
+  public InternalFactoryToGeneratorAdapter(
+      Generator<? extends T> factory) {
     this.factory = factory;
   }
 
   public T get(InternalContext context) {
-    return factory.get(context.getExternalContext());
+    return factory.generate(context.getExternalContext());
   }
 
   public String toString() {
diff --git a/src/com/google/inject/package-info.java b/src/com/google/inject/package-info.java
index dfed264..198cfda 100644
--- a/src/com/google/inject/package-info.java
+++ b/src/com/google/inject/package-info.java
@@ -39,7 +39,7 @@
  *     to collect these bindings.
  *
  * <dt>{@link com.google.inject.Factory} and
- *     {@link com.google.inject.ContextualFactory}
+ *     {@link com.google.inject.Generator}
  * <dd>The interface you will implement when you need to customize exactly how
  *     Guice creates instances for a particular binding.  These differ only in
  *     whether a {@link com.google.inject.Context} is made available to the
diff --git a/src/com/google/inject/servlet/ServletModule.java b/src/com/google/inject/servlet/ServletModule.java
index ffe8df1..8bf734e 100644
--- a/src/com/google/inject/servlet/ServletModule.java
+++ b/src/com/google/inject/servlet/ServletModule.java
@@ -19,6 +19,8 @@
 import com.google.inject.AbstractModule;
 import com.google.inject.Factory;
 import com.google.inject.TypeLiteral;
+import com.google.inject.Generator;
+import com.google.inject.Context;
 import static com.google.inject.servlet.ServletScopes.REQUEST;
 import static com.google.inject.servlet.ServletScopes.SESSION;
 
@@ -44,9 +46,9 @@
     scope(SessionScoped.class, SESSION);
 
     // Bind request.
-    Factory<HttpServletRequest> requestFactory =
-        new Factory<HttpServletRequest>() {
-          public HttpServletRequest get() {
+    Generator<HttpServletRequest> requestGenerator =
+        new Generator<HttpServletRequest>() {
+          public HttpServletRequest generate(Context context) {
             return GuiceFilter.getRequest();
           }
 
@@ -54,13 +56,13 @@
             return "RequestFactory";
           }
         };
-    bind(HttpServletRequest.class).to(requestFactory);
-    bind(ServletRequest.class).to(requestFactory);
+    bind(HttpServletRequest.class).to(requestGenerator);
+    bind(ServletRequest.class).to(requestGenerator);
 
     // Bind response.
-    Factory<HttpServletResponse> responseFactory =
-        new Factory<HttpServletResponse>() {
-          public HttpServletResponse get() {
+    Generator<HttpServletResponse> responseGenerator =
+        new Generator<HttpServletResponse>() {
+          public HttpServletResponse generate(Context context) {
             return GuiceFilter.getResponse();
           }
 
@@ -68,12 +70,12 @@
             return "ResponseFactory";
           }
         };
-    bind(HttpServletResponse.class).to(responseFactory);
-    bind(ServletResponse.class).to(responseFactory);
+    bind(HttpServletResponse.class).to(responseGenerator);
+    bind(ServletResponse.class).to(responseGenerator);
 
     // Bind session.
-    bind(HttpSession.class).to(new Factory<HttpSession>() {
-      public HttpSession get() {
+    bind(HttpSession.class).to(new Generator<HttpSession>() {
+      public HttpSession generate(Context context) {
         return GuiceFilter.getRequest().getSession();
       }
 
@@ -85,15 +87,15 @@
     // Bind request parameters.
     bind(new TypeLiteral<Map<String, String[]>>() {})
         .annotatedWith(RequestParameters.class)
-        .to(new Factory<Map<String, String[]>>() {
-          @SuppressWarnings({"unchecked"})
-          public Map<String, String[]> get() {
-            return GuiceFilter.getRequest().getParameterMap();
-          }
+        .to(new Generator<Map<String, String[]>>() {
+              @SuppressWarnings({"unchecked"})
+              public Map<String, String[]> generate(Context context) {
+                return GuiceFilter.getRequest().getParameterMap();
+              }
 
-          public String toString() {
-            return "RequestParametersFactory";
-          }
-        });
+              public String toString() {
+                return "RequestParametersFactory";
+              }
+            });
   }
 }
diff --git a/test/com/google/inject/AllTests.java b/test/com/google/inject/AllTests.java
index 9ba37b9..c97fc7b 100644
--- a/test/com/google/inject/AllTests.java
+++ b/test/com/google/inject/AllTests.java
@@ -49,7 +49,7 @@
     suite.addTestSuite(ReflectionTest.class);
     suite.addTestSuite(ScopesTest.class);
     suite.addTestSuite(ImplicitBindingTest.class);
-    suite.addTestSuite(CustomFactoryTest.class);
+    suite.addTestSuite(GeneratorTest.class);
 
     suite.addTestSuite(MatcherTest.class);
     suite.addTestSuite(ProxyFactoryTest.class);
diff --git a/test/com/google/inject/CustomFactoryTest.java b/test/com/google/inject/CustomFactoryTest.java
deleted file mode 100644
index 55cd5a5..0000000
--- a/test/com/google/inject/CustomFactoryTest.java
+++ /dev/null
@@ -1,168 +0,0 @@
-/**
- * Copyright (C) 2006 Google Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.google.inject;
-
-import junit.framework.TestCase;
-
-/**
- * @author crazybob@google.com (Bob Lee)
- */
-public class CustomFactoryTest extends TestCase {
-
-  public void testFooFactory() throws ContainerCreationException {
-    ContainerBuilder cb = new ContainerBuilder();
-    cb.bind(Foo.class).toFactory(FooFactory.class);
-    Container c = cb.create();
-
-    Foo a = c.getInstance(Foo.class);
-    Foo b = c.getInstance(Foo.class);
-
-    assertEquals(0, a.i);
-    assertEquals(0, b.i);
-    assertNotNull(a.bar);
-    assertNotNull(b.bar);
-    assertNotSame(a.bar, b.bar);
-  }
-
-  public void testContainerScopedFooFactory()
-      throws ContainerCreationException {
-    ContainerBuilder cb = new ContainerBuilder();
-    cb.bind(Foo.class).toFactory(ContainerScopedFooFactory.class);
-    Container c = cb.create();
-
-    Foo a = c.getInstance(Foo.class);
-    Foo b = c.getInstance(Foo.class);
-
-    assertEquals(0, a.i);
-    assertEquals(1, b.i);
-    assertNotNull(a.bar);
-    assertNotNull(b.bar);
-    assertSame(a.bar, b.bar);
-  }
-
-  public void testContextualFooFactory() throws ContainerCreationException {
-    ContainerBuilder cb = new ContainerBuilder();
-    cb.bind(Foo.class).toContextualFactory(ContextualFooFactory.class);
-    Container c = cb.create();
-
-    Foo a = c.getInstance(Foo.class);
-    Foo b = c.getInstance(Foo.class);
-
-    assertEquals(0, a.i);
-    assertEquals(0, b.i);
-    assertNotNull(a.bar);
-    assertNotNull(b.bar);
-    assertNotSame(a.bar, b.bar);
-  }
-
-  public void testContextualContainerScopedFooFactory()
-      throws ContainerCreationException {
-    ContainerBuilder cb = new ContainerBuilder();
-    cb.bind(Foo.class).toContextualFactory(ContainerScopedContextualFooFactory.class);
-    Container c = cb.create();
-
-    Foo a = c.getInstance(Foo.class);
-    Foo b = c.getInstance(Foo.class);
-
-    assertEquals(0, a.i);
-    assertEquals(1, b.i);
-    assertNotNull(a.bar);
-    assertNotNull(b.bar);
-    assertSame(a.bar, b.bar);
-  }
-
-  static class Bar {}
-
-  static class Foo {
-    final Bar bar;
-    final int i;
-
-    Foo(Bar bar, int i) {
-      this.bar = bar;
-      this.i = i;
-    }
-  }
-
-  static class FooFactory implements Factory<Foo> {
-
-    final Bar bar;
-    int count = 0;
-
-    @Inject
-    public FooFactory(Bar bar) {
-      this.bar = bar;
-    }
-
-    public Foo get() {
-      return new Foo(this.bar, count++);
-    }
-  }
-
-  @ContainerScoped
-  static class ContainerScopedFooFactory implements Factory<Foo> {
-
-    final Bar bar;
-    int count = 0;
-
-    @Inject
-    public ContainerScopedFooFactory(Bar bar) {
-      this.bar = bar;
-    }
-
-    public Foo get() {
-      return new Foo(this.bar, count++);
-    }
-  }
-
-  static class ContextualFooFactory implements ContextualFactory<Foo> {
-
-    final Bar bar;
-    int count = 0;
-
-    @Inject
-    public ContextualFooFactory(Bar bar) {
-      this.bar = bar;
-    }
-
-    public Foo get(Context context) {
-      assertNull(context.getMember());
-      assertEquals(-1, context.getParameterIndex());
-      assertEquals(Foo.class, context.getKey().getRawType());
-      return new Foo(this.bar, count++);
-    }
-  }
-
-  @ContainerScoped
-  static class ContainerScopedContextualFooFactory
-      implements ContextualFactory<Foo> {
-
-    final Bar bar;
-    int count = 0;
-
-    @Inject
-    public ContainerScopedContextualFooFactory(Bar bar) {
-      this.bar = bar;
-    }
-
-    public Foo get(Context context) {
-      assertNull(context.getMember());
-      assertEquals(-1, context.getParameterIndex());
-      assertEquals(Foo.class, context.getKey().getRawType());
-      return new Foo(this.bar, count++);
-    }
-  }
-}
diff --git a/test/com/google/inject/FactoryTest.java b/test/com/google/inject/FactoryTest.java
index fa8cd4d..04e3f81 100644
--- a/test/com/google/inject/FactoryTest.java
+++ b/test/com/google/inject/FactoryTest.java
@@ -32,22 +32,22 @@
   public void testParameterIndex() throws ContainerCreationException {
     ContainerBuilder cb = new ContainerBuilder();
 
-    cb.bind(Zero.class).to(new ContextualFactory<Zero>() {
-      public Zero get(Context context) {
+    cb.bind(Zero.class).to(new Generator<Zero>() {
+      public Zero generate(Context context) {
         assertEquals(0, context.getParameterIndex());
         return new Zero();
       }
     });
 
-    cb.bind(One.class).to(new ContextualFactory<One>() {
-      public One get(Context context) {
+    cb.bind(One.class).to(new Generator<One>() {
+      public One generate(Context context) {
         assertEquals(1, context.getParameterIndex());
         return new One();
       }
     });
 
-    cb.bind(NegativeOne.class).to(new ContextualFactory<NegativeOne>() {
-      public NegativeOne get(Context context) {
+    cb.bind(NegativeOne.class).to(new Generator<NegativeOne>() {
+      public NegativeOne generate(Context context) {
         assertEquals(-1, context.getParameterIndex());
         return new NegativeOne();
       }
@@ -141,11 +141,11 @@
     assertNotNull(foo.bar.tee2.bob2);
   }
 
-  <T> ContextualFactory<T> createFactory(
+  <T> Generator<T> createFactory(
       final Class<T> type, final Class<? extends Annotation> annotationType,
       final Member expectedMember) {
-    return new ContextualFactory<T>() {
-      public T get(Context context) {
+    return new Generator<T>() {
+      public T generate(Context context) {
         assertEquals(expectedMember, context.getMember());
         assertEquals(type, context.getKey().getType().getType());
         return context.getContainer().getFactory(type).get();
diff --git a/test/com/google/inject/GeneratorTest.java b/test/com/google/inject/GeneratorTest.java
new file mode 100644
index 0000000..d0b4b09
--- /dev/null
+++ b/test/com/google/inject/GeneratorTest.java
@@ -0,0 +1,105 @@
+/**
+ * Copyright (C) 2006 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.inject;
+
+import junit.framework.TestCase;
+
+/**
+ * @author crazybob@google.com (Bob Lee)
+ */
+public class GeneratorTest extends TestCase {
+
+  public void testFooGenerator() throws ContainerCreationException {
+    ContainerBuilder cb = new ContainerBuilder();
+    cb.bind(Foo.class).toGenerator(FooGenerator.class);
+    Container c = cb.create();
+
+    Foo a = c.getInstance(Foo.class);
+    Foo b = c.getInstance(Foo.class);
+
+    assertEquals(0, a.i);
+    assertEquals(0, b.i);
+    assertNotNull(a.bar);
+    assertNotNull(b.bar);
+    assertNotSame(a.bar, b.bar);
+  }
+
+  public void testContainerScopedFooGenerator()
+      throws ContainerCreationException {
+    ContainerBuilder cb = new ContainerBuilder();
+    cb.bind(Foo.class).toGenerator(ContainerScopedFooGenerator.class);
+    Container c = cb.create();
+
+    Foo a = c.getInstance(Foo.class);
+    Foo b = c.getInstance(Foo.class);
+
+    assertEquals(0, a.i);
+    assertEquals(1, b.i);
+    assertNotNull(a.bar);
+    assertNotNull(b.bar);
+    assertSame(a.bar, b.bar);
+  }
+
+  static class Bar {}
+
+  static class Foo {
+    final Bar bar;
+    final int i;
+
+    Foo(Bar bar, int i) {
+      this.bar = bar;
+      this.i = i;
+    }
+  }
+
+  static class FooGenerator implements Generator<Foo> {
+
+    final Bar bar;
+    int count = 0;
+
+    @Inject
+    public FooGenerator(Bar bar) {
+      this.bar = bar;
+    }
+
+    public Foo generate(Context context) {
+      assertNull(context.getMember());
+      assertEquals(-1, context.getParameterIndex());
+      assertEquals(Foo.class, context.getKey().getRawType());
+      return new Foo(this.bar, count++);
+    }
+  }
+
+  @ContainerScoped
+  static class ContainerScopedFooGenerator implements Generator<Foo> {
+
+    final Bar bar;
+    int count = 0;
+
+    @Inject
+    public ContainerScopedFooGenerator(Bar bar) {
+      this.bar = bar;
+    }
+
+    public Foo generate(Context context) {
+      assertNull(context.getMember());
+      assertEquals(-1, context.getParameterIndex());
+      assertEquals(Foo.class, context.getKey().getRawType());
+      return new Foo(this.bar, count++);
+    }
+  }
+}