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:
Edwin Kempin
2011-12-27 15:41:47 +01:00
parent 690c5161b9
commit 22cdc62569

View File

@@ -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);
}
}