/*
 * Copyright 2009 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.template.soy.shared.internal;

import com.google.common.collect.ImmutableMap;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.multibindings.Multibinder;
import com.google.template.soy.shared.restricted.ApiCallScopeBindingAnnotations.LocaleString;
import com.google.template.soy.shared.restricted.SoyFunction;
import com.google.template.soy.shared.restricted.SoyPrintDirective;
import java.util.Set;
import javax.inject.Singleton;

/**
 * Guice module for users that want to contribute plugins to Soy via Guice.
 *
 * <p>Important: Do not use outside of Soy code (treat as superpackage-private).
 *
 */
public final class SharedModule extends AbstractModule {

  public SharedModule() {}

  @Override
  protected void configure() {
    // Create empty multibinders so we can inject user-supplied ones.
    Multibinder.newSetBinder(binder(), SoyFunction.class);
    Multibinder.newSetBinder(binder(), SoyPrintDirective.class);

    bind(SoyScopedData.class).toInstance(new SoySimpleScope());
  }

  // Unused by Soy, but provided because user plugins currently inject this.
  @Provides
  @LocaleString
  String provideLocaleString(SoyScopedData data) {
    return data.getLocale();
  }

  /**
   * Builds and provides the map of all installed SoyFunctions (name to function).
   *
   * @param soyFunctionsSet The installed set of SoyFunctions (from Guice Multibinder).
   */
  @Provides
  @Singleton
  ImmutableMap<String, ? extends SoyFunction> provideSoyFunctionsMap(
      Set<SoyFunction> soyFunctionsSet) {
    ImmutableMap.Builder<String, SoyFunction> mapBuilder = ImmutableMap.builder();
    for (SoyFunction function : soyFunctionsSet) {
      mapBuilder.put(function.getName(), function);
    }
    return mapBuilder.build();
  }

  /**
   * Builds and provides the map of all installed SoyPrintDirectives (name to directive).
   *
   * @param soyDirectivesSet The installed set of SoyPrintDirectives (from Guice Multibinder).
   */
  @Provides
  @Singleton
  ImmutableMap<String, ? extends SoyPrintDirective> provideSoyDirectivesMap(
      Set<SoyPrintDirective> soyDirectivesSet) {
    ImmutableMap.Builder<String, SoyPrintDirective> mapBuilder = ImmutableMap.builder();
    for (SoyPrintDirective directive : soyDirectivesSet) {
      mapBuilder.put(directive.getName(), directive);
    }
    return mapBuilder.build();
  }

  @Override
  public boolean equals(Object other) {
    return other != null && this.getClass().equals(other.getClass());
  }

  @Override
  public int hashCode() {
    return this.getClass().hashCode();
  }
}

@@ -1038,16 +1038,70 @@
 ective;%0A
+import java.util.LinkedHashMap;%0Aimport java.util.Map;%0A
 import j
@@ -2229,36 +2229,19 @@
 ) %7B%0A    
-ImmutableMap.Builder
+Map
 %3CString,
@@ -2267,36 +2267,35 @@
 ilder = 
-ImmutableMap.builder
+new LinkedHashMap%3C%3E
 ();%0A    
@@ -2338,32 +2338,50 @@
 ionsSet) %7B%0A     
+ SoyFunction old =
  mapBuilder.put(
@@ -2419,38 +2419,415 @@
     
-%7D%0A    return mapBuilder.b
+  if (old != null && !old.getClass().getName().equals(function.getClass().getName())) %7B%0A        throw new IllegalStateException(%0A            %22Found two functions with the same name: '%22%0A                + function.getName()%0A                + %22' and different implementations: %22%0A                + function%0A                + %22 vs %22%0A                + old);%0A      %7D%0A    %7D%0A    return ImmutableMap.copyOf(mapB
 uild
-(
+er
 );%0A 
@@ -3191,28 +3191,11 @@
     
-ImmutableMap.Builder
+Map
 %3CStr
@@ -3235,28 +3235,27 @@
 r = 
-ImmutableMap.builder
+new LinkedHashMap%3C%3E
 ();%0A
@@ -3318,16 +3318,40 @@
  %7B%0A     
+ SoyPrintDirective old =
  mapBuil
@@ -3399,38 +3399,425 @@
     
-%7D%0A    return mapBuilder.b
+  if (old != null && !old.getClass().getName().equals(directive.getClass().getName())) %7B%0A        throw new IllegalStateException(%0A            %22Found two print directives with the same name: '%22%0A                + directive.getName()%0A                + %22' and different implementations: %22%0A                + directive%0A                + %22 vs %22%0A                + old);%0A      %7D%0A    %7D%0A    return ImmutableMap.copyOf(mapB
 uild
-(
+er
 );%0A 

Allow duplicate soy functions and print directives to be registered via guice as long as they have the same class name.