package nl.dvberkel.tree;

import nl.dvberkel.box.BoundingBox;
import nl.dvberkel.layout.Aligner;
import nl.dvberkel.layout.HorizontalAligner;
import nl.dvberkel.layout.VerticalAligner;

import static nl.dvberkel.box.BoundingBox.mergeAll;

public class SvgNode implements SvgTree {
    private final SvgTree left;
    private final SvgTree right;
    private BoundingBox boundingBox;

    public SvgNode(SvgTree left, SvgTree right) {
        if (left == null) { throw new IllegalArgumentException("left should not be null"); }
        if (right == null) { throw new IllegalArgumentException("right should not be null"); }
        this.left = left;
        this.right = right;
    }

    @Override
    public BoundingBox boundingBox(Configuration configuration) {
        if (boundingBox == null) {
            Aligner horizontalAligner = new HorizontalAligner();
            BoundingBox[] alignedSubTrees = horizontalAligner.align(left.boundingBox(configuration), right.boundingBox(configuration));
            BoundingBox mergedBoundingBox = mergeAll(alignedSubTrees);
            Aligner verticalAligner = new VerticalAligner();
            int size = 2 * (configuration.radius + configuration.padding);
            BoundingBox[] aligned = verticalAligner.align(new BoundingBox(0, 0, size, size), mergedBoundingBox);
            boundingBox = mergeAll(aligned);
        }
        return boundingBox;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        SvgNode svgNode = (SvgNode) o;

        if (!left.equals(svgNode.left)) return false;
        return right.equals(svgNode.right);

    }

    @Override
    public int hashCode() {
        int result = left.hashCode();
        result = 31 * result + right.hashCode();
        return result;
    }
}

@@ -275,16 +275,170 @@
 gTree %7B%0A
+    private static final Aligner HORIZONTAL_ALIGNER = new HorizontalAligner();%0A    private static final Aligner VERTICAL_ALIGNER = new VerticalAligner();%0A
     priv
@@ -950,73 +950,8 @@
 ) %7B%0A
-            Aligner horizontalAligner = new HorizontalAligner();%0A
     
@@ -994,25 +994,26 @@
 s = 
-horizontalAligner
+HORIZONTAL_ALIGNER
 .ali
@@ -1158,69 +1158,8 @@
 s);%0A
-            Aligner verticalAligner = new VerticalAligner();%0A
     
@@ -1269,23 +1269,24 @@
 d = 
-verticalAligner
+VERTICAL_ALIGNER
 .ali

Create Aligner constants