Detection of last node in TreeFormatter should not rely on object identity
If the nodes implement the equals() method the detection of the last node may be inaccurate (e.g. if the node set contains two nodes for which equals() returns true and one of them is the last one in the sorted set). In this case a wrong node prefix would be printed out. With this change the last node is now detected by counting the nodes so that there is no dependency on the object identity anymore. Change-Id: I533610780ac58d79e879475e9dc9f7ef7c5a04c2 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
@@ -40,8 +40,9 @@ public class TreeFormatter {
|
||||
printTree(rootNodes.first());
|
||||
} else {
|
||||
currentTabSeparator = DEFAULT_TAB_SEPARATOR;
|
||||
int i = 0;
|
||||
for (final TreeNode rootNode : rootNodes) {
|
||||
final boolean isLastRoot = rootNodes.last().equals(rootNode);
|
||||
final boolean isLastRoot = ++i == rootNodes.size();
|
||||
if (isLastRoot) {
|
||||
currentTabSeparator = " ";
|
||||
}
|
||||
@@ -58,8 +59,9 @@ public class TreeFormatter {
|
||||
final boolean isLast) {
|
||||
printNode(node, level, isLast);
|
||||
final SortedSet<? extends TreeNode> childNodes = node.getChildren();
|
||||
int i = 0;
|
||||
for (final TreeNode childNode : childNodes) {
|
||||
final boolean isLastChild = childNodes.last().equals(childNode);
|
||||
final boolean isLastChild = ++i == childNodes.size();
|
||||
printTree(childNode, level + 1, isLastChild);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user