package org.zalando.riptide;

/*
 * ⁣​
 * Riptide: Core
 * ⁣⁣
 * Copyright (C) 2015 - 2016 Zalando SE
 * ⁣⁣
 * 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.
 * ​⁣
 */

import com.google.common.collect.ImmutableList;
import org.springframework.http.client.AsyncClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

public final class RestBuilder {

    // package private so we can trick code coverage
    static class Converters {
        private static final ImmutableList<HttpMessageConverter<?>> DEFAULT =
                ImmutableList.copyOf(new RestTemplate().getMessageConverters());
    }

    private AsyncClientHttpRequestFactory requestFactory;
    private final List<HttpMessageConverter<?>> converters = new ArrayList<>();
    private String baseUrl;

    RestBuilder() {

    }

    public RestBuilder requestFactory(final AsyncClientHttpRequestFactory requestFactory) {
        this.requestFactory = requestFactory;
        return this;
    }

    public RestBuilder defaultConverters() {
        return converters(Converters.DEFAULT);
    }

    public RestBuilder converters(final Iterable<HttpMessageConverter<?>> converters) {
        converters.forEach(this::converter);
        return this;
    }

    public RestBuilder converter(final HttpMessageConverter<?> converter) {
        this.converters.add(converter);
        return this;
    }

    public RestBuilder baseUrl(final String baseUrl) {
        this.baseUrl = baseUrl;
        return this;
    }

    public RestBuilder configure(final RestConfigurer configurer) {
        configurer.configure(this);
        return this;
    }

    public static RestConfigurer simpleRequestFactory(final ExecutorService executor) {
        return builder -> {
            final SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            factory.setTaskExecutor(new ConcurrentTaskExecutor(executor));
            builder.requestFactory(factory);
        };
    }

    public Rest build() {
        return new Rest(requestFactory, converters(), baseUrl);
    }

    private List<HttpMessageConverter<?>> converters() {
        return converters.isEmpty() ? Converters.DEFAULT : converters;
    }

}

@@ -23,16 +23,444 @@
 ptide;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.concurrent.ExecutorService;%0A%0Aimport org.springframework.http.client.AsyncClientHttpRequestFactory;%0Aimport org.springframework.http.client.SimpleClientHttpRequestFactory;%0Aimport org.springframework.http.converter.HttpMessageConverter;%0Aimport org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;%0Aimport org.springframework.web.client.RestTemplate;%0A%0A
 /*%0A * %E2%81%A3%E2%80%8B
@@ -1150,435 +1150,8 @@
 st;%0A
-import org.springframework.http.client.AsyncClientHttpRequestFactory;%0Aimport org.springframework.http.client.SimpleClientHttpRequestFactory;%0Aimport org.springframework.http.converter.HttpMessageConverter;%0Aimport org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;%0Aimport org.springframework.web.client.RestTemplate;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.concurrent.ExecutorService;%0A
 %0Apub
@@ -1177,16 +1177,37 @@
 tBuilder
+ implements Cloneable
  %7B%0A%0A    
@@ -2806,32 +2806,191 @@
       %7D;%0A    %7D%0A%0A
+    @Override%0A    public RestBuilder clone() %7B%0A        return new RestBuilder().requestFactory(requestFactory).converters(converters).baseUrl(baseUrl);%0A    %7D%0A%0A
     public Rest 

make restbuilder cloneable to allow templating for multiple instances.