/*
 * 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.metadata;

import com.facebook.presto.operator.scalar.annotations.ScalarFromAnnotationsParser;
import com.facebook.presto.operator.window.ReflectionWindowFunctionSupplier;
import com.facebook.presto.operator.window.SqlWindowFunction;
import com.facebook.presto.operator.window.WindowAnnotationsParser;
import com.facebook.presto.operator.window.WindowFunctionSupplier;
import com.facebook.presto.spi.function.ValueWindowFunction;
import com.facebook.presto.spi.function.WindowFunction;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.spi.type.TypeSignature;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.facebook.presto.metadata.FunctionKind.WINDOW;
import static com.facebook.presto.metadata.Signature.typeVariable;
import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
import static java.util.Objects.requireNonNull;

public class FunctionListBuilder
{
    private final List<SqlFunction> functions = new ArrayList<>();

    public FunctionListBuilder window(String name, Type returnType, List<? extends Type> argumentTypes, Class<? extends WindowFunction> functionClass)
    {
        WindowFunctionSupplier windowFunctionSupplier = new ReflectionWindowFunctionSupplier<>(
                new Signature(name, WINDOW, returnType.getTypeSignature(), Lists.transform(ImmutableList.copyOf(argumentTypes), Type::getTypeSignature)),
                functionClass);

        functions.add(new SqlWindowFunction(windowFunctionSupplier));
        return this;
    }

    public FunctionListBuilder window(String name, Class<? extends ValueWindowFunction> clazz, String typeVariable, String... argumentTypes)
    {
        Signature signature = new Signature(
                name,
                WINDOW,
                ImmutableList.of(typeVariable(typeVariable)),
                ImmutableList.of(),
                parseTypeSignature(typeVariable),
                Arrays.asList(argumentTypes).stream().map(TypeSignature::parseTypeSignature).collect(toImmutableList()),
                false);
        functions.add(new SqlWindowFunction(new ReflectionWindowFunctionSupplier<>(signature, clazz)));
        return this;
    }

    public FunctionListBuilder window(Class<? extends WindowFunction> clazz)
    {
        functions.addAll(WindowAnnotationsParser.parseFunctionDefinition(clazz));
        return this;
    }

    public FunctionListBuilder aggregate(Class<?> aggregationDefinition)
    {
        functions.addAll(SqlAggregationFunction.createByAnnotations(aggregationDefinition));
        return this;
    }

    public FunctionListBuilder scalar(Class<?> clazz)
    {
        functions.addAll(ScalarFromAnnotationsParser.parseFunctionDefinition(clazz));
        return this;
    }

    public FunctionListBuilder scalars(Class<?> clazz)
    {
        functions.addAll(ScalarFromAnnotationsParser.parseFunctionDefinitions(clazz));
        return this;
    }

    public FunctionListBuilder functions(SqlFunction... sqlFunctions)
    {
        for (SqlFunction sqlFunction : sqlFunctions) {
            function(sqlFunction);
        }
        return this;
    }

    public FunctionListBuilder function(SqlFunction sqlFunction)
    {
        requireNonNull(sqlFunction, "parametricFunction is null");
        functions.add(sqlFunction);
        return this;
    }

    public List<SqlFunction> getFunctions()
    {
        return ImmutableList.copyOf(functions);
    }
}

@@ -725,235 +725,29 @@
 dow.
-ReflectionWindowFunctionSupplier;%0Aimport com.facebook.presto.operator.window.SqlWindowFunction;%0Aimport com.facebook.presto.operator.window.WindowAnnotationsParser;%0Aimport com.facebook.presto.operator.window.WindowFunctionSuppli
+WindowAnnotationsPars
 er;%0A
@@ -786,21 +786,16 @@
 unction.
-Value
 WindowFu
@@ -817,1947 +817,248 @@
 com.
-facebook.presto.spi.function.WindowFunction;%0Aimport com.facebook.presto.spi.type.Type;%0Aimport com.facebook.presto.spi.type.TypeSignature;%0Aimport com.google.common.collect.ImmutableList;%0Aimport com.google.common.collect.Lists;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0Aimport static com.facebook.presto.metadata.FunctionKind.WINDOW;%0Aimport static com.facebook.presto.metadata.Signature.typeVariable;%0Aimport static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;%0Aimport static com.facebook.presto.util.ImmutableCollectors.toImmutableList;%0Aimport static java.util.Objects.requireNonNull;%0A%0Apublic class FunctionListBuilder%0A%7B%0A    private final List%3CSqlFunction%3E functions = new ArrayList%3C%3E();%0A%0A    public FunctionListBuilder window(String name, Type returnType, List%3C? extends Type%3E argumentTypes, Class%3C? extends WindowFunction%3E functionClass)%0A    %7B%0A        WindowFunctionSupplier windowFunctionSupplier = new ReflectionWindowFunctionSupplier%3C%3E(%0A                new Signature(name, WINDOW, returnType.getTypeSignature(), Lists.transform(ImmutableList.copyOf(argumentTypes), Type::getTypeSignature)),%0A                functionClass);%0A%0A        functions.add(new SqlWindowFunction(windowFunctionSupplier));%0A        return this;%0A    %7D%0A%0A    public FunctionListBuilder window(String name, Class%3C? extends ValueWindowFunction%3E clazz, String typeVariable, String... argumentTypes)%0A    %7B%0A        Signature signature = new Signature(%0A                name,%0A                WINDOW,%0A                ImmutableList.of(typeVariable(typeVariable)),%0A                ImmutableList.of(),%0A                parseTypeSignature(typeVariable),%0A                Arrays.asList(argumentTypes).stream().map(TypeSignature::parseTypeSignature).collect(toImmutableList()),%0A                false);%0A        functions.add(new SqlWindowFunction(new ReflectionWindowFunctionSupplier%3C%3E(signature, clazz)));%0A        return this;%0A    %7D
+google.common.collect.ImmutableList;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport static java.util.Objects.requireNonNull;%0A%0Apublic class FunctionListBuilder%0A%7B%0A    private final List%3CSqlFunction%3E functions = new ArrayList%3C%3E();
 %0A%0A  

Remove unused methods from FunctionListBuilder