| 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 | |
package net.sourceforge.cobertura.coveragedata; |
| 26 | |
|
| 27 | |
import java.util.Iterator; |
| 28 | |
import java.util.SortedSet; |
| 29 | |
import java.util.TreeSet; |
| 30 | |
|
| 31 | |
import net.sourceforge.cobertura.util.StringUtil; |
| 32 | |
|
| 33 | |
public class SourceFileData extends CoverageDataContainer |
| 34 | |
implements Comparable, HasBeenInstrumented |
| 35 | |
{ |
| 36 | |
|
| 37 | |
private static final long serialVersionUID = 3; |
| 38 | |
|
| 39 | |
private String name; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public SourceFileData(String name) |
| 45 | |
{ |
| 46 | |
if (name == null) |
| 47 | |
throw new IllegalArgumentException( |
| 48 | |
"Source file name must be specified."); |
| 49 | |
this.name = name; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public synchronized void addClassData(ClassData classData) |
| 53 | |
{ |
| 54 | |
if (children.containsKey(classData.getBaseName())) |
| 55 | |
throw new IllegalArgumentException("Source file " + this.name |
| 56 | |
+ " already contains a class with the name " |
| 57 | |
+ classData.getBaseName()); |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
children.put(classData.getBaseName(), classData); |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
public int compareTo(Object o) |
| 68 | |
{ |
| 69 | |
if (!o.getClass().equals(SourceFileData.class)) |
| 70 | |
return Integer.MAX_VALUE; |
| 71 | |
return this.name.compareTo(((SourceFileData)o).name); |
| 72 | |
} |
| 73 | |
|
| 74 | |
public boolean contains(String name) |
| 75 | |
{ |
| 76 | |
return this.children.containsKey(name); |
| 77 | |
} |
| 78 | |
|
| 79 | |
public boolean containsInstrumentationInfo() |
| 80 | |
{ |
| 81 | |
|
| 82 | |
|
| 83 | |
Iterator iter = this.children.values().iterator(); |
| 84 | |
while (iter.hasNext()) |
| 85 | |
{ |
| 86 | |
ClassData classData = (ClassData)iter.next(); |
| 87 | |
if (!classData.containsInstrumentationInfo()) |
| 88 | |
return false; |
| 89 | |
} |
| 90 | |
return true; |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public boolean equals(Object obj) |
| 99 | |
{ |
| 100 | |
if (this == obj) |
| 101 | |
return true; |
| 102 | |
if ((obj == null) || !(obj.getClass().equals(this.getClass()))) |
| 103 | |
return false; |
| 104 | |
|
| 105 | |
SourceFileData sourceFileData = (SourceFileData)obj; |
| 106 | |
return super.equals(obj) |
| 107 | |
&& this.name.equals(sourceFileData.name); |
| 108 | |
} |
| 109 | |
|
| 110 | |
public String getBaseName() |
| 111 | |
{ |
| 112 | |
String fullNameWithoutExtension; |
| 113 | |
int lastDot = this.name.lastIndexOf('.'); |
| 114 | |
if (lastDot == -1) |
| 115 | |
{ |
| 116 | |
fullNameWithoutExtension = this.name; |
| 117 | |
} |
| 118 | |
else |
| 119 | |
{ |
| 120 | |
fullNameWithoutExtension = this.name.substring(0, lastDot); |
| 121 | |
} |
| 122 | |
|
| 123 | |
int lastSlash = fullNameWithoutExtension.lastIndexOf('/'); |
| 124 | |
if (lastSlash == -1) |
| 125 | |
{ |
| 126 | |
return fullNameWithoutExtension; |
| 127 | |
} |
| 128 | |
return fullNameWithoutExtension.substring(lastSlash + 1); |
| 129 | |
} |
| 130 | |
|
| 131 | |
public SortedSet getClasses() |
| 132 | |
{ |
| 133 | |
return new TreeSet(this.children.values()); |
| 134 | |
} |
| 135 | |
|
| 136 | |
public LineData getLineCoverage(int lineNumber) |
| 137 | |
{ |
| 138 | |
Iterator iter = this.children.values().iterator(); |
| 139 | |
while (iter.hasNext()) |
| 140 | |
{ |
| 141 | |
ClassData classData = (ClassData)iter.next(); |
| 142 | |
if (classData.isValidSourceLineNumber(lineNumber)) |
| 143 | |
return classData.getLineCoverage(lineNumber); |
| 144 | |
} |
| 145 | |
return null; |
| 146 | |
} |
| 147 | |
|
| 148 | |
public String getName() |
| 149 | |
{ |
| 150 | |
return this.name; |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
public String getNormalizedName() |
| 159 | |
{ |
| 160 | |
String fullNameWithoutExtension; |
| 161 | |
int lastDot = this.name.lastIndexOf('.'); |
| 162 | |
if (lastDot == -1) |
| 163 | |
{ |
| 164 | |
fullNameWithoutExtension = this.name; |
| 165 | |
} |
| 166 | |
else |
| 167 | |
{ |
| 168 | |
fullNameWithoutExtension = this.name.substring(0, lastDot); |
| 169 | |
} |
| 170 | |
|
| 171 | |
return StringUtil.replaceAll(fullNameWithoutExtension, "/", "."); |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
public String getPackageName() |
| 179 | |
{ |
| 180 | |
int lastSlash = this.name.lastIndexOf('/'); |
| 181 | |
if (lastSlash == -1) |
| 182 | |
{ |
| 183 | |
return null; |
| 184 | |
} |
| 185 | |
return StringUtil.replaceAll(this.name.substring(0, lastSlash), "/", |
| 186 | |
"."); |
| 187 | |
} |
| 188 | |
|
| 189 | |
public int hashCode() |
| 190 | |
{ |
| 191 | |
return this.name.hashCode(); |
| 192 | |
} |
| 193 | |
|
| 194 | |
public boolean isValidSourceLineNumber(int lineNumber) |
| 195 | |
{ |
| 196 | |
Iterator iter = this.children.values().iterator(); |
| 197 | |
while (iter.hasNext()) |
| 198 | |
{ |
| 199 | |
ClassData classData = (ClassData)iter.next(); |
| 200 | |
if (classData.isValidSourceLineNumber(lineNumber)) |
| 201 | |
return true; |
| 202 | |
} |
| 203 | |
return false; |
| 204 | |
} |
| 205 | |
|
| 206 | |
} |