Examples.

git-svn-id: https://google-guice.googlecode.com/svn/trunk@247 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/guice.ipr b/guice.ipr
index ba9e813..9c5c59f 100644
--- a/guice.ipr
+++ b/guice.ipr
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project version="4" relativePaths="true">
+<project version="4" relativePaths="false">
   <component name="AntConfiguration">
     <defaultAnt bundledAnt="true" />
     <buildFile url="file://$PROJECT_DIR$/build.xml">
@@ -14,7 +14,7 @@
     <settings>
       <class-settings class="com.google.devtools.intellig.configcheck.ProjectPathChecker" />
       <class-settings class="com.google.devtools.intellig.configcheck.ProjectJdkChecker">
-        <setting name="getProjectJdk" value="$PROJECT_DIR$/../../buildtools/java/jdk1.5.0_06" />
+        <setting name="getProjectJdk" value="/Users/crazybob/buildtools/java/jdk1.5.0_06" />
         <setting name="getModuleJdks" value="rO0ABXNyABFqYXZhLnV0aWwuSGFzaFNldLpEhZWWuLc0AwAAeHB3DAAAABA/QAAAAAAAAHg=" />
       </class-settings>
       <class-settings class="com.google.devtools.intellig.configcheck.PythonSdkChecker" />
diff --git a/src/com/google/inject/DefaultImplementation.java b/src/com/google/inject/DefaultImplementation.java
new file mode 100644
index 0000000..7191bf7
--- /dev/null
+++ b/src/com/google/inject/DefaultImplementation.java
@@ -0,0 +1,30 @@
+/**
+ * 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;
+
+/**
+ * A pointer to the default implementation of a type.
+ *
+ * @author crazybob@google.com (Bob Lee)
+ */
+public @interface DefaultImplementation {
+
+  /**
+   * The implementation type.
+   */
+  Class<?> value();
+}
diff --git a/src/com/google/inject/DefaultProvider.java b/src/com/google/inject/DefaultProvider.java
new file mode 100644
index 0000000..7128ed2
--- /dev/null
+++ b/src/com/google/inject/DefaultProvider.java
@@ -0,0 +1,30 @@
+/**
+ * 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;
+
+/**
+ * A pointer to the default provider implementation for a type.
+ *
+ * @author crazybob@google.com (Bob Lee)
+ */
+public @interface DefaultProvider {
+
+  /**
+   * The implementation type.
+   */
+  Class<?> value();
+}
diff --git a/test/com/google/inject/example/ClientServiceWithDependencyInjection.java b/test/com/google/inject/example/ClientServiceWithDependencyInjection.java
new file mode 100644
index 0000000..ada9dd5
--- /dev/null
+++ b/test/com/google/inject/example/ClientServiceWithDependencyInjection.java
@@ -0,0 +1,102 @@
+/**
+ * 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.example;
+
+import static junit.framework.Assert.assertTrue;
+import com.google.inject.example.ClientServiceWithFactories.Service;
+import com.google.inject.example.ClientServiceWithFactories.Factory;
+import com.google.inject.example.ClientServiceWithFactories.ServiceImpl;
+import com.google.inject.example.ClientServiceWithFactories.ServiceFactory;
+import com.google.inject.example.ClientServiceWithFactories.MockService;
+import com.google.inject.example.ClientServiceWithFactories.Client;
+import junit.framework.Assert;
+
+/**
+ * @author crazybob@google.com (Bob Lee)
+ */
+public class ClientServiceWithDependencyInjection {
+
+// 62 lines
+
+public interface Service {
+  void go();
+}
+
+public static class ServiceImpl implements ClientServiceWithDependencyInjection.Service {
+  public void go() {
+    // ...
+  }
+}
+
+public static class ServiceFactory {
+
+  private ServiceFactory() {}
+
+  private static final Service service = new ServiceImpl();
+
+  public static Service getInstance() {
+    return service;
+  }
+}
+
+public static class Client {
+
+  private final Service service;
+
+  public Client(Service service) {
+    this.service = service;
+  }
+
+  public void go() {
+    service.go();
+  }
+}
+
+public static class ClientFactory {
+
+  private ClientFactory() {}
+
+  public static Client getInstance() {
+    Service service = ServiceFactory.getInstance();
+    return new Client(service);
+  }
+}
+
+public void testClient() {
+  MockService mock = new MockService();
+  Client client = new Client(mock);
+  client.go();
+  assertTrue(mock.isGone());
+}
+
+public static class MockService implements Service {
+
+  private boolean gone = false;
+
+  public void go() {
+    gone = true;
+  }
+
+  public boolean isGone() {
+    return gone;
+  }
+}
+
+  public static void main(String[] args) {
+    new ClientServiceWithDependencyInjection().testClient();
+  }
+}
diff --git a/test/com/google/inject/example/ClientServiceWithFactories.java b/test/com/google/inject/example/ClientServiceWithFactories.java
index 55c9ff7..c07fa60 100644
--- a/test/com/google/inject/example/ClientServiceWithFactories.java
+++ b/test/com/google/inject/example/ClientServiceWithFactories.java
@@ -23,6 +23,8 @@
  */
 public class ClientServiceWithFactories {
 
+// 72 lines
+
 public interface Service {
   void go();
 }
@@ -33,45 +35,48 @@
   }
 }
 
