From 504ca816530efdff06bc202e0432ebd354aec304 Mon Sep 17 00:00:00 2001 From: EVE Date: Tue, 14 Mar 2017 12:07:14 -0400 Subject: Cleanup --- BJC-Utils2/src/main/java/bjc/utils/data/Tree.java | 89 ++++++++++------------- 1 file changed, 40 insertions(+), 49 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/Tree.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java index 6a16491..52414d2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java @@ -1,17 +1,17 @@ package bjc.utils.data; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; +import bjc.utils.funcdata.bst.TreeLinearizationMethod; + import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.UnaryOperator; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.bst.TreeLinearizationMethod; - /** * A node in a homogenous tree. - * + * * @author ben * * @param @@ -19,16 +19,16 @@ import bjc.utils.funcdata.bst.TreeLinearizationMethod; public class Tree implements ITree { private ContainedType data; - private IList> children; - private boolean hasChildren; - private int childCount = 0; + private IList> children; + private boolean hasChildren; + private int childCount = 0; - private int ID; - private static int nextID = 0; + private int ID; + private static int nextID = 0; /** * Create a new leaf node in a tree - * + * * @param leaf * The data to store as a leaf node */ @@ -42,7 +42,7 @@ public class Tree implements ITree { /** * Create a new tree node with the specified children - * + * * @param leaf * The data to hold in this node * @param childrn @@ -58,7 +58,7 @@ public class Tree implements ITree { /** * Create a new tree node with the specified children - * + * * @param leaf * The data to hold in this node * @param childrn @@ -74,7 +74,7 @@ public class Tree implements ITree { children = new FunctionalList<>(); - for (ITree child : childrn) { + for(ITree child : childrn) { children.add(child); childCount++; @@ -83,7 +83,7 @@ public class Tree implements ITree { @Override public void addChild(ITree child) { - if (hasChildren == false) { + if(hasChildren == false) { hasChildren = true; children = new FunctionalList<>(); @@ -109,7 +109,7 @@ public class Tree implements ITree { @Override public ITree flatMapTree(Function> mapper) { - if (hasChildren) { + if(hasChildren) { ITree flatMappedData = mapper.apply(data); children.map((child) -> child.flatMapTree(mapper)) @@ -128,10 +128,10 @@ public class Tree implements ITree { protected NewType internalCollapse(Function leafTransform, Function, NewType>> nodeCollapser) { - if (hasChildren) { + if(hasChildren) { Function, NewType> nodeTransformer = nodeCollapser.apply(data); - IList collapsedChildren = (IList) children.map((child) -> { + IList collapsedChildren = children.map((child) -> { return child.collapse(leafTransform, nodeCollapser, (subTreeVal) -> subTreeVal); }); @@ -142,7 +142,7 @@ public class Tree implements ITree { } protected void internalToString(StringBuilder builder, int indentLevel, boolean initial) { - for (int i = 0; i < indentLevel; i++) { + for(int i = 0; i < indentLevel; i++) { builder.append(">\t"); } @@ -152,7 +152,7 @@ public class Tree implements ITree { builder.append(data == null ? "(null)" : data.toString()); builder.append("\n"); - if (hasChildren) { + if(hasChildren) { children.forEach((child) -> { ((Tree) child).internalToString(builder, indentLevel + 1, false); }); @@ -162,7 +162,7 @@ public class Tree implements ITree { @Override public ITree rebuildTree(Function leafTransformer, Function operatorTransformer) { - if (hasChildren) { + if(hasChildren) { IList> mappedChildren = children.map((child) -> { return child.rebuildTree(leafTransformer, operatorTransformer); }); @@ -175,7 +175,7 @@ public class Tree implements ITree { @Override public void selectiveTransform(Predicate nodePicker, UnaryOperator transformer) { - if (hasChildren) { + if(hasChildren) { children.forEach((child) -> child.selectiveTransform(nodePicker, transformer)); } else { data = transformer.apply(data); @@ -187,11 +187,11 @@ public class Tree implements ITree { UnaryOperator> transformer) { TopDownTransformResult transformResult = transformPicker.apply(data); - switch (transformResult) { + switch(transformResult) { case PASSTHROUGH: ITree result = new Tree<>(data); - if (hasChildren) { + if(hasChildren) { children.forEach((child) -> { result.addChild(child.topDownTransform(transformPicker, transformer)); }); @@ -207,7 +207,7 @@ public class Tree implements ITree { case PUSHDOWN: result = new Tree<>(data); - if (hasChildren) { + if(hasChildren) { children.forEach((child) -> { result.addChild(child.topDownTransform(transformPicker, transformer)); }); @@ -244,9 +244,8 @@ public class Tree implements ITree { @Override public TransformedType transformChild(int childNo, Function, TransformedType> transformer) { - if (childNo < 0 || childNo > (childCount - 1)) { + if(childNo < 0 || childNo > childCount - 1) throw new IllegalArgumentException("Child index #" + childNo + " is invalid"); - } return transformer.apply(children.getByIndex(childNo)); } @@ -258,7 +257,7 @@ public class Tree implements ITree { @Override public ITree transformTree(Function transformer) { - if (hasChildren) { + if(hasChildren) { IList> transformedChildren = children .map((child) -> child.transformTree(transformer)); @@ -270,13 +269,11 @@ public class Tree implements ITree { @Override public void traverse(TreeLinearizationMethod linearizationMethod, Consumer action) { - if (hasChildren) { - switch (linearizationMethod) { + if(hasChildren) { + switch(linearizationMethod) { case INORDER: - if (childCount != 2) { - throw new IllegalArgumentException( - "Can only do in-order traversal for binary trees."); - } + if(childCount != 2) throw new IllegalArgumentException( + "Can only do in-order traversal for binary trees."); children.getByIndex(0).traverse(linearizationMethod, action); @@ -303,32 +300,26 @@ public class Tree implements ITree { } } + @Override public boolean equals(Object other) { - if (!(other instanceof Tree)) - return false; + if(!(other instanceof Tree)) return false; @SuppressWarnings("unchecked") Tree otr = (Tree) other; - if (!otr.data.equals(data)) - return false; + if(!otr.data.equals(data)) return false; - if (children == null && otr.children == null) - return true; + if(children == null && otr.children == null) return true; - if (children == null && otr.children != null) - return false; - if (children != null && otr.children == null) - return false; + if(children == null && otr.children != null) return false; + if(children != null && otr.children == null) return false; - if (children.getSize() != otr.children.getSize()) - return false; + if(children.getSize() != otr.children.getSize()) return false; int childNo = 0; - for (ITree child : children) { - if (!otr.children.getByIndex(childNo).equals(child)) - return false; + for(ITree child : children) { + if(!otr.children.getByIndex(childNo).equals(child)) return false; childNo += 1; } -- cgit v1.2.3