1 package net.sourceforge.pmd.dfa.report;
2
3 import java.util.ArrayList;
4
5 public abstract class AbstractReportNode {
6 private ArrayList childNodes = new ArrayList();
7 private AbstractReportNode parentNode = null;
8
9
10
11
12
13 private int numberOfViolations;
14
15 /***
16 * Should compare to nodes of the tree.
17 */
18 public abstract boolean equalsNode(AbstractReportNode arg0);
19
20 /***
21 * @return null If there isn't any child.
22 */
23 public AbstractReportNode getFirstChild() {
24 if (this.isLeaf()) {
25 return null;
26 }
27 return (AbstractReportNode) this.childNodes.get(0);
28 }
29
30 /***
31 * @return null If there isn't any sibling.
32 */
33 public AbstractReportNode getNextSibling() {
34 if (this.parentNode == null) {
35 return null;
36 }
37 int index = this.parentNode.getChildIndex(this);
38 if (index < 0) {
39 return null;
40 }
41 if (index >= this.parentNode.childNodes.size() - 1) {
42 return null;
43 }
44 return (AbstractReportNode) this.parentNode.childNodes.get(index + 1);
45 }
46
47 /***
48 * @return index The index of the x-th child of his parent.
49 */
50 private int getChildIndex(AbstractReportNode child) {
51 for (int i = 0; i < this.childNodes.size(); i++) {
52 if (this.childNodes.get(i).equals(child)) {
53 return i;
54 }
55 }
56 return -1;
57 }
58
59 /***
60 * Adds the child in front of any other childs.
61 */
62 public void addFirst(AbstractReportNode child) {
63 this.childNodes.add(0, child);
64 child.parentNode = this;
65 }
66
67 /***
68 * Adds the child at the end.
69 */
70 public void add(AbstractReportNode child) {
71 this.childNodes.add(child);
72 child.parentNode = this;
73 }
74
75 public void addNumberOfViolation(int number) {
76 this.numberOfViolations += number;
77 }
78
79 /***
80 * @return The number of all violations downside the node.
81 */
82 public int getNumberOfViolations() {
83 return numberOfViolations;
84 }
85
86
87
88 public void childrenAccept(ReportVisitor visitor) {
89 for (int i = 0; i < childNodes.size(); i++) {
90 AbstractReportNode node = (AbstractReportNode) childNodes.get(i);
91 node.accept(visitor);
92 }
93 }
94
95 public void accept(ReportVisitor visitor) {
96 visitor.visit(this);
97 }
98
99 public AbstractReportNode getChildAt(int arg0) {
100 if (arg0 >= 0 && arg0 <= this.childNodes.size() - 1) {
101 return (AbstractReportNode) this.childNodes.get(arg0);
102 }
103 return null;
104 }
105
106 public int getChildCount() {
107 return this.childNodes.size();
108 }
109
110 public AbstractReportNode getParent() {
111 return this.parentNode;
112 }
113
114 public boolean isLeaf() {
115 return this.childNodes.isEmpty();
116 }
117
118 }