From 889fac2bdf993dc86f64a8893c0260fdcf848acb Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 10 Apr 2017 16:40:33 -0400 Subject: Cleanup --- BJC-Utils2/src/main/java/bjc/utils/data/Tree.java | 142 +++++++++++----------- 1 file changed, 69 insertions(+), 73 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 0dc96eb..a52f699 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java @@ -1,15 +1,15 @@ package bjc.utils.data; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; -import bjc.utils.funcdata.bst.TreeLinearizationMethod; -import bjc.utils.functypes.ListFlattener; - 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; +import bjc.utils.functypes.ListFlattener; + /** * A node in a homogeneous tree. * @@ -33,7 +33,7 @@ public class Tree implements ITree { * @param leaf * The data to store as a leaf node. */ - public Tree(ContainedType leaf) { + public Tree(final ContainedType leaf) { data = leaf; hasChildren = false; @@ -46,11 +46,11 @@ public class Tree implements ITree { * * @param leaf * The data to hold in this node. - * + * * @param childrn * A list of children for this node. */ - public Tree(ContainedType leaf, IList> childrn) { + public Tree(final ContainedType leaf, final IList> childrn) { this(leaf); hasChildren = true; @@ -65,12 +65,12 @@ public class Tree implements ITree { * * @param leaf * The data to hold in this node. - * + * * @param childrn * A list of children for this node. */ @SafeVarargs - public Tree(ContainedType leaf, ITree... childrn) { + public Tree(final ContainedType leaf, final ITree... childrn) { this(leaf); hasChildren = true; @@ -79,7 +79,7 @@ public class Tree implements ITree { children = new FunctionalList<>(); - for (ITree child : childrn) { + for (final ITree child : childrn) { children.add(child); childCount++; @@ -87,7 +87,7 @@ public class Tree implements ITree { } @Override - public void addChild(ITree child) { + public void addChild(final ITree child) { if (hasChildren == false) { hasChildren = true; @@ -100,7 +100,7 @@ public class Tree implements ITree { } @Override - public void prependChild(ITree child) { + public void prependChild(final ITree child) { if (hasChildren == false) { hasChildren = true; @@ -113,7 +113,7 @@ public class Tree implements ITree { } @Override - public void doForChildren(Consumer> action) { + public void doForChildren(final Consumer> action) { if (childCount > 0) { children.forEach(action); } @@ -125,14 +125,12 @@ public class Tree implements ITree { } @Override - public int revFind(Predicate> childPred) { - if (childCount == 0) { + public int revFind(final Predicate> childPred) { + if (childCount == 0) return -1; - } else { + else { for (int i = childCount - 1; i >= 0; i--) { - if (childPred.test(getChild(i))) { - return i; - } + if (childPred.test(getChild(i))) return i; } } @@ -140,12 +138,12 @@ public class Tree implements ITree { } @Override - public void traverse(TreeLinearizationMethod linearizationMethod, Consumer action) { + public void traverse(final TreeLinearizationMethod linearizationMethod, final Consumer action) { if (hasChildren) { switch (linearizationMethod) { case INORDER: if (childCount != 2) { - String msg = "Can only do in-order traversal for binary trees."; + final String msg = "Can only do in-order traversal for binary trees."; throw new IllegalArgumentException(msg); } @@ -176,18 +174,19 @@ public class Tree implements ITree { } @Override - public ReturnedType collapse(Function leafTransform, - Function> nodeCollapser, - Function resultTransformer) { + public ReturnedType collapse(final Function leafTransform, + final Function> nodeCollapser, + final Function resultTransformer) { return resultTransformer.apply(internalCollapse(leafTransform, nodeCollapser)); } @Override - public ITree flatMapTree(Function> mapper) { + public ITree flatMapTree(final Function> mapper) { if (hasChildren) { - ITree flatMappedData = mapper.apply(data); + final ITree flatMappedData = mapper.apply(data); - IList> mappedChildren = children.map(child -> child.flatMapTree(mapper)); + final IList> mappedChildren = children + .map(child -> child.flatMapTree(mapper)); mappedChildren.forEach(child -> flatMappedData.addChild(child)); @@ -197,13 +196,13 @@ public class Tree implements ITree { return mapper.apply(data); } - protected NewType internalCollapse(Function leafTransform, - Function> nodeCollapser) { + protected NewType internalCollapse(final Function leafTransform, + final Function> nodeCollapser) { if (hasChildren) { - Function, NewType> nodeTransformer = nodeCollapser.apply(data); + final Function, NewType> nodeTransformer = nodeCollapser.apply(data); - IList collapsedChildren = children.map(child -> { - NewType collapsed = child.collapse(leafTransform, nodeCollapser, + final IList collapsedChildren = children.map(child -> { + final NewType collapsed = child.collapse(leafTransform, nodeCollapser, subTreeVal -> subTreeVal); return collapsed; @@ -215,7 +214,7 @@ public class Tree implements ITree { return leafTransform.apply(data); } - protected void internalToString(StringBuilder builder, int indentLevel, boolean initial) { + protected void internalToString(final StringBuilder builder, final int indentLevel, final boolean initial) { for (int i = 0; i < indentLevel; i++) { builder.append(">\t"); } @@ -229,7 +228,7 @@ public class Tree implements ITree { if (hasChildren) { children.forEach(child -> { if (child instanceof Tree) { - Tree kid = (Tree) child; + final Tree kid = (Tree) child; kid.internalToString(builder, indentLevel + 1, false); } else { @@ -244,10 +243,10 @@ public class Tree implements ITree { } @Override - public ITree rebuildTree(Function leafTransformer, - Function operatorTransformer) { + public ITree rebuildTree(final Function leafTransformer, + final Function operatorTransformer) { if (hasChildren) { - IList> mappedChildren = children.map(child -> { + final IList> mappedChildren = children.map(child -> { return child.rebuildTree(leafTransformer, operatorTransformer); }); @@ -258,7 +257,8 @@ public class Tree implements ITree { } @Override - public void selectiveTransform(Predicate nodePicker, UnaryOperator transformer) { + public void selectiveTransform(final Predicate nodePicker, + final UnaryOperator transformer) { if (hasChildren) { children.forEach(child -> child.selectiveTransform(nodePicker, transformer)); } else { @@ -267,9 +267,10 @@ public class Tree implements ITree { } @Override - public ITree topDownTransform(Function transformPicker, - UnaryOperator> transformer) { - TopDownTransformResult transformResult = transformPicker.apply(data); + public ITree topDownTransform( + final Function transformPicker, + final UnaryOperator> transformer) { + final TopDownTransformResult transformResult = transformPicker.apply(data); switch (transformResult) { case PASSTHROUGH: @@ -277,7 +278,8 @@ public class Tree implements ITree { if (hasChildren) { children.forEach(child -> { - ITree kid = child.topDownTransform(transformPicker, transformer); + final ITree kid = child.topDownTransform(transformPicker, + transformer); result.addChild(kid); }); @@ -295,7 +297,8 @@ public class Tree implements ITree { if (hasChildren) { children.forEach(child -> { - ITree kid = child.topDownTransform(transformPicker, transformer); + final ITree kid = child.topDownTransform(transformPicker, + transformer); result.addChild(kid); }); @@ -303,40 +306,41 @@ public class Tree implements ITree { return transformer.apply(result); case PULLUP: - ITree intermediateResult = transformer.apply(this); + final ITree intermediateResult = transformer.apply(this); result = new Tree<>(intermediateResult.getHead()); intermediateResult.doForChildren(child -> { - ITree kid = child.topDownTransform(transformPicker, transformer); + final ITree kid = child.topDownTransform(transformPicker, transformer); result.addChild(kid); }); return result; default: - String msg = String.format("Recieved unknown transform result %s", transformResult); + final String msg = String.format("Recieved unknown transform result %s", transformResult); throw new IllegalArgumentException(msg); } } @Override - public TransformedType transformChild(int childNo, - Function, TransformedType> transformer) { + public TransformedType transformChild(final int childNo, + final Function, TransformedType> transformer) { if (childNo < 0 || childNo > childCount - 1) { - String msg = String.format("Child index #%d is invalid", childNo); + final String msg = String.format("Child index #%d is invalid", childNo); throw new IllegalArgumentException(msg); } - ITree selectedKid = children.getByIndex(childNo); + final ITree selectedKid = children.getByIndex(childNo); return transformer.apply(selectedKid); } @Override - public TransformedType transformHead(Function transformer) { + public TransformedType transformHead( + final Function transformer) { return transformer.apply(data); } @@ -346,15 +350,15 @@ public class Tree implements ITree { int result = 1; result = prime * result + childCount; - result = prime * result + ((children == null) ? 0 : children.hashCode()); - result = prime * result + ((data == null) ? 0 : data.hashCode()); + result = prime * result + (children == null ? 0 : children.hashCode()); + result = prime * result + (data == null ? 0 : data.hashCode()); return result; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); internalToString(builder, 1, true); @@ -364,30 +368,22 @@ public class Tree implements ITree { } @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof Tree)) - return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof Tree)) return false; - Tree other = (Tree) obj; + final Tree other = (Tree) obj; if (data == null) { - if (other.data != null) - return false; - } else if (!data.equals(other.data)) - return false; + 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 (other.children != null) return false; + } else if (!children.equals(other.children)) return false; return true; } -- cgit v1.2.3