// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.http.xml;

import com.yahoo.config.model.producer.AbstractConfigProducer;
import com.yahoo.vespa.model.builder.xml.dom.chains.ChainsBuilder;
import com.yahoo.vespa.model.builder.xml.dom.chains.ComponentsBuilder;
import com.yahoo.vespa.model.builder.xml.dom.chains.ComponentsBuilder.ComponentType;
import com.yahoo.vespa.model.builder.xml.dom.chains.DomChainBuilderBase;
import com.yahoo.vespa.model.builder.xml.dom.chains.DomChainsBuilder;
import com.yahoo.vespa.model.container.component.chain.Chain;
import com.yahoo.vespa.model.container.http.Filter;
import com.yahoo.vespa.model.container.http.FilterChains;
import org.w3c.dom.Element;

import java.util.*;

/**
 * @author tonytv
 */
public class FilterChainsBuilder extends DomChainsBuilder<Filter, Chain<Filter>, FilterChains>  {
    private static final Collection<ComponentType<Filter>> allowedComponentTypes =
            Collections.singleton(ComponentType.filter);

    //TODO: simplify
    private static final Map<String, Class<? extends DomChainBuilderBase<? extends Filter, ? extends Chain<Filter>>>> chainType2BuilderClass =
            Collections.unmodifiableMap(
                    new LinkedHashMap<String, Class<? extends DomChainBuilderBase<? extends Filter, ? extends Chain<Filter>>>>() {{
                        put("request-chain", FilterChainBuilder.class);
                        put("response-chain", FilterChainBuilder.class);
                    }});

    public FilterChainsBuilder() {
        super(null, allowedComponentTypes, null);
    }

    @Override
    protected FilterChains newChainsInstance(AbstractConfigProducer parent) {
        return new FilterChains(parent);
    }

    @Override
    protected ChainsBuilder<Filter, Chain<Filter>> readChains(
            AbstractConfigProducer ancestor,
            List<Element> allChainsElems, Map<String, ComponentsBuilder.ComponentType> outerComponentTypeByComponentName) {

        return new ChainsBuilder<>(ancestor, allChainsElems, outerComponentTypeByComponentName, chainType2BuilderClass);
    }
}

@@ -158,16 +158,63 @@
 p.xml;%0A%0A
+import com.google.common.collect.ImmutableMap;%0A
 import c
@@ -1125,29 +1125,8 @@
 );%0A%0A
-    //TODO: simplify%0A
     
@@ -1280,196 +1280,23 @@
     
-Collections.unmodifiableMap(%0A                    new LinkedHashMap%3CString, Class%3C? extends DomChainBuilderBase%3C? extends Filter, ? extends Chain%3CFilter%3E%3E%3E%3E() %7B%7B%0A                        put
+ImmutableMap.of
 (%22re
@@ -1325,34 +1325,33 @@
 ainBuilder.class
-);
+,
 %0A               
@@ -1363,12 +1363,12 @@
     
-put(
+    
 %22res
@@ -1411,33 +1411,8 @@
 ss);
-%0A                    %7D%7D);
 %0A%0A  

Use guava for initializing map.