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

import com.facebook.presto.sql.tree.DefaultTraversalVisitor;
import com.facebook.presto.sql.tree.Node;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.Spliterators;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class AstUtils
{
    public static boolean nodeContains(Node node, Node subNode)
    {
        return new DefaultTraversalVisitor<Boolean, AtomicBoolean>()
        {
            @Override
            public Boolean process(Node node, AtomicBoolean findResultHolder)
            {
                if (!findResultHolder.get()) {
                    if (node == subNode) {
                        findResultHolder.set(true);
                    }
                    else {
                        super.process(node, findResultHolder);
                    }
                }
                return findResultHolder.get();
            }
        }.process(node, new AtomicBoolean(false));
    }

    public static Stream<Node> preOrder(Node node)
    {
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new PreOrderIterator(node), 0), false);
    }

    private static final class PreOrderIterator
            implements Iterator<Node>
    {
        private final Deque<Node> remaining = new ArrayDeque<>();

        public PreOrderIterator(Node node)
        {
            remaining.push(node);
        }

        @Override
        public boolean hasNext()
        {
            return remaining.size() > 0;
        }

        @Override
        public Node next()
        {
            Node node = remaining.pop();
            node.getChildren().forEach(remaining::push);
            return node;
        }
    }

    private AstUtils() {}
}

@@ -598,69 +598,8 @@
 l;%0A%0A
-import com.facebook.presto.sql.tree.DefaultTraversalVisitor;%0A
 impo
@@ -636,16 +636,16 @@
 e.Node;%0A
+
 %0Aimport 
@@ -752,58 +752,8 @@
 rs;%0A
-import java.util.concurrent.atomic.AtomicBoolean;%0A
 impo
@@ -820,16 +820,65 @@
 pport;%0A%0A
+import static java.util.Objects.requireNonNull;%0A%0A
 public c
@@ -977,542 +977,135 @@
   re
-turn new DefaultTraversalVisitor%3CBoolean, AtomicBoolean%3E()%0A        %7B%0A            @Override%0A            public Boolean process(Node node, AtomicBoolean findResultHolder)%0A            %7B%0A                if (!findResultHolder.get()) %7B%0A                    if (node == subNode) %7B%0A                        findResultHolder.set(true);%0A                    %7D%0A                    else %7B%0A                        super.process(node, findResultHolder);%0A                    %7D%0A                %7D%0A                return findResultHolder.get();%0A  
+quireNonNull(node, %22node is null%22);%0A        requireNonNull(subNode, %22subNode is null%22);%0A%0A        return preOrder(node)%0A
         
   %7D%0A
@@ -1104,20 +1104,16 @@
     
-  %7D%0A
         
 %7D.pr
@@ -1112,48 +1112,51 @@
     
-%7D.process(node, new AtomicBoolean(false)
+.anyMatch(childNode -%3E childNode == subNode
 );%0A 

Simplify AstUtils.nodeContains