|
17 | 17 | package de.danielbechler.diff.visitor; |
18 | 18 |
|
19 | 19 | import de.danielbechler.diff.node.*; |
| 20 | +import de.danielbechler.util.*; |
20 | 21 |
|
21 | 22 | import java.lang.Override; |
22 | 23 | import java.lang.String; |
23 | | -import java.lang.StringBuilder; |
| 24 | +import java.lang.SuppressWarnings; |
24 | 25 |
|
25 | | -/** @author Daniel Bechler */ |
26 | | -// TODO The name of this class is confusing. Should be renamed in a future version |
27 | | -public class ToStringPrintingVisitor implements Node.Visitor |
| 26 | +/** |
| 27 | + * Prints the hierarchy of the object graph in a human-readable form. |
| 28 | + * |
| 29 | + * @author Daniel Bechler |
| 30 | + */ |
| 31 | +public class NodeHierarchyVisitor implements Node.Visitor |
28 | 32 | { |
29 | | - private int depth; |
30 | | - |
31 | | - public ToStringPrintingVisitor() |
32 | | - { |
33 | | - } |
| 33 | + public static final int UNLIMITED = -1; |
34 | 34 |
|
35 | | - public ToStringPrintingVisitor(final int depth) |
36 | | - { |
37 | | - this.depth = depth; |
38 | | - } |
| 35 | + private final int maxDepth; |
39 | 36 |
|
40 | | - public int getDepth() |
| 37 | + @SuppressWarnings({"UnusedDeclaration"}) |
| 38 | + public NodeHierarchyVisitor() |
41 | 39 | { |
42 | | - return depth; |
| 40 | + this(UNLIMITED); |
43 | 41 | } |
44 | 42 |
|
45 | | - public void setDepth(final int depth) |
| 43 | + public NodeHierarchyVisitor(final int maxDepth) |
46 | 44 | { |
47 | | - this.depth = depth; |
| 45 | + this.maxDepth = maxDepth; |
48 | 46 | } |
49 | 47 |
|
50 | 48 | @Override |
51 | 49 | public void accept(final Node node, final Visit visit) |
52 | 50 | { |
53 | | - if (depth > 0) |
| 51 | + if (maxDepth == 0) |
| 52 | + { |
| 53 | + visit.stop(); |
| 54 | + } |
| 55 | + final int currentLevel = calculateDepth(node); |
| 56 | + if (maxDepth > 0) |
54 | 57 | { |
55 | | - if (calculateDepth(node) <= depth) |
| 58 | + if (currentLevel <= maxDepth) |
56 | 59 | { |
57 | | - System.out.println(toIndentedString(node)); |
| 60 | + print(node, currentLevel); |
58 | 61 | } |
59 | 62 | else |
60 | 63 | { |
61 | 64 | visit.dontGoDeeper(); |
62 | 65 | } |
63 | 66 | } |
| 67 | + else if (maxDepth < 0) |
| 68 | + { |
| 69 | + print(node, currentLevel); |
| 70 | + } |
64 | 71 | } |
65 | 72 |
|
66 | | - private static String toIndentedString(final Node node) |
| 73 | + protected void print(final Node node, final int level) |
67 | 74 | { |
68 | | - final int level = calculateDepth(node); |
69 | | - final StringBuilder sb = new StringBuilder(); |
70 | | - for (int i = 0; i < level; i++) |
71 | | - { |
72 | | - sb.append(" "); |
73 | | - } |
74 | | - sb.append(node.toString()); |
75 | | - return sb.toString(); |
| 75 | + final String nodeAsString = node.toString(); |
| 76 | + final String indentedNodeString = Strings.indent(level, nodeAsString); |
| 77 | + print(indentedNodeString); |
| 78 | + } |
| 79 | + |
| 80 | + @SuppressWarnings({"MethodMayBeStatic"}) |
| 81 | + protected void print(final String text) |
| 82 | + { |
| 83 | + System.out.println(text); |
76 | 84 | } |
77 | 85 |
|
78 | 86 | private static int calculateDepth(final Node node) |
|
0 commit comments