001 package net.sourceforge.pmd.lang.dfa.report;
002
003 import java.util.ArrayList;
004 import java.util.List;
005
006 public abstract class AbstractReportNode {
007 private List<AbstractReportNode> childNodes = new ArrayList<AbstractReportNode>();
008 private AbstractReportNode parentNode = null;
009
010 /*
011 * Number of all RuleViolations down to this node. At the moment it will
012 * only be calculated by running the ReportHTMLPrintVisitor.
013 * */
014 private int numberOfViolations;
015
016 /**
017 * Should compare to nodes of the tree.
018 */
019 public abstract boolean equalsNode(AbstractReportNode arg0);
020
021 /**
022 * @return null If there isn't any child.
023 */
024 public AbstractReportNode getFirstChild() {
025 if (this.isLeaf()) {
026 return null;
027 }
028 return this.childNodes.get(0);
029 }
030
031 /**
032 * @return null If there isn't any sibling.
033 */
034 public AbstractReportNode getNextSibling() {
035 if (this.parentNode == null) {
036 return null;
037 }
038 int index = this.parentNode.getChildIndex(this);
039 if (index < 0) {
040 return null;
041 }
042 if (index >= this.parentNode.childNodes.size() - 1) {
043 return null;
044 }
045 return this.parentNode.childNodes.get(index + 1);
046 }
047
048 /**
049 * @return index The index of the x-th child of his parent.
050 */
051 private int getChildIndex(AbstractReportNode child) {
052 for (int i = 0; i < this.childNodes.size(); i++) {
053 if (this.childNodes.get(i).equals(child)) {
054 return i;
055 }
056 }
057 return -1;
058 }
059
060 /**
061 * Adds the child in front of any other childs.
062 */
063 public void addFirst(AbstractReportNode child) {
064 this.childNodes.add(0, child);
065 child.parentNode = this;
066 }
067
068 /**
069 * Adds the child at the end.
070 */
071 public void add(AbstractReportNode child) {
072 this.childNodes.add(child);
073 child.parentNode = this;
074 }
075
076 public void addNumberOfViolation(int number) {
077 this.numberOfViolations += number;
078 }
079
080 /**
081 * @return The number of all violations downside the node.
082 */
083 public int getNumberOfViolations() {
084 return numberOfViolations;
085 }
086
087 // ----------------------------------------------------------------------------
088 // visitor methods
089 public void childrenAccept(ReportVisitor visitor) {
090 for (int i = 0; i < childNodes.size(); i++) {
091 AbstractReportNode node = childNodes.get(i);
092 node.accept(visitor);
093 }
094 }
095
096 public void accept(ReportVisitor visitor) {
097 visitor.visit(this);
098 }
099
100 public AbstractReportNode getChildAt(int arg0) {
101 if (arg0 >= 0 && arg0 <= this.childNodes.size() - 1) {
102 return this.childNodes.get(arg0);
103 }
104 return null;
105 }
106
107 public int getChildCount() {
108 return this.childNodes.size();
109 }
110
111 public AbstractReportNode getParent() {
112 return this.parentNode;
113 }
114
115 public boolean isLeaf() {
116 return this.childNodes.isEmpty();
117 }
118
119 }
|