package org.auxis.commons.tree.operators;

import org.auxis.commons.tree.Tree;
import org.auxis.commons.tree.TreeBuilder;
import org.auxis.commons.tree.TreeCombiner;
import org.auxis.commons.tree.TreeIndex;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

import static org.auxis.commons.tree.Selector.selector;
import static org.auxis.commons.tree.annotated.Tag.tag;
import static org.auxis.commons.tree.util.TreeSession.wrapAsIndex;

/**
 * Created by tonit on 05/03/15.
 */
@Singleton
public class UnionTreeCombiner implements TreeCombiner
{
    private final Provider<TreeBuilder> treeBuilderProvider;

    @Inject
    public UnionTreeCombiner( Provider<TreeBuilder> builder )
    {
        treeBuilderProvider = builder;
    }

    @Override public Tree combine( Tree left, Tree right )
    {
        TreeBuilder builder = treeBuilderProvider.get().tag( tag ( "UNION" ));
        include( builder, left );
        include( builder, right );
        return builder.seal();
    }

    private void include( TreeBuilder collector, Tree left )
    {

        if ( left.branches().length == 0 ) {
            collector.branch( left );
        }else
        {
            //TreeBuilder subBuilder = collector.branch( left.selector() ).tag( tag ("infix" ) );

            for ( Tree tree : left.branches() )
            {
                include( collector, tree );
            }
        }
    }
}

@@ -70,140 +70,9 @@
 ree.
-Tree;%0Aimport org.auxis.commons.tree.TreeBuilder;%0Aimport org.auxis.commons.tree.TreeCombiner;%0Aimport org.auxis.commons.tree.TreeIndex
+*
 ;%0A%0Ai
@@ -352,36 +352,255 @@
  * C
-reated by tonit on 05/03/15.
+overs merges of similar trees so that combiners work as a system. (see test cases about combiner integrity)%0A * In a sense this should be really coverable by native %7B@link TreeBuilder#branch(tree)%7D .%0A *%0A * @author Toni Menzel (rebaze)%0A * @since 0.3
 %0A */
@@ -985,17 +985,16 @@
 tag( tag
- 
 ( %22UNION
@@ -996,16 +996,17 @@
 UNION%22 )
+ 
 );%0A     
@@ -1217,16 +1217,24 @@
 h == 0 )
+%0A       
  %7B%0A     
@@ -1275,16 +1275,25 @@
        %7D
+%0A        
 else%0A   
@@ -1315,10 +1315,8 @@
     
-//
 Tree
@@ -1362,19 +1362,8 @@
 left
-.selector()
  ).t
@@ -1369,18 +1369,18 @@
 tag( tag
- 
 (
+ 
 %22infix%22 
@@ -1384,17 +1384,33 @@
 x%22 ) );%0A
-%0A
+            /**%0A 
         
@@ -1453,37 +1453,35 @@
 ) )%0A            
+ 
 %7B%0A
-   
              inc
@@ -1486,24 +1486,25 @@
 nclude( 
-collecto
+subBuilde
 r, tree 
@@ -1510,33 +1510,51 @@
  );%0A            
-%7D
+ %7D%0A             **/
 %0A        %7D%0A    %7D

Work in progress unioncombiner. Solution must be just branch(tree). Test to work on still fails (which is what we want).