Renamed performance test. Introduced AbstractModule to support more concise configuration code.
git-svn-id: https://google-guice.googlecode.com/svn/trunk@35 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/guice.iws b/guice.iws
index aa6a44c..e90353a 100644
--- a/guice.iws
+++ b/guice.iws
@@ -21,7 +21,7 @@
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/test/com/google/inject/NotRequiredTest.java" afterPath="$PROJECT_DIR$/test/com/google/inject/NotRequiredTest.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/test/com/google/inject/SuperclassTest.java" afterPath="$PROJECT_DIR$/test/com/google/inject/SuperclassTest.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/src/com/google/inject/Key.java" afterPath="$PROJECT_DIR$/src/com/google/inject/Key.java" />
- <change type="MODIFICATION" beforePath="$PROJECT_DIR$/test/com/google/inject/SpringPerformanceComparison.java" afterPath="$PROJECT_DIR$/test/com/google/inject/SpringPerformanceComparison.java" />
+ <change type="DELETED" beforePath="$PROJECT_DIR$/test/com/google/inject/SpringPerformanceComparison.java" afterPath="" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/test/com/google/inject/GenericInjectionTest.java" afterPath="$PROJECT_DIR$/test/com/google/inject/GenericInjectionTest.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/src/com/google/inject/ContainerImpl.java" afterPath="$PROJECT_DIR$/src/com/google/inject/ContainerImpl.java" />
<change type="DELETED" beforePath="$PROJECT_DIR$/src/com/google/inject/SourceProvider.java" afterPath="" />
@@ -33,7 +33,10 @@
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/test/com/google/inject/FactoryTest.java" afterPath="$PROJECT_DIR$/test/com/google/inject/FactoryTest.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/src/com/google/inject/ContainerBuilder.java" afterPath="$PROJECT_DIR$/src/com/google/inject/ContainerBuilder.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/test/com/google/inject/ContainerTest.java" afterPath="$PROJECT_DIR$/test/com/google/inject/ContainerTest.java" />
+ <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/test/com/google/inject/PerformanceComparison.java" />
+ <change type="MODIFICATION" beforePath="$PROJECT_DIR$/src/com/google/inject/SimpleModule.java" afterPath="$PROJECT_DIR$/src/com/google/inject/SimpleModule.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/test/com/google/inject/CircularDependencyTest.java" afterPath="$PROJECT_DIR$/test/com/google/inject/CircularDependencyTest.java" />
+ <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/src/com/google/inject/AbstractModule.java" />
</list>
</component>
<component name="ChangeListSynchronizer" />
@@ -215,7 +218,7 @@
<file leaf-file-name="ContainerImpl.java" pinned="false" current="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/com/google/inject/ContainerImpl.java">
<provider selected="true" editor-type-id="text-editor">
- <state line="137" column="28" selection-start="4578" selection-end="4578" vertical-scroll-proportion="0.3383356">
+ <state line="671" column="29" selection-start="21743" selection-end="21743" vertical-scroll-proportion="0.3383356">
<folding />
</state>
</provider>
@@ -260,7 +263,16 @@
<file leaf-file-name="ConstantConversionTest.java" pinned="false" current="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/test/com/google/inject/ConstantConversionTest.java">
<provider selected="true" editor-type-id="text-editor">
- <state line="14" column="3" selection-start="595" selection-end="595" vertical-scroll-proportion="0.0">
+ <state line="26" column="13" selection-start="770" selection-end="770" vertical-scroll-proportion="-1.4242837">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ </file>
+ <file leaf-file-name="ConstantConversionException.java" pinned="false" current="false" current-in-tab="false">
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/ConstantConversionException.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="25" column="6" selection-start="773" selection-end="773" vertical-scroll-proportion="0.25511596">
<folding />
</state>
</provider>
@@ -287,7 +299,16 @@
<file leaf-file-name="ContainerTest.java" pinned="false" current="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/test/com/google/inject/ContainerTest.java">
<provider selected="true" editor-type-id="text-editor">
- <state line="151" column="0" selection-start="3306" selection-end="3306" vertical-scroll-proportion="0.19918145">
+ <state line="44" column="0" selection-start="1314" selection-end="1314" vertical-scroll-proportion="0.579809">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ </file>
+ <file leaf-file-name="package-info.java" pinned="false" current="false" current-in-tab="false">
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/package-info.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0">
<folding />
</state>
</provider>
@@ -296,21 +317,32 @@
<file leaf-file-name="ContainerBuilder.java" pinned="false" current="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/com/google/inject/ContainerBuilder.java">
<provider selected="true" editor-type-id="text-editor">
- <state line="52" column="19" selection-start="1658" selection-end="1658" vertical-scroll-proportion="-5.7489767">
+ <state line="585" column="12" selection-start="17541" selection-end="17541" vertical-scroll-proportion="1.5075034">
<folding />
</state>
</provider>
</entry>
</file>
- <file leaf-file-name="SpringPerformanceComparison.java" pinned="false" current="true" current-in-tab="true">
- <entry file="file://$PROJECT_DIR$/test/com/google/inject/SpringPerformanceComparison.java">
+ <file leaf-file-name="PerformanceComparison.java" pinned="false" current="false" current-in-tab="false">
+ <entry file="file://$PROJECT_DIR$/test/com/google/inject/PerformanceComparison.java">
<provider selected="true" editor-type-id="text-editor">
- <state line="146" column="27" selection-start="5172" selection-end="5172" vertical-scroll-proportion="0.8144611">
+ <state line="100" column="11" selection-start="3562" selection-end="3562" vertical-scroll-proportion="0.9276944">
<folding />
</state>
</provider>
</entry>
</file>
+ <file leaf-file-name="AbstractModule.java" pinned="false" current="true" current-in-tab="true">
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/AbstractModule.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="30" column="11" selection-start="812" selection-end="812" vertical-scroll-proportion="0.4952251">
+ <folding>
+ <element signature="imports" expanded="true" />
+ </folding>
+ </state>
+ </provider>
+ </entry>
+ </file>
<file leaf-file-name="Key.java" pinned="false" current="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/com/google/inject/Key.java">
<provider selected="true" editor-type-id="text-editor">
@@ -323,7 +355,7 @@
<file leaf-file-name="AllTests.java" pinned="false" current="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/test/com/google/inject/AllTests.java">
<provider selected="true" editor-type-id="text-editor">
- <state line="41" column="13" selection-start="1395" selection-end="1395" vertical-scroll-proportion="0.48703957">
+ <state line="29" column="13" selection-start="964" selection-end="964" vertical-scroll-proportion="0.20873123">
<folding />
</state>
</provider>
@@ -341,7 +373,25 @@
<file leaf-file-name="Container.java" pinned="false" current="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/com/google/inject/Container.java">
<provider selected="true" editor-type-id="text-editor">
- <state line="70" column="20" selection-start="2038" selection-end="2038" vertical-scroll-proportion="0.8021828">
+ <state line="65" column="17" selection-start="1917" selection-end="1917" vertical-scroll-proportion="0.686221">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ </file>
+ <file leaf-file-name="ConstructionContext.java" pinned="false" current="false" current-in-tab="false">
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/ConstructionContext.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="38" column="17" selection-start="1151" selection-end="1151" vertical-scroll-proportion="0.44065484">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ </file>
+ <file leaf-file-name="Scope.java" pinned="false" current="false" current-in-tab="false">
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/Scope.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="24" column="1" selection-start="836" selection-end="836" vertical-scroll-proportion="0.55661666">
<folding />
</state>
</provider>
@@ -619,7 +669,7 @@
</key>
</component>
<component name="RestoreUpdateTree" />
- <component name="RunManager" selected="Application.SpringPerformanceComparison">
+ <component name="RunManager" selected="JUnit.AllTests">
<tempConfiguration default="false" name="AllTests" type="JUnit" factoryName="JUnit" enabled="false" merge="false">
<pattern value="com.google.inject.*" />
<module name="guice" />
@@ -693,8 +743,8 @@
<option name="Make" value="true" />
</method>
</configuration>
- <configuration default="false" name="SpringPerformanceComparison" type="Application" factoryName="Application" enabled="false" merge="false">
- <option name="MAIN_CLASS_NAME" value="com.google.inject.SpringPerformanceComparison" />
+ <configuration default="false" name="PerformanceComparison" type="Application" factoryName="Application" enabled="false" merge="false">
+ <option name="MAIN_CLASS_NAME" value="com.google.inject.PerformanceComparison" />
<option name="VM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$" />
@@ -708,9 +758,6 @@
<RunnerSettings RunnerId="Run" />
<ConfigurationWrapper RunnerId="Profile " />
<ConfigurationWrapper RunnerId="Run" />
- <method>
- <option name="Make" value="true" />
- </method>
</configuration>
</component>
<component name="ScopeViewComponent">
@@ -910,69 +957,6 @@
<option name="myLastEditedConfigurable" value="Default" />
</component>
<component name="editorHistoryManager">
- <entry file="file://$PROJECT_DIR$/test/com/google/inject/SuperclassTest.java">
- <provider selected="true" editor-type-id="text-editor">
- <state line="31" column="22" selection-start="1042" selection-end="1042" vertical-scroll-proportion="0.39427012">
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/test/com/google/inject/ConstantConversionTest.java">
- <provider selected="true" editor-type-id="text-editor">
- <state line="14" column="3" selection-start="595" selection-end="595" vertical-scroll-proportion="0.0">
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/test/com/google/inject/FactoryInjectionTest.java">
- <provider selected="true" editor-type-id="text-editor">
- <state line="19" column="51" selection-start="499" selection-end="499" vertical-scroll-proportion="0.44065484">
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/test/com/google/inject/FactoryTest.java">
- <provider selected="true" editor-type-id="text-editor">
- <state line="85" column="0" selection-start="2663" selection-end="2663" vertical-scroll-proportion="0.8021828">
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/test/com/google/inject/NotRequiredTest.java">
- <provider selected="true" editor-type-id="text-editor">
- <state line="29" column="43" selection-start="956" selection-end="956" vertical-scroll-proportion="0.3478854">
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/test/com/google/inject/ContainerTest.java">
- <provider selected="true" editor-type-id="text-editor">
- <state line="151" column="0" selection-start="3306" selection-end="3306" vertical-scroll-proportion="0.19918145">
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/src/com/google/inject/ContainerImpl.java">
- <provider selected="true" editor-type-id="text-editor">
- <state line="137" column="28" selection-start="4578" selection-end="4578" vertical-scroll-proportion="0.3383356">
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/src/com/google/inject/Key.java">
- <provider selected="true" editor-type-id="text-editor">
- <state line="139" column="5" selection-start="3533" selection-end="3533" vertical-scroll-proportion="2.8526604">
- <folding />
- </state>
- </provider>
- </entry>
- <entry file="file://$PROJECT_DIR$/test/com/google/inject/AllTests.java">
- <provider selected="true" editor-type-id="text-editor">
- <state line="41" column="13" selection-start="1395" selection-end="1395" vertical-scroll-proportion="0.48703957">
- <folding />
- </state>
- </provider>
- </entry>
<entry file="file://$PROJECT_DIR$/src/com/google/inject/util/Function.java">
<provider selected="true" editor-type-id="text-editor">
<state line="31" column="3" selection-start="1284" selection-end="1284" vertical-scroll-proportion="0.39427012">
@@ -994,27 +978,92 @@
</state>
</provider>
</entry>
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/Scope.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="24" column="1" selection-start="836" selection-end="836" vertical-scroll-proportion="0.55661666">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/test/com/google/inject/ConstantConversionTest.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="26" column="13" selection-start="770" selection-end="770" vertical-scroll-proportion="-1.4242837">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/ConstantConversionException.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="25" column="6" selection-start="773" selection-end="773" vertical-scroll-proportion="0.25511596">
+ <folding />
+ </state>
+ </provider>
+ </entry>
<entry file="file://$PROJECT_DIR$/src/com/google/inject/Container.java">
<provider selected="true" editor-type-id="text-editor">
- <state line="70" column="20" selection-start="2038" selection-end="2038" vertical-scroll-proportion="0.8021828">
+ <state line="65" column="17" selection-start="1917" selection-end="1917" vertical-scroll-proportion="0.686221">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/ConstructionContext.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="38" column="17" selection-start="1151" selection-end="1151" vertical-scroll-proportion="0.44065484">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/test/com/google/inject/ContainerTest.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="44" column="0" selection-start="1314" selection-end="1314" vertical-scroll-proportion="0.579809">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/package-info.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/test/com/google/inject/PerformanceComparison.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="100" column="11" selection-start="3562" selection-end="3562" vertical-scroll-proportion="0.9276944">
+ <folding />
+ </state>
+ </provider>
+ </entry>
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/ContainerImpl.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="671" column="29" selection-start="21743" selection-end="21743" vertical-scroll-proportion="0.3383356">
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/com/google/inject/ContainerBuilder.java">
<provider selected="true" editor-type-id="text-editor">
- <state line="52" column="19" selection-start="1658" selection-end="1658" vertical-scroll-proportion="-5.7489767">
+ <state line="585" column="12" selection-start="17541" selection-end="17541" vertical-scroll-proportion="1.5075034">
<folding />
</state>
</provider>
</entry>
- <entry file="file://$PROJECT_DIR$/test/com/google/inject/SpringPerformanceComparison.java">
+ <entry file="file://$PROJECT_DIR$/test/com/google/inject/AllTests.java">
<provider selected="true" editor-type-id="text-editor">
- <state line="146" column="27" selection-start="5172" selection-end="5172" vertical-scroll-proportion="0.8144611">
+ <state line="29" column="13" selection-start="964" selection-end="964" vertical-scroll-proportion="0.20873123">
<folding />
</state>
</provider>
</entry>
+ <entry file="file://$PROJECT_DIR$/src/com/google/inject/AbstractModule.java">
+ <provider selected="true" editor-type-id="text-editor">
+ <state line="30" column="11" selection-start="812" selection-end="812" vertical-scroll-proportion="0.4952251">
+ <folding>
+ <element signature="imports" expanded="true" />
+ </folding>
+ </state>
+ </provider>
+ </entry>
</component>
</project>
diff --git a/src/com/google/inject/AbstractModule.java b/src/com/google/inject/AbstractModule.java
new file mode 100644
index 0000000..2b76aaa
--- /dev/null
+++ b/src/com/google/inject/AbstractModule.java
@@ -0,0 +1,116 @@
+// Copyright 2006 Google Inc. All Rights Reserved.
+
+package com.google.inject;
+
+import com.google.inject.util.Objects;
+
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * A support class for {@link Module}s which reduces repetition and
+ * results in a more readable configuration. Simply extends this class,
+ * implement {@link #configure()}, and call inherited methods.
+ *
+ * @author crazybob@google.com (Bob Lee)
+ */
+public abstract class AbstractModule implements Module {
+
+ ContainerBuilder builder;
+
+ public final synchronized void configure(ContainerBuilder builder) {
+ try {
+ if (this.builder != null) {
+ throw new IllegalStateException("Re-entry is not allowed.");
+ }
+ this.builder = Objects.nonNull(builder, "builder");
+
+ configure();
+
+ } finally {
+ this.builder = null;
+ }
+ }
+
+ /**
+ * Configures a {@link ContainerBuilder} via the exposed methods.
+ */
+ protected abstract void configure();
+
+ /**
+ * @see ContainerBuilder#put(String, Scope)
+ */
+ protected void put(String name, Scope scope) {
+ builder.put(name, scope);
+ }
+
+ /**
+ * @see ContainerBuilder#bind(Key)
+ */
+ protected <T> ContainerBuilder.BindingBuilder<T> bind(Key<T> key) {
+ return builder.bind(key);
+ }
+
+ /**
+ * @see ContainerBuilder#bind(TypeToken)
+ */
+ protected <T> ContainerBuilder.BindingBuilder<T> bind(TypeToken<T> typeToken) {
+ return builder.bind(typeToken);
+ }
+
+ /**
+ * @see ContainerBuilder#bind(Class)
+ */
+ protected <T> ContainerBuilder.BindingBuilder<T> bind(Class<T> clazz) {
+ return builder.bind(clazz);
+ }
+
+ /**
+ * @see ContainerBuilder#link(Key)
+ */
+ protected <T> ContainerBuilder.LinkedBindingBuilder<T> link(Key<T> key) {
+ return builder.link(key);
+ }
+
+ /**
+ * @see ContainerBuilder#bind(String)
+ */
+ protected ContainerBuilder.ConstantBindingBuilder bind(String name) {
+ return builder.bind(name);
+ }
+
+ /**
+ * @see ContainerBuilder#bindProperties(java.util.Map)
+ */
+ protected void bindProperties(Map<String, String> properties) {
+ builder.bindProperties(properties);
+ }
+
+ /**
+ * @see ContainerBuilder#bindProperties(java.util.Properties)
+ */
+ protected void bindProperties(Properties properties) {
+ builder.bindProperties(properties);
+ }
+
+ /**
+ * @see ContainerBuilder#injectStatics(Class[])
+ */
+ protected void injectStatics(Class<?>... types) {
+ builder.injectStatics(types);
+ }
+
+ /**
+ * @see ContainerBuilder#add(ErrorMessage)
+ */
+ protected void add(ErrorMessage errorMessage) {
+ builder.add(errorMessage);
+ }
+
+ /**
+ * @see ContainerBuilder#apply(Module)
+ */
+ protected void apply(Module module) {
+ builder.apply(module);
+ }
+}
diff --git a/src/com/google/inject/Container.java b/src/com/google/inject/Container.java
index 4370e51..169f5f5 100644
--- a/src/com/google/inject/Container.java
+++ b/src/com/google/inject/Container.java
@@ -18,7 +18,7 @@
/**
* Injects dependencies into constructors, methods and fields annotated with
- * {@link Inject}.
+ * @{@link Inject}.
*
* <p>When injecting a method or constructor, you can additionally annotate
* its parameters with {@link Inject} and specify a dependency name. When a
diff --git a/src/com/google/inject/ContainerBuilder.java b/src/com/google/inject/ContainerBuilder.java
index b4107de..4cc8506 100644
--- a/src/com/google/inject/ContainerBuilder.java
+++ b/src/com/google/inject/ContainerBuilder.java
@@ -196,7 +196,7 @@
/**
* Binds a string constant for each property.
*/
- public ContainerBuilder bindProperties(Map<String, String> properties) {
+ public void bindProperties(Map<String, String> properties) {
ensureNotCreated();
Object source = source();
for (Map.Entry<String, String> entry : properties.entrySet()) {
@@ -204,13 +204,12 @@
String value = entry.getValue();
bind(key, source).to(value);
}
- return this;
}
/**
* Binds a string constant for each property.
*/
- public ContainerBuilder bindProperties(Properties properties) {
+ public void bindProperties(Properties properties) {
ensureNotCreated();
Object source = source();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
@@ -218,7 +217,6 @@
String value = (String) entry.getValue();
bind(key, source).to(value);
}
- return this;
}
/**
@@ -227,9 +225,15 @@
*
* @param types for which static members will be injected
*/
- public ContainerBuilder injectStatics(Class<?>... types) {
+ public void injectStatics(Class<?>... types) {
staticInjections.addAll(Arrays.asList(types));
- return this;
+ }
+
+ /**
+ * Applies the given module to this builder.
+ */
+ public void apply(Module module) {
+ module.configure(this);
}
/**
diff --git a/test/com/google/inject/ContainerTest.java b/test/com/google/inject/ContainerTest.java
index 0a48d54..2e8649c 100644
--- a/test/com/google/inject/ContainerTest.java
+++ b/test/com/google/inject/ContainerTest.java
@@ -41,10 +41,15 @@
private Container createFooContainer() {
ContainerBuilder builder = new ContainerBuilder();
- builder.bind(Bar.class).to(BarImpl.class);
- builder.bind(Tee.class).to(TeeImpl.class);
- builder.bind("s").to("test");
- builder.bind("i").to(5);
+
+ builder.apply(new AbstractModule() {
+ protected void configure() {
+ bind(Bar.class).to(BarImpl.class);
+ bind(Tee.class).to(TeeImpl.class);
+ bind("s").to("test");
+ bind("i").to(5);
+ }
+ });
return builder.create(false);
}
diff --git a/test/com/google/inject/SpringPerformanceComparison.java b/test/com/google/inject/PerformanceComparison.java
similarity index 83%
rename from test/com/google/inject/SpringPerformanceComparison.java
rename to test/com/google/inject/PerformanceComparison.java
index e26b2ca..7fb4b85 100644
--- a/test/com/google/inject/SpringPerformanceComparison.java
+++ b/test/com/google/inject/PerformanceComparison.java
@@ -36,7 +36,7 @@
*
* @author crazybob@google.com (Bob Lee)
*/
-public class SpringPerformanceComparison {
+public class PerformanceComparison {
static final Callable<Foo> springFactory = new Callable<Foo>() {
@@ -79,11 +79,16 @@
final Factory<Foo> fooFactory;
{
ContainerBuilder builder = new ContainerBuilder();
- builder.bind(Tee.class).to(TeeImpl.class);
- builder.bind(Bar.class).to(BarImpl.class);
- builder.bind(Foo.class).to(Foo.class);
- builder.bind("i").to(5);
- builder.bind("s").to("test");
+
+ builder.apply(new AbstractModule() {
+ protected void configure() {
+ bind(Tee.class).to(TeeImpl.class);
+ bind(Bar.class).to(BarImpl.class);
+ bind(Foo.class).to(Foo.class);
+ bind("i").to(5);
+ bind("s").to("test");
+ }
+ });
fooFactory = builder.create(false).getCreator(Foo.class);
}
@@ -117,37 +122,31 @@
}
public static void main(String[] args) throws Exception {
- validate(springFactory);
+ // Once warm up. Takes lazy loading out of the equation and ensures we
+ // created the graphs properly.
validate(springFactory);
validate(juiceFactory);
- validate(juiceFactory);
- validate(byHandFactory);
validate(byHandFactory);
- int count = 100000;
for (int i2 = 0; i2 < 10; i2++) {
- long time = System.currentTimeMillis();
- for (int i = 0; i < count; i++)
- springFactory.call();
- time = System.currentTimeMillis() - time;
- System.err.println("Spring: " + count * 1000 / time + " creations/s");
-
- time = System.currentTimeMillis();
- for (int i = 0; i < count; i++)
- juiceFactory.call();
- time = System.currentTimeMillis() - time;
- System.err.println("Guice: " + count * 1000 / time + " creations/s");
-
- time = System.currentTimeMillis();
- for (int i = 0; i < count; i++)
- byHandFactory.call();
- time = System.currentTimeMillis() - time;
- System.err.println("By Hand: " + count * 1000 / time + " creations/s");
+ iterate(springFactory, "Spring: ");
+ iterate(juiceFactory, "Guice: ");
+ iterate(byHandFactory, "By Hand: ");
System.err.println();
}
}
+ static void iterate(Callable<Foo> callable, String label) throws Exception {
+ int count = 100000;
+ long time = System.currentTimeMillis();
+ for (int i = 0; i < count; i++) {
+ callable.call();
+ }
+ time = System.currentTimeMillis() - time;
+ System.err.println(label + count * 1000 / time + " creations/s");
+ }
+
public static class Foo {
Bar bar;