-public static abstract class ServiceFactory {
+public interface Factory<T> {
+  T get();
+}
 
-  public abstract Service getService();
+public static class ServiceFactory {
 
-  static ServiceFactory instance = new DefaultServiceFactory();
+  private ServiceFactory() {}
 
-  public static ServiceFactory getInstance() {
+  private static Factory<Service> instance = new Factory<Service>() {
+    private final Service service = new ServiceImpl();
+    public Service get() {
+      return service;
+    }
+  };
+
+  public static Factory<Service> getInstance() {
     return instance;
   }
 
-  public static void setInstance(ServiceFactory serviceFactory) {
+  public static void setInstance(Factory<Service> serviceFactory) {
     instance = serviceFactory;
   }
 }
 
-public static class DefaultServiceFactory extends ServiceFactory {
-  public Service getService() {
-    return new ServiceImpl();
-  }
-}
+public static class Client {
 
-static class Client {
-
-  final Service service;
-
-  Client() {
-    this.service = ServiceFactory.getInstance().getService();
-  }
-
-  void go() {
+  public void go() {
+    Factory<Service> factory = ServiceFactory.getInstance();
+    Service service = factory.get();
     service.go();
   }
 }
 
 public void testClient() {
-  ServiceFactory previous = ServiceFactory.getInstance();
+  Factory<Service> previous = ServiceFactory.getInstance();
   try {
-    MockService mock = new MockService();
-    ServiceFactory.setInstance(new SingletonServiceFactory(mock));
+    final MockService mock = new MockService();
+    ServiceFactory.setInstance(new Factory<Service>() {
+      public Service get() {
+        return mock;
+      }
+    });
     Client client = new Client();
     client.go();
     assertTrue(mock.isGone());
@@ -83,7 +88,7 @@
 
 public static class MockService implements Service {
 
-  boolean gone = false;
+  private boolean gone = false;
 
   public void go() {
     gone = true;
@@ -94,20 +99,7 @@
   }
 }
 
-public static class SingletonServiceFactory extends ServiceFactory {
-
-  final Service service;
-
-  public SingletonServiceFactory(Service service) {
-    this.service = service;
+  public static void main(String[] args) {
+    new ClientServiceWithFactories().testClient();
   }
-
-  public Service getService() {
-    return this.service;
-  }
-}
-
-public static void main(String[] args) {
-  new ClientServiceWithFactories().testClient();
-}
 }
diff --git a/test/com/google/inject/example/ClientServiceWithGuice.java b/test/com/google/inject/example/ClientServiceWithGuice.java
index 061dbbc..d16dd83 100644
--- a/test/com/google/inject/example/ClientServiceWithGuice.java
+++ b/test/com/google/inject/example/ClientServiceWithGuice.java
@@ -1,4 +1,18 @@
-// Copyright 2006 Google Inc. All Rights Reserved.
+/**
+ * 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.example;
 
@@ -7,6 +21,7 @@
 import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
+import com.google.inject.Scopes;
 import static junit.framework.Assert.assertTrue;
 
 /**
@@ -14,6 +29,8 @@
  */
 public class ClientServiceWithGuice {
 
+// 48 lines
+
 public interface Service {
   void go();
 }
@@ -24,15 +41,15 @@
   }
 }
 
-public static class ServiceModule extends AbstractModule {
+public static class MyModule extends AbstractModule {
   protected void configure() {
-    bind(Service.class).to(ServiceImpl.class);
+    bind(Service.class).to(ServiceImpl.class).in(Scopes.SINGLETON);
   }
 }
 
 public static class Client {
 
-  final Service service;
+  private final Service service;
 
   @Inject
   public Client(Service service) {
@@ -47,12 +64,13 @@
 public void testClient() {
   MockService mock = new MockService();
   Client client = new Client(mock);
+  client.go();
   assertTrue(mock.isGone());
 }
 
 public static class MockService implements Service {
 
-  boolean gone = false;
+  private boolean gone = false;
 
   public void go() {
     gone = true;
@@ -65,8 +83,7 @@
 
 public static void main(String[] args) throws CreationException {
   new ClientServiceWithGuice().testClient();
-
-  Injector injector = Guice.createInjector(new ServiceModule());
+  Injector injector = Guice.createInjector(new MyModule());
   Client client = injector.getInstance(Client.class);
 }
 }
diff --git a/test/com/google/inject/example/ClientServiceWithGuiceDefaults.java b/test/com/google/inject/example/ClientServiceWithGuiceDefaults.java
new file mode 100644
index 0000000..b61e1de
--- /dev/null
+++ b/test/com/google/inject/example/ClientServiceWithGuiceDefaults.java
@@ -0,0 +1,86 @@
+/**
+ * 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.example;
+
+import com.google.inject.Inject;
+import com.google.inject.CreationException;
+import com.google.inject.Injector;
+import com.google.inject.Guice;
+import com.google.inject.DefaultImplementation;
+import com.google.inject.Singleton;
+import com.google.inject.example.ClientServiceWithGuice.MyModule;
+import junit.framework.Assert;
+
+/**
+ * @author crazybob@google.com (Bob Lee)
+ */
+public class ClientServiceWithGuiceDefaults {
+
+// 44 lines
+
+@DefaultImplementation(ServiceImpl.class)
+public interface Service {
+  void go();
+}
+
+@Singleton
+public static class ServiceImpl implements ClientServiceWithGuiceDefaults.Service {
+  public void go() {
+    // ...
+  }
+}
+
+public static class Client {
+
+  private final Service service;
+
+  @Inject
+  public Client(Service service) {
+    this.service = service;
+  }
+
+  public void go() {
+    service.go();
+  }
+}
+
+public void testClient() {
+  MockService mock = new MockService();
+  Client client = new Client(mock);
+  client.go();
+  Assert.assertTrue(mock.isGone());
+}
+
+public static class MockService implements Service {
+
+  private boolean gone = false;
+
+  public void go() {
+    gone = true;
+  }
+
+  public boolean isGone() {
+    return gone;
+  }
+}
+
+public static void main(String[] args) throws CreationException {
+  new ClientServiceWithGuiceDefaults().testClient();
+  Injector injector = Guice.createInjector(new MyModule());
+  Client client = injector.getProvider(Client.class).get();
+}
+}