/*
 * 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.facebook.presto.tests;

import com.google.common.collect.ImmutableList;

import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

public class QueryTemplate
{
    private final String queryTemplate;
    private final List<Parameter> defaultParameters;

    public QueryTemplate(String queryTemplate, Parameter... parameters)
    {
        for (Parameter parameter : parameters) {
            String queryParameterKey = asQueryParameterKey(parameter.getKey());
            checkArgument(
                    queryTemplate.contains(queryParameterKey),
                    "Query template does not contain: %s", queryParameterKey);
        }

        this.queryTemplate = queryTemplate;
        this.defaultParameters = ImmutableList.copyOf(parameters);
    }

    public String replace(Parameter... parameters)
    {
        String query = queryTemplate;
        for (Parameter parameter : parameters) {
            query = resolve(query, parameter.getKey(), parameter.getValue());
        }
        for (Parameter parameter : defaultParameters) {
            query = resolve(query, parameter.getKey(), parameter.getValue());
        }

        return query;
    }

    private String resolve(String query, String key, String value)
    {
        return query.replaceAll(asQueryParameterKey(key), value);
    }

    private String asQueryParameterKey(String key)
    {
        return "%" + key + "%";
    }

    public static final class Parameter
    {
        private final String key;
        private final String value;

        public Parameter(String key, String value)
        {
            this.key = requireNonNull(key, "key is null");
            this.value = requireNonNull(value, "defaultValue is null");
        }

        public String getValue()
        {
            return value;
        }

        public String getKey()
        {
            return key;
        }

        public Parameter of(String value)
        {
            return new Parameter(key, value);
        }
    }
}

@@ -659,24 +659,51 @@
 a.util.List;
+%0Aimport java.util.Optional;
 %0A%0Aimport sta
@@ -1627,39 +1627,121 @@
 eter
-.getKey(), parameter.getValue()
+);%0A        %7D%0A        for (Parameter parameter : defaultParameters) %7B%0A            query = resolve(query, parameter
 );%0A 
@@ -1741,32 +1741,33 @@
 ter);%0A        %7D%0A
+%0A
         for (Par
@@ -1822,250 +1822,515 @@
     
-query = resolve(query, parameter.getKey(), parameter.getValue());%0A        %7D%0A%0A        return query;%0A    %7D%0A%0A    private String resolve(String query, String key, String value)%0A    %7B%0A        return query.replaceAll(asQueryParameterKey(key), value
+String queryParameterKey = asQueryParameterKey(parameter.getKey());%0A            checkArgument(%0A                    !query.contains(queryParameterKey),%0A                    %22Query template parameters was not given: %25s%22, queryParameterKey);%0A        %7D%0A%0A        return query;%0A    %7D%0A%0A    private String resolve(String query, Parameter parameter)%0A    %7B%0A        return parameter.getValue()%0A                .map(value -%3E query.replaceAll(asQueryParameterKey(parameter.getKey()), value))%0A                .orElse(query
 );%0A 
@@ -2533,22 +2533,32 @@
 e final 
+Optional%3C
 String
+%3E
  value;%0A
@@ -2597,16 +2597,241 @@
  key
-, String
+)%0A        %7B%0A            this(key, Optional.empty());%0A        %7D%0A%0A        public Parameter(String key, String value)%0A        %7B%0A            this(key, Optional.of(value));%0A        %7D%0A%0A        private Parameter(String key, Optional%3CString%3E
  val
@@ -2997,30 +2997,40 @@
      public 
+Optional%3C
 String
+%3E
  getValue()%0A

Make value in QueryTemplate.Parameter to be optional