1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.inline.mojo;
16
17 import org.basepom.inline.transformer.ClassPathResource;
18
19 import java.util.SortedMap;
20 import java.util.StringJoiner;
21 import java.util.TreeMap;
22
23 import com.google.common.base.Strings;
24
25 public final class TreeNode {
26
27 private final String name;
28 private final ClassPathResource classPathResource;
29
30 private final SortedMap<String, TreeNode> children = new TreeMap<>();
31 private boolean written = false;
32
33 public static TreeNode getRootNode() {
34 var rootNode = new TreeNode("", null);
35 rootNode.written = true;
36 return rootNode;
37 }
38
39 public TreeNode(String name, ClassPathResource classPathResource) {
40 this.name = name;
41 this.classPathResource = classPathResource;
42 }
43
44 public void addChild(String childName, ClassPathResource classPathResource) {
45 children.computeIfAbsent(childName, k -> new TreeNode(childName, classPathResource));
46 }
47
48 public TreeNode getChild(String childName) {
49 return children.get(childName);
50 }
51
52 public String getName() {
53 return name;
54 }
55
56 public ClassPathResource getClassPathResource() {
57 return classPathResource;
58 }
59
60 public SortedMap<String, TreeNode> getChildren() {
61 return children;
62 }
63
64 public void write() {
65 written = true;
66 }
67
68 public boolean needsWriting() {
69 return !written;
70 }
71
72 @Override
73 public String toString() {
74 return render(0);
75 }
76
77 private String render(int indent) {
78 var result = new StringBuilder(renderNoChildren(indent));
79 var indentPadding = Strings.repeat(" ", indent);
80 result.append('\n').append(indentPadding).append("children:\n");
81
82 for (TreeNode child : children.values()) {
83 result.append(indentPadding);
84 result.append(child.render(indent + 2));
85 result.append('\n');
86 }
87
88 return result.toString();
89 }
90
91 private String renderNoChildren(int indent) {
92 var result = new StringJoiner(", ", TreeNode.class.getSimpleName() + "[", "]")
93 .add("name='" + name + "'")
94 .add("written=" + written)
95 .add("classPathResource=" + classPathResource);
96
97 return Strings.repeat(" ", indent) + result;
98 }
99 }