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