/*
 * 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.sql.planner.assertions;

import com.facebook.presto.Session;
import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.sql.planner.plan.PlanNode;
import com.facebook.presto.sql.planner.plan.WindowNode;
import com.facebook.presto.sql.tree.FunctionCall;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;

import java.util.LinkedList;
import java.util.List;

import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;

final class WindowMatcher
        implements Matcher
{
    private final List<FunctionCall> functionCalls;

    WindowMatcher(List<FunctionCall> functionCalls)
    {
        this.functionCalls = ImmutableList.copyOf(requireNonNull(functionCalls, "functionCalls is null"));
    }

    @Override
    public boolean matches(PlanNode node, Session session, Metadata metadata, ExpressionAliases expressionAliases)
    {
        if (!(node instanceof WindowNode)) {
            return false;
        }

        WindowNode windowNode = (WindowNode) node;
        List<FunctionCall> actualCalls = windowNode.getWindowFunctions().values().stream()
                .map(WindowNode.Function::getFunctionCall)
                .collect(toList());

        if (actualCalls.size() != functionCalls.size()) {
            return false;
        }

        LinkedList<FunctionCall> expectedCalls = new LinkedList<>(functionCalls);

        for (FunctionCall expectedCall : expectedCalls) {
            if (!actualCalls.remove(expectedCall)) {
                // Found an expectedCall not in expectedCalls.
                return false;
            }
        }

        // expectedCalls was missing something in actualCalls.
        return actualCalls.isEmpty();
    }

    @Override
    public String toString()
    {
        return MoreObjects.toStringHelper(this)
                .add("functionCalls", functionCalls)
                .toString();
    }
}

@@ -995,31 +995,23 @@
 l.List;%0A
-%0A
 import 
-static 
 java.uti
@@ -1016,32 +1016,28 @@
 til.
-Objects.requireNonNull
+stream.Collectors
 ;%0A
+%0A
 impo
@@ -1060,32 +1060,30 @@
 til.
-stream.Collectors.toList
+Objects.requireNonNull
 ;%0A%0Af
@@ -1637,16 +1637,22 @@
         
+Linked
 List%3CFun
@@ -1814,15 +1814,47 @@
 ect(
-toList(
+Collectors.toCollection(LinkedList::new
 ));%0A
@@ -1953,35 +1953,29 @@
  %7D%0A%0A        
-LinkedList%3C
+for (
 FunctionCall
@@ -1974,17 +1974,16 @@
 tionCall
-%3E
  expecte
@@ -1991,95 +1991,19 @@
 Call
-s = new LinkedList%3C%3E(functionCalls);%0A%0A        for (FunctionCall expectedCall : expected
+ : function
 Call

Optimize WindowMatcher traversal in DSL tests