1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2009 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.squid.api;
import java.util.HashSet;
import java.util.Set;
import org.sonar.graph.Edge;
public class SourceCodeEdge implements Edge<SourceCode> {
private final SourceCode from;
private final SourceCode to;
private final SourceCodeEdgeUsage usage;
private Set<SourceCodeEdge> rootEdges;
private Set<SourceCode> rootFromNodes;
private Set<SourceCode> rootToNodes;
private final int hashcode;
private SourceCodeEdge parent;
public SourceCodeEdge(SourceCode from, SourceCode to, SourceCodeEdgeUsage link) {
this(from, to, link, null);
}
public SourceCodeEdge(SourceCode from, SourceCode to, SourceCodeEdgeUsage usage, SourceCodeEdge rootEdge) {
this.hashcode = from.hashCode() * 31 + to.hashCode() + usage.hashCode(); //NOSONAR even if this basic algorithm could be improved
this.from = from;
this.to = to;
this.usage = usage;
addRootEdge(rootEdge);
}
public SourceCode getFrom() {
return from;
}
public SourceCode getTo() {
return to;
}
public SourceCodeEdgeUsage getUsage() {
return usage;
}
private boolean noRoots() {
return rootEdges == null;
}
public boolean hasAnEdgeFromRootNode(SourceCode rootFromNode) {
if (noRoots()) {
return false;
}
return rootFromNodes.contains(rootFromNode);
}
public boolean hasAnEdgeToRootNode(SourceCode rootToNode) {
if (noRoots()) {
return false;
}
return rootToNodes.contains(rootToNode);
}
public Set<SourceCodeEdge> getRootEdges() {
return rootEdges;
}
public int getNumberOfRootFromNodes() {
if (noRoots()) {
return 0;
}
return rootFromNodes.size();
}
public final void addRootEdge(SourceCodeEdge rootRelationShip) {
if (noRoots()) {
rootEdges = new HashSet<SourceCodeEdge>();
rootFromNodes = new HashSet<SourceCode>();
rootToNodes = new HashSet<SourceCode>();
}
if (rootRelationShip != null) {
rootEdges.add(rootRelationShip);
rootFromNodes.add(rootRelationShip.getFrom());
rootToNodes.add(rootRelationShip.getTo());
rootRelationShip.setParent(this);
}
}
public int getWeight() {
if (noRoots()) {
return 0;
}
return rootEdges.size();
}
public SourceCodeEdge getParent() {
return parent;
}
public SourceCodeEdge setParent(SourceCodeEdge parent) {
this.parent = parent;
return this;
}
@Override
public boolean equals(Object obj) {
if ( !(obj instanceof SourceCodeEdge) || this.hashCode() != obj.hashCode()) {
return false;
}
SourceCodeEdge edge = (SourceCodeEdge) obj;
return from.equals(edge.from) && to.equals(edge.to);
}
@Override
public int hashCode() {
return hashcode;
}
@Override
public String toString() {
return "from : " + from + ", to : " + to;
}
}
|