package com.facebook.presto.sql.planner;

import com.facebook.presto.sql.analyzer.Symbol;
import com.facebook.presto.sql.analyzer.SymbolAllocator;
import com.facebook.presto.sql.planner.plan.PlanFragmentId;
import com.facebook.presto.sql.planner.plan.PlanNode;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static com.google.common.base.Predicates.in;

public class SubPlanBuilder
{
    private final PlanFragmentId id;
    private PlanNode root;
    private boolean isPartitioned;
    private List<SubPlan> children = new ArrayList<>();

    private final SymbolAllocator allocator;

    public SubPlanBuilder(PlanFragmentId id, SymbolAllocator allocator, PlanNode root)
    {
        Preconditions.checkNotNull(id, "id is null");
        Preconditions.checkNotNull(allocator, "allocator is null");
        Preconditions.checkNotNull(root, "root is null");

        this.allocator = allocator;
        this.id = id;
        this.root = root;
    }

    public PlanFragmentId getId()
    {
        return id;
    }

    public SubPlanBuilder setRoot(PlanNode root)
    {
        Preconditions.checkNotNull(root, "root is null");
        this.root = root;
        return this;
    }

    public SubPlanBuilder setPartitioned(boolean partitioned)
    {
        isPartitioned = partitioned;
        return this;
    }

    public PlanNode getRoot()
    {
        return root;
    }

    public boolean isPartitioned()
    {
        return isPartitioned;
    }

    public SubPlanBuilder setChildren(Iterable<SubPlan> children)
    {
        this.children = Lists.newArrayList(children);
        return this;
    }

    public SubPlanBuilder addChild(SubPlan child)
    {
        this.children.add(child);
        return this;
    }

    public List<SubPlan> getChildren()
    {
        return children;
    }

    public SubPlan build()
    {
        Set<Symbol> dependencies = SymbolExtractor.extract(root);

        PlanFragment fragment = new PlanFragment(id, isPartitioned, Maps.filterKeys(allocator.getTypes(), in(dependencies)), root);

        return new SubPlan(fragment, children);
    }
}

@@ -1161,32 +1161,96 @@
 turn id;%0A    %7D%0A%0A
+    public PlanNode getRoot()%0A    %7B%0A        return root;%0A    %7D%0A%0A
     public SubPl
@@ -1392,32 +1392,110 @@
 rn this;%0A    %7D%0A%0A
+    public boolean isPartitioned()%0A    %7B%0A        return isPartitioned;%0A    %7D%0A%0A
     public SubPl
@@ -1626,93 +1626,33 @@
 lic 
-PlanNode getRoot()%0A    %7B%0A        return root;%0A    %7D%0A%0A    public boolean isPartitioned
+List%3CSubPlan%3E getChildren
 ()%0A 
@@ -1667,37 +1667,32 @@
      return 
-isPartitioned
+children
 ;%0A    %7D%0A%0A   
@@ -1964,85 +1964,8 @@
  %7D%0A%0A
-    public List%3CSubPlan%3E getChildren()%0A    %7B%0A        return children;%0A    %7D%0A%0A
     

Reorganize methods