1 23 package org.hammurapi; 24 25 import java.text.MessageFormat ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Date ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.TreeMap ; 33 34 import org.hammurapi.results.AggregatedResults; 35 import org.hammurapi.results.Annotation; 36 import org.hammurapi.results.BasicResults; 37 import org.hammurapi.results.CompositeResults; 38 import org.hammurapi.results.InspectorSummary; 39 import org.hammurapi.results.NamedResults; 40 import org.hammurapi.results.quick.MetricSummary; 41 import org.hammurapi.results.quick.PackageTotal; 42 import org.hammurapi.results.quick.Result; 43 import org.hammurapi.results.quick.Warning; 44 import org.hammurapi.results.simple.SimpleAggregatedResults; 45 46 import com.pavelvlasov.review.SimpleSourceMarker; 47 import com.pavelvlasov.review.SourceMarker; 48 49 53 public class QuickPackageResults implements CompositeResults { 54 55 private PackageTotal total; 56 private QuickResultsCollector collector; 57 private InspectorSet inspectorSet; 58 59 63 public QuickPackageResults(PackageTotal total, QuickResultsCollector collector, InspectorSet inspectorSet) { 64 this.total=total; 65 this.collector=collector; 66 this.inspectorSet=inspectorSet; 67 } 68 69 private List children; 70 71 public Collection getChildren() { 72 if (children==null) { 73 children=new ArrayList (); 74 Iterator rit=collector.getEngine().getPackageResults(total.getPackageName()).iterator(); 75 while (rit.hasNext()) { 76 final Result result=(Result) rit.next(); 77 children.add(new NamedResults() { 78 79 public Waiver addViolation(Violation violation) throws HammurapiException { 80 throw new UnsupportedOperationException (); 81 } 82 83 public Map getSeveritySummary() { 84 return null; 85 } 86 87 public Collection getWarnings() { 88 return null; 89 } 90 91 public boolean hasWarnings() { 92 return result.getHasWarnings(); 93 } 94 95 public void addWarning(Violation warning) { 96 throw new UnsupportedOperationException (); 97 } 98 99 public void addMetric(SourceMarker source, String name, double value) { 100 throw new UnsupportedOperationException (); 101 } 102 103 public Map getMetrics() { 104 return null; 105 } 106 107 public void aggregate(AggregatedResults agregee) { 108 throw new UnsupportedOperationException (); 109 } 110 111 public void setReviewsNumber(long reviews) { 112 throw new UnsupportedOperationException (); 113 } 114 115 public void setCodeBase(long codeBase) { 116 throw new UnsupportedOperationException (); 117 } 118 119 public void addAnnotation(Annotation annotation) { 120 throw new UnsupportedOperationException (); 121 } 122 123 public Collection getAnnotations() { 124 return null; 125 } 126 127 public WaiverSet getWaiverSet() { 128 return null; 129 } 130 131 public void commit() throws HammurapiException { 132 133 } 134 135 public boolean isNew() { 136 return result.getState()==QuickResultsCollector.RESULT_NEW; 137 } 138 139 public BasicResults getBaseLine() { 140 return null; 141 } 142 143 public long getCodeBase() { 144 return result.getCodebase(); 145 } 146 147 public String getDPMO() { 148 if (result.getReviews()==0) { 149 return "Not available, no reviews"; 150 } else { 151 return String.valueOf((int) (1000000*result.getViolationLevel()/result.getReviews())) + (result.getHasWarnings() ? " (not accurate because of warnings)" : ""); 152 } 153 } 154 155 public Number getMaxSeverity() { 156 return result.getMaxSeverity(); 157 } 158 159 public long getReviewsNumber() { 160 return result.getReviews(); 161 } 162 163 public String getSigma() { 164 double p=1.0-result.getViolationLevel()/result.getReviews(); 165 if (result.getReviews()==0) { 166 return "No results"; 167 } else if (p<=0) { 168 return "Full incompliance"; 169 } else if (p>=1) { 170 return "Full compliance"; 171 } else { 172 return MessageFormat.format("{0,number,#.###}", new Object [] {new Double (SimpleAggregatedResults.normsinv(p)+1.5)}) + (result.getHasWarnings() ? " (not accurate because of warnings)" : ""); 173 } 174 } 175 176 public double getViolationLevel() { 177 return result.getViolationLevel(); 178 } 179 180 public int getViolationsNumber() { 181 return (int) result.getViolations(); 182 } 183 184 public int getWaivedViolationsNumber() { 185 return (int) result.getWaivedViolations(); 186 } 187 188 public Date getDate() { 189 return new Date (result.getResultDate().getTime()); 190 } 191 192 public String getName() { 193 return result.getName(); 194 } 195 196 }); 197 } 198 } 199 return children; 200 } 201 202 public void add(AggregatedResults child) { 203 throw new UnsupportedOperationException (); 204 } 205 206 public int size() { 207 return total.getResultSize(); 208 } 209 210 private Collection violations=new ArrayList (); 211 212 public Collection getViolations() { 213 return violations; 214 } 215 216 public Collection getWaivedViolations() { 217 return violations; 218 } 219 220 public String getName() { 221 return total.getPackageName(); 222 } 223 224 public Waiver addViolation(Violation violation) throws HammurapiException { 225 throw new UnsupportedOperationException (); 226 } 227 228 private Map severitySummary; 229 230 public Map getSeveritySummary() { 231 if (severitySummary==null) { 232 severitySummary=new TreeMap (); 233 234 Iterator it=collector.getEngine().getPackageInspectorSummary(total.getPackageName()).iterator(); 235 while (it.hasNext()) { 236 final org.hammurapi.results.quick.InspectorSummary ps=(org.hammurapi.results.quick.InspectorSummary) it.next(); 237 Integer severity = new Integer (ps.getSeverity()); 238 Map imap=(Map ) severitySummary.get(severity); 239 if (imap==null) { 240 imap=new TreeMap (); 241 severitySummary.put(severity, imap); 242 } 243 imap.put(ps.getName(), 244 new InspectorSummary() { 245 246 public String getDescription() { 247 return ps.getDescription(); 248 } 249 250 public List getLocations() { 251 return null; 252 } 253 254 public int getLocationsCount() { 255 return (int) ps.getViolations(); 256 } 257 258 public String getName() { 259 return ps.getName(); 260 } 261 262 public String getVersion() { 263 return ""; 264 } 265 266 public Number getSeverity() { 267 return new Integer (ps.getSeverity()); 268 } 269 270 public String getConfigInfo() { 271 return ps.getConfigInfo(); 272 } 273 274 public int getBaseLineLocationsCount() { 275 return -1; 276 } 277 278 public int compareTo(Object o) { 279 if (o instanceof InspectorSummary) { 280 return ps.getName().compareTo(((InspectorSummary) o).getName()); 281 } else if (o instanceof Comparable ) { 282 return -((Comparable ) o).compareTo(this); 283 } else { 284 return this.hashCode()-o.hashCode(); 285 } 286 } 287 }); 288 } 289 } 290 return severitySummary; 291 } 292 293 private Collection warnings; 294 295 public Collection getWarnings() { 296 if (warnings==null) { 297 warnings=new ArrayList (); 298 Iterator it=collector.getEngine().getWarningPackageEQ(total.getPackageName()).iterator(); 299 while (it.hasNext()) { 300 final Warning warning=(Warning) it.next(); 301 warnings.add(new Violation() { 302 303 public String getMessage() { 304 return warning.getMessage(); 305 } 306 307 public InspectorDescriptor getDescriptor() { 308 return inspectorSet.getDescriptor(warning.getInspector()); 309 } 310 311 private SimpleSourceMarker sourceMarker; 312 313 { 314 if (warning.getSource()!=null && warning.getLine()!=null && warning.getCol()!=null) { 315 sourceMarker=new SimpleSourceMarker( 316 warning.getCol().intValue(), 317 warning.getLine().intValue(), 318 warning.getSource(), 319 null); 320 321 sourceMarker.setSignature(warning.getSourceSignature()); 322 } 323 } 324 325 public SourceMarker getSource() { 326 return sourceMarker; 327 } 328 329 public int compareTo(Object o) { 330 if (o==this) { 331 return 0; 332 } else if (o instanceof Violation) { 333 Violation v=(Violation) o; 334 int vline = v.getSource()==null ? 0 : v.getSource().getLine(); 335 int line = getSource()==null ? 0 : getSource().getLine(); 336 if (vline==line) { 337 int vcolumn = v.getSource()==null ? 0 : v.getSource().getColumn(); 338 int column = getSource()==null ? 0 : getSource().getColumn(); 339 if (vcolumn==column) { 340 if (warning.getMessage()==null) { 341 return v.getMessage()==null ? 0 : 1; 342 } else { 343 if (v.getMessage()==null) { 344 return -1; 345 } else { 346 return warning.getMessage().compareTo(v.getMessage()); 347 } 348 } 349 } else { 350 return column-vcolumn; 351 } 352 } else { 353 return line-vline; 354 } 355 } else { 356 return 1; 357 } 358 } 359 360 }); 361 } 362 } 363 return warnings; 364 } 365 366 public boolean hasWarnings() { 367 return total.getHasWarnings(); 368 } 369 370 public void addWarning(Violation warning) { 371 throw new UnsupportedOperationException (); 372 } 373 374 public void addMetric(SourceMarker source, String name, double value) { 375 throw new UnsupportedOperationException (); 376 } 377 378 private Map metrics; 379 380 public Map getMetrics() { 381 if (metrics==null) { 382 metrics=new TreeMap (); 383 Iterator it=collector.getEngine().getPackageMetrics(total.getPackageName()).iterator(); 384 while (it.hasNext()) { 385 final MetricSummary metric=(MetricSummary) it.next(); 386 metrics.put( 387 metric.getName(), 388 new com.pavelvlasov.metrics.Metric() { 389 390 public int getNumber() { 391 return (int) metric.getMeasurements(); 392 } 393 394 public double getMin() { 395 return metric.getMinValue(); 396 } 397 398 public double getMax() { 399 return metric.getMaxValue(); 400 } 401 402 public double getAvg() { 403 return metric.getMetricTotal()/metric.getMeasurements(); 404 } 405 406 public double getTotal() { 407 return metric.getMetricTotal(); 408 } 409 410 public void add(double value, long time) { 411 throw new UnsupportedOperationException (); 412 } 413 414 public void add(com.pavelvlasov.metrics.Metric metric) { 415 throw new UnsupportedOperationException (); 416 } 417 418 public Collection getMeasurements() { 419 return null; 420 } 421 422 public String getName() { 423 return metric.getName(); 424 } 425 426 public double getDeviation() { 427 return 0; 429 } 430 431 }); 432 } 433 } 434 return metrics; 435 } 436 437 public void aggregate(AggregatedResults agregee) { 438 throw new UnsupportedOperationException (); 439 } 440 441 public void setReviewsNumber(long reviews) { 442 throw new UnsupportedOperationException (); 443 } 444 445 public void setCodeBase(long codeBase) { 446 throw new UnsupportedOperationException (); 447 } 448 449 public void addAnnotation(Annotation annotation) { 450 throw new UnsupportedOperationException (); 451 } 452 453 public Collection getAnnotations() { 454 return violations; 455 } 456 457 public WaiverSet getWaiverSet() { 458 throw new UnsupportedOperationException (); 459 } 460 461 public void commit() throws HammurapiException { 462 463 } 464 465 public boolean isNew() { 466 return total.getMinState()==QuickResultsCollector.RESULT_NEW; 467 } 468 469 public BasicResults getBaseLine() { 470 return null; 471 } 472 473 public long getCodeBase() { 474 return total.getCodebase(); 475 } 476 477 public String getDPMO() { 478 if (total.getReviews()==0) { 479 return "Not available, no reviews"; 480 } else { 481 return String.valueOf((int) (1000000*total.getViolationLevel()/total.getReviews())) + (total.getHasWarnings() ? " (not accurate because of warnings)" : ""); 482 } 483 } 484 485 public String getSigma() { 486 double p=1.0-total.getViolationLevel()/total.getReviews(); 487 if (total.getReviews()==0) { 488 return "No results"; 489 } else if (p<=0) { 490 return "Full incompliance"; 491 } else if (p>=1) { 492 return "Full compliance"; 493 } else { 494 return MessageFormat.format("{0,number,#.###}", new Object [] {new Double (SimpleAggregatedResults.normsinv(p)+1.5)}) + (total.getHasWarnings() ? " (not accurate because of warnings)" : ""); 495 } 496 } 497 498 public Number getMaxSeverity() { 499 return total.getMaxSeverity(); 500 } 501 502 public long getReviewsNumber() { 503 return total.getReviews(); 504 } 505 506 public double getViolationLevel() { 507 return total.getViolationLevel(); 508 } 509 510 public int getViolationsNumber() { 511 return (int) total.getViolations(); 512 } 513 514 public int getWaivedViolationsNumber() { 515 return (int) total.getWaivedViolations(); 516 } 517 518 public Date getDate() { 519 return new Date (total.getResultDate().getTime()); 520 } 521 522 } 523 | Popular Tags |