// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container;

import com.yahoo.container.core.http.HttpFilterConfig;
import com.yahoo.container.di.componentgraph.Provider;
import com.yahoo.jdisc.http.filter.FilterConfig;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Tony Vaagenes
 */
public final class FilterConfigProvider implements Provider<FilterConfig> {

    private static class MapFilterConfig implements FilterConfig {

        private final Map<String, String> initParameters;
        private final String filterName;
        private final String filterClass;

        MapFilterConfig(Map<String, String> initParameters, String filterName, String filterClass) {
            this.initParameters = initParameters;
            this.filterName = filterName;
            this.filterClass = filterClass;
        }

        @Override
        public String getFilterName() {
            return filterName;
        }

        @Override
        public String getFilterClass() {
            return filterClass;
        }

        @Override
        public String getInitParameter(String name) {
            return initParameters.get(name);
        }

        @Override
        public boolean getBooleanInitParameter(String name, boolean defaultValue) {
            return initParameters.containsKey(name) ?
                    Boolean.parseBoolean(initParameters.get(name)) :
                    defaultValue;
        }

        @Override
        public Collection<String> getInitParameterNames() {
            return initParameters.keySet();
        }
    }

    private final FilterConfig filterConfig;

    public FilterConfigProvider(HttpFilterConfig vespaConfig) {
        this(new MapFilterConfig(toMap(vespaConfig), vespaConfig.filterName(), vespaConfig.filterClass()));
    }

    private FilterConfigProvider(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }

    private static Map<String, String> toMap(HttpFilterConfig vespaConfig) {
        Map<String, String> parameters = new HashMap<>();
        for (HttpFilterConfig.Param param : vespaConfig.param())
            parameters.put(param.name(), param.value());
        return parameters;
    }

    public static FilterConfigProvider from(String filterName, String filterClass, Map<String, String> initParameters) {
        return new FilterConfigProvider(new MapFilterConfig(initParameters, filterName, filterClass));
    }

    @Override
    public FilterConfig get() {
        return filterConfig;
    }

    @Override
    public void deconstruct() {}
}

@@ -1809,13 +1809,23 @@
     
-this(
+filterConfig = 
 new 
@@ -1916,120 +1916,8 @@
 s())
-);%0A    %7D%0A%0A    private FilterConfigProvider(FilterConfig filterConfig) %7B%0A        this.filterConfig = filterConfig
 ;%0A  
@@ -2174,24 +2174,24 @@
 m.value());%0A
+
         retu
@@ -2216,239 +2216,8 @@
  %7D%0A%0A
-    public static FilterConfigProvider from(String filterName, String filterClass, Map%3CString, String%3E initParameters) %7B%0A        return new FilterConfigProvider(new MapFilterConfig(initParameters, filterName, filterClass));%0A    %7D%0A%0A
     

Revert FilterConfigProvider changes from 2f0e8c98e495ea5b986ec2614bafa33c1aacf5d8