1 24 25 package net.sourceforge.cobertura.coveragedata; 26 27 import java.io.Serializable ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 32 44 public abstract class CoverageDataContainer 45 implements CoverageData, HasBeenInstrumented, Serializable 46 { 47 48 53 Map children = new HashMap (); 54 55 63 public boolean equals(Object obj) 64 { 65 if (this == obj) 66 return true; 67 if ((obj == null) || !(obj.getClass().equals(this.getClass()))) 68 return false; 69 70 CoverageDataContainer coverageDataContainer = (CoverageDataContainer)obj; 71 return this.children.equals(coverageDataContainer.children); 72 } 73 74 78 public double getBranchCoverageRate() 79 { 80 int number = 0; 81 int numberCovered = 0; 82 Iterator iter = this.children.values().iterator(); 83 while (iter.hasNext()) 84 { 85 CoverageData coverageContainer = (CoverageData)iter.next(); 86 number += coverageContainer.getNumberOfValidBranches(); 87 numberCovered += coverageContainer.getNumberOfCoveredBranches(); 88 } 89 if (number == 0) 90 { 91 return 1d; 93 } 94 return (double)numberCovered / number; 95 } 96 97 104 public CoverageData getChild(String name) 105 { 106 return (CoverageData)this.children.get(name); 107 } 108 109 114 public double getLineCoverageRate() 115 { 116 int number = 0; 117 int numberCovered = 0; 118 Iterator iter = this.children.values().iterator(); 119 while (iter.hasNext()) 120 { 121 CoverageData coverageContainer = (CoverageData)iter.next(); 122 number += coverageContainer.getNumberOfValidLines(); 123 numberCovered += coverageContainer.getNumberOfCoveredLines(); 124 } 125 if (number == 0) 126 { 127 return 1d; 129 } 130 return (double)numberCovered / number; 131 } 132 133 136 public int getNumberOfChildren() 137 { 138 return this.children.size(); 139 } 140 141 public int getNumberOfCoveredBranches() 142 { 143 int number = 0; 144 Iterator iter = this.children.values().iterator(); 145 while (iter.hasNext()) 146 { 147 CoverageData coverageContainer = (CoverageData)iter.next(); 148 number += coverageContainer.getNumberOfCoveredBranches(); 149 } 150 return number; 151 } 152 153 public int getNumberOfCoveredLines() 154 { 155 int number = 0; 156 Iterator iter = this.children.values().iterator(); 157 while (iter.hasNext()) 158 { 159 CoverageData coverageContainer = (CoverageData)iter.next(); 160 number += coverageContainer.getNumberOfCoveredLines(); 161 } 162 return number; 163 } 164 165 public int getNumberOfValidBranches() 166 { 167 int number = 0; 168 Iterator iter = this.children.values().iterator(); 169 while (iter.hasNext()) 170 { 171 CoverageData coverageContainer = (CoverageData)iter.next(); 172 number += coverageContainer.getNumberOfValidBranches(); 173 } 174 return number; 175 } 176 177 public int getNumberOfValidLines() 178 { 179 int number = 0; 180 Iterator iter = this.children.values().iterator(); 181 while (iter.hasNext()) 182 { 183 CoverageData coverageContainer = (CoverageData)iter.next(); 184 number += coverageContainer.getNumberOfValidLines(); 185 } 186 return number; 187 } 188 189 194 public int hashCode() 195 { 196 return this.children.size(); 197 } 198 199 204 public void merge(CoverageData coverageData) 205 { 206 CoverageDataContainer container = (CoverageDataContainer)coverageData; 207 Iterator iter = container.children.keySet().iterator(); 208 while (iter.hasNext()) 209 { 210 Object key = iter.next(); 211 CoverageData newChild = (CoverageData)container.children.get(key); 212 CoverageData existingChild = (CoverageData)this.children.get(key); 213 if (existingChild != null) 214 { 215 existingChild.merge(newChild); 216 } 217 else 218 { 219 this.children.put(key, newChild); 223 } 224 } 225 } 226 227 } 228 | Popular Tags |