Refactored scope handling. Allows overriding annotations with in(). Improved error reporting.

git-svn-id: https://google-guice.googlecode.com/svn/trunk@117 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/src/com/google/inject/Scopes.java b/src/com/google/inject/Scopes.java
index 86a6ca3..67b7220 100644
--- a/src/com/google/inject/Scopes.java
+++ b/src/com/google/inject/Scopes.java
@@ -16,6 +16,9 @@
 
 package com.google.inject;
 
+import java.lang.annotation.Annotation;
+import java.util.Map;
+
 /**
  * Built in scope implementations.
  *
@@ -23,8 +26,7 @@
  */
 public class Scopes {
 
-  private Scopes() {
-  }
+  private Scopes() {}
 
   /**
    * Name of the default scope.
@@ -38,6 +40,10 @@
     public <T> Factory<T> scope(Key<T> key, Factory<T> creator) {
       return creator;
     }
+
+    public String toString() {
+      return DEFAULT_NAME;
+    }
   };
 
   /**
@@ -54,7 +60,7 @@
 
         private volatile T instance;
 
-        // DCL on a volatile is safe as of Java 5, which we obviously require
+        // DCL on a volatile is safe as of Java 5, which we obviously require.
         @SuppressWarnings("DoubleCheckedLocking")
         public T get() {
           if (instance == null) {
@@ -78,5 +84,109 @@
         }
       };
     }
+
+    public String toString() {
+      return CONTAINER_NAME;
+    }
   };
+
+  /**
+   * Gets the scope for a type based on its annotations. Returns {@code null}
+   * if none specified.
+   *
+   * @param implementation type
+   * @param scopes map of scope names to scopes
+   * @param errorHandler handles errors
+   */
+  static Scope getScopeForType(Class<?> implementation,
+      Map<String, Scope> scopes, ErrorHandler errorHandler) {
+    return getScopeForName(
+        getScopeNameForType(implementation, errorHandler),
+        scopes,
+        errorHandler
+    );
+
+  }
+
+  /**
+   * Finds scope for the given name. Returns {@code null} if the name is
+   * {@code null}. Otherwise, records an error if a scope isn't found.
+   */
+  static Scope getScopeForName(String name, Map<String, Scope> scopes,
+      ErrorHandler errorHandler) {
+    // None found.
+    if (name == null) {
+      return null;
+    }
+
+    // Look up scope for name.
+    Scope scope = scopes.get(name);
+    if (scope == null) {
+      errorHandler.handle(
+          ErrorMessages.SCOPE_NOT_FOUND, name, scopes.keySet());
+    }
+    return scope;
+  }
+
+  /**
+   * Gets the scope name from annotations on the given type. Records errors if
+   * multiple names are found.
+   */
+  static String getScopeNameForType(Class<?> implementation,
+      ErrorHandler errorHandler) {
+    // The first name and annotation type we come to. We hold on to the
+    // annotation type in case we need it in an error message.
+    String firstName = null;
+    Class<? extends Annotation> firstType = null;
+
+    for (Annotation annotation : implementation.getAnnotations()) {
+      // Look for @Scoped on the class itself and on annotations.
+      Scoped scoped = findScoped(annotation);
+
+      // If we found an @Scoped, record its value or an error if we already
+      // recorded a value.
+      if (scoped != null) {
+        String name = scoped.value();
+        Class<? extends Annotation> type = annotation.annotationType();
+        if (firstName == null) {
+          firstName = name;
+          firstType = type;
+        } else {
+          // Scope already set.
+          errorHandler.handle(ErrorMessages.SCOPE_ALREADY_SET_BY_ANNOTATION,
+              implementation, type.getSimpleName(), name,
+              firstType.getSimpleName(), firstName);
+        }
+      }
+    }
+
+    return firstName;
+  }
+
+  /**
+   * The given annotation may be an instance of {@code Scoped} or it may be
+   * annotated with {@code Scoped}.
+   */
+  static Scoped findScoped(Annotation annotation) {
+    Class<? extends Annotation> annotationType = annotation.annotationType();
+    return annotationType == Scoped.class
+        ? (Scoped) annotation
+        : annotationType.getAnnotation(Scoped.class);
+  }
+
+  /**
+   * Scopes an internal factory.
+   */
+  static <T> InternalFactory<? extends T> scope(Key<T> key,
+      ContainerImpl container, InternalFactory<? extends T> creator,
+      Scope scope) {
+    // Default scope does nothing.
+    if (scope == null || scope == DEFAULT) {
+      return creator;
+    }
+
+    Factory<T> scoped = scope.scope(key,
+        new FactoryToInternalFactoryAdapter<T>(container, creator));
+    return new InternalFactoryToFactoryAdapter<T>(scoped);
+  }
 }