From df94066e3af02ff02d5ab4d033a3d603f743234c Mon Sep 17 00:00:00 2001 From: bjculkin Date: Mon, 12 Feb 2018 22:45:04 -0500 Subject: Formatting pass --- base/src/main/java/bjc/utils/data/Tree.java | 92 ++++++++++++++--------------- 1 file changed, 45 insertions(+), 47 deletions(-) (limited to 'base/src/main/java/bjc/utils/data/Tree.java') diff --git a/base/src/main/java/bjc/utils/data/Tree.java b/base/src/main/java/bjc/utils/data/Tree.java index 386153b..6b7e03a 100644 --- a/base/src/main/java/bjc/utils/data/Tree.java +++ b/base/src/main/java/bjc/utils/data/Tree.java @@ -16,7 +16,7 @@ import bjc.utils.functypes.ListFlattener; * @author ben * * @param - * The type contained in the tree. + * The type contained in the tree. */ public class Tree implements ITree { /* The data/label for this node. */ @@ -26,17 +26,16 @@ public class Tree implements ITree { private IList> children; /* Whether this node has children. */ - /* @NOTE - * Why have both this boolean and childCount? Why not just do a - * childCount == 0 - * whenever you'd check hasChildren? + /* + * @NOTE Why have both this boolean and childCount? Why not just do a + * childCount == 0 whenever you'd check hasChildren? */ private boolean hasChildren; /* The number of children this node has. */ - private int childCount = 0; + private int childCount = 0; /* The ID of this node. */ - private int ID; + private int ID; /* The next ID to assign to a node. */ private static int nextID = 0; @@ -44,7 +43,7 @@ public class Tree implements ITree { * Create a new leaf node in a tree. * * @param leaf - * The data to store as a leaf node. + * The data to store as a leaf node. */ public Tree(final ContainedType leaf) { data = leaf; @@ -58,10 +57,10 @@ public class Tree implements ITree { * Create a new tree node with the specified children. * * @param leaf - * The data to hold in this node. + * The data to hold in this node. * * @param childrn - * A list of children for this node. + * A list of children for this node. */ public Tree(final ContainedType leaf, final IList> childrn) { this(leaf); @@ -77,10 +76,10 @@ public class Tree implements ITree { * Create a new tree node with the specified children. * * @param leaf - * The data to hold in this node. + * The data to hold in this node. * * @param childrn - * A list of children for this node. + * A list of children for this node. */ @SafeVarargs public Tree(final ContainedType leaf, final ITree... childrn) { @@ -92,7 +91,7 @@ public class Tree implements ITree { children = new FunctionalList<>(); - for (final ITree child : childrn) { + for(final ITree child : childrn) { children.add(child); childCount++; @@ -101,7 +100,7 @@ public class Tree implements ITree { @Override public void addChild(final ITree child) { - if (hasChildren == false) { + if(hasChildren == false) { hasChildren = true; children = new FunctionalList<>(); @@ -114,7 +113,7 @@ public class Tree implements ITree { @Override public void prependChild(final ITree child) { - if (hasChildren == false) { + if(hasChildren == false) { hasChildren = true; children = new FunctionalList<>(); @@ -127,7 +126,7 @@ public class Tree implements ITree { @Override public void doForChildren(final Consumer> action) { - if (childCount > 0) { + if(childCount > 0) { children.forEach(action); } } @@ -139,11 +138,11 @@ public class Tree implements ITree { @Override public int revFind(final Predicate> childPred) { - if (childCount == 0) { + if(childCount == 0) { return -1; } else { - for (int i = childCount - 1; i >= 0; i--) { - if (childPred.test(getChild(i))) return i; + for(int i = childCount - 1; i >= 0; i--) { + if(childPred.test(getChild(i))) return i; } return -1; @@ -152,10 +151,10 @@ public class Tree implements ITree { @Override public void traverse(final TreeLinearizationMethod linearizationMethod, final Consumer action) { - if (hasChildren) { - switch (linearizationMethod) { + if(hasChildren) { + switch(linearizationMethod) { case INORDER: - if (childCount != 2) { + if(childCount != 2) { final String msg = "Can only do in-order traversal for binary trees."; throw new IllegalArgumentException(msg); @@ -195,7 +194,7 @@ public class Tree implements ITree { @Override public ITree flatMapTree(final Function> mapper) { - if (hasChildren) { + if(hasChildren) { final ITree flatMappedData = mapper.apply(data); final IList> mappedChildren = children @@ -212,14 +211,13 @@ public class Tree implements ITree { /* * Do a collapse of this tree. * - * @NOTE - * Why is this protected? I can't see any good reason someone'd - * want to override it. + * @NOTE Why is this protected? I can't see any good reason someone'd + * want to override it. */ protected NewType internalCollapse(final Function leafTransform, final Function> nodeCollapser) { - if (hasChildren) { + if(hasChildren) { final Function, NewType> nodeTransformer = nodeCollapser.apply(data); final IList collapsedChildren = children.map(child -> { @@ -236,7 +234,7 @@ public class Tree implements ITree { } protected void internalToString(final StringBuilder builder, final int indentLevel, final boolean initial) { - for (int i = 0; i < indentLevel; i++) { + for(int i = 0; i < indentLevel; i++) { builder.append(">\t"); } @@ -246,14 +244,14 @@ public class Tree implements ITree { builder.append(data == null ? "(null)" : data.toString()); builder.append("\n"); - if (hasChildren) { + if(hasChildren) { children.forEach(child -> { - if (child instanceof Tree) { + if(child instanceof Tree) { final Tree kid = (Tree) child; kid.internalToString(builder, indentLevel + 1, false); } else { - for (int i = 0; i < indentLevel + 1; i++) { + for(int i = 0; i < indentLevel + 1; i++) { builder.append(">\t"); } @@ -268,7 +266,7 @@ public class Tree implements ITree { @Override public ITree rebuildTree(final Function leafTransformer, final Function operatorTransformer) { - if (hasChildren) { + if(hasChildren) { final IList> mappedChildren = children.map(child -> { return child.rebuildTree(leafTransformer, operatorTransformer); }); @@ -283,7 +281,7 @@ public class Tree implements ITree { @Override public void selectiveTransform(final Predicate nodePicker, final UnaryOperator transformer) { - if (hasChildren) { + if(hasChildren) { children.forEach(child -> child.selectiveTransform(nodePicker, transformer)); } else { data = transformer.apply(data); @@ -296,11 +294,11 @@ public class Tree implements ITree { final UnaryOperator> transformer) { final TopDownTransformResult transformResult = transformPicker.apply(data); - switch (transformResult) { + switch(transformResult) { case PASSTHROUGH: ITree result = new Tree<>(data); - if (hasChildren) { + if(hasChildren) { children.forEach(child -> { final ITree kid = child.topDownTransform(transformPicker, transformer); @@ -319,7 +317,7 @@ public class Tree implements ITree { case PUSHDOWN: result = new Tree<>(data); - if (hasChildren) { + if(hasChildren) { children.forEach(child -> { final ITree kid = child.topDownTransform(transformPicker, transformer); @@ -351,7 +349,7 @@ public class Tree implements ITree { @Override public TransformedType transformChild(final int childNo, final Function, TransformedType> transformer) { - if (childNo < 0 || childNo > childCount - 1) { + if(childNo < 0 || childNo > childCount - 1) { final String msg = String.format("Child index #%d is invalid", childNo); throw new IllegalArgumentException(msg); @@ -394,21 +392,21 @@ public class Tree implements ITree { @Override public boolean equals(final Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (!(obj instanceof Tree)) return false; + if(this == obj) return true; + if(obj == null) return false; + if(!(obj instanceof Tree)) return false; final Tree other = (Tree) obj; - if (data == null) { - if (other.data != null) return false; - } else if (!data.equals(other.data)) return false; + if(data == null) { + if(other.data != null) return false; + } else if(!data.equals(other.data)) return false; - if (childCount != other.childCount) return false; + if(childCount != other.childCount) return false; - if (children == null) { - if (other.children != null) return false; - } else if (!children.equals(other.children)) return false; + if(children == null) { + if(other.children != null) return false; + } else if(!children.equals(other.children)) return false; return true; } -- cgit v1.2.3