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