1 23 package org.hammurapi; 24 25 import java.sql.SQLException ; 26 import java.sql.Timestamp ; 27 import java.text.MessageFormat ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Date ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.TreeMap ; 35 36 import org.hammurapi.results.AggregatedResults; 37 import org.hammurapi.results.Annotation; 38 import org.hammurapi.results.BasicResults; 39 import org.hammurapi.results.CompositeResults; 40 import org.hammurapi.results.InspectorSummary; 41 import org.hammurapi.results.quick.MetricSummary; 42 import org.hammurapi.results.quick.Summary; 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 QuickSummary implements CompositeResults { 54 55 private Summary summary; 56 private QuickResultsCollector collector; 57 private InspectorSet inspectorSet; 58 private String title; 59 60 65 public QuickSummary(String title, QuickResultsCollector collector, InspectorSet inspectorSet, Collection children) throws SQLException { 66 this.collector=collector; 67 this.inspectorSet=inspectorSet; 68 this.summary=collector.getEngine().getSummary(); 69 this.children=children; 70 this.title=title; 71 } 72 73 private Collection children; 74 75 public Collection getChildren() { 76 return children; 77 } 78 79 public void add(AggregatedResults child) { 80 throw new UnsupportedOperationException (); 81 } 82 83 public int size() { 84 return summary.getResultSize(); 85 } 86 87 private Collection violations=new ArrayList (); 88 89 public Collection getViolations() { 90 return violations; 91 } 92 93 public Collection getWaivedViolations() { 94 return violations; 95 } 96 97 public String getName() { 98 return title; 99 } 100 101 public Waiver addViolation(Violation violation) { 102 throw new UnsupportedOperationException (); 103 } 104 105 private Map severitySummary; 106 107 public Map getSeveritySummary() { 108 if (severitySummary==null) { 109 severitySummary=new TreeMap (); 110 111 Iterator it=collector.getEngine().getInspectorSummary().iterator(); 112 while (it.hasNext()) { 113 final org.hammurapi.results.quick.InspectorSummary ps=(org.hammurapi.results.quick.InspectorSummary) it.next(); 114 Integer severity = new Integer (ps.getSeverity()); 115 Map imap=(Map ) severitySummary.get(severity); 116 if (imap==null) { 117 imap=new TreeMap (); 118 severitySummary.put(severity, imap); 119 } 120 imap.put(ps.getName(), 121 new InspectorSummary() { 122 123 public String getDescription() { 124 return ps.getDescription(); 125 } 126 127 public List getLocations() { 128 return null; 129 } 130 131 public int getLocationsCount() { 132 return (int) ps.getViolations(); 133 } 134 135 public String getName() { 136 return ps.getName(); 137 } 138 139 public String getVersion() { 140 return ""; 141 } 142 143 public Number getSeverity() { 144 return new Integer (ps.getSeverity()); 145 } 146 147 public String getConfigInfo() { 148 return ps.getConfigInfo(); 149 } 150 151 public int getBaseLineLocationsCount() { 152 return -1; 153 } 154 155 public int compareTo(Object o) { 156 if (o instanceof InspectorSummary) { 157 return ps.getName().compareTo(((InspectorSummary) o).getName()); 158 } else if (o instanceof Comparable ) { 159 return -((Comparable ) o).compareTo(this); 160 } else { 161 return this.hashCode()-o.hashCode(); 162 } 163 } 164 }); 165 } 166 } 167 return severitySummary; 168 } 169 170 private Collection warnings; 171 172 public Collection getWarnings() { 173 if (warnings==null) { 174 warnings=new ArrayList (); 175 Iterator it=collector.getEngine().getWarning().iterator(); 176 while (it.hasNext()) { 177 final Warning warning=(Warning) it.next(); 178 warnings.add(new Violation() { 179 180 public String getMessage() { 181 return warning.getMessage(); 182 } 183 184 public InspectorDescriptor getDescriptor() { 185 return inspectorSet.getDescriptor(warning.getInspector()); 186 } 187 188 private SimpleSourceMarker sourceMarker; 189 190 { 191 if (warning.getSource()!=null && warning.getLine()!=null && warning.getCol()!=null) { 192 sourceMarker=new SimpleSourceMarker( 193 warning.getCol().intValue(), 194 warning.getLine().intValue(), 195 warning.getSource(), 196 null); 197 198 sourceMarker.setSignature(warning.getSourceSignature()); 199 } 200 } 201 202 public SourceMarker getSource() { 203 return sourceMarker; 204 } 205 206 public int compareTo(Object o) { 207 if (o==this) { 208 return 0; 209 } else if (o instanceof Violation) { 210 Violation v=(Violation) o; 211 int vline = v.getSource()==null ? 0 : v.getSource().getLine(); 212 int line = getSource()==null ? 0 : getSource().getLine(); 213 if (vline==line) { 214 int vcolumn = v.getSource()==null ? 0 : v.getSource().getColumn(); 215 int column = getSource()==null ? 0 : getSource().getColumn(); 216 if (vcolumn==column) { 217 if (warning.getMessage()==null) { 218 return v.getMessage()==null ? 0 : 1; 219 } else { 220 if (v.getMessage()==null) { 221 return -1; 222 } else { 223 return warning.getMessage().compareTo(v.getMessage()); 224 } 225 } 226 } else { 227 return column-vcolumn; 228 } 229 } else { 230 return line-vline; 231 } 232 } else { 233 return 1; 234 } 235 } 236 237 }); 238 } 239 } 240 return warnings; 241 } 242 243 public boolean hasWarnings() { 244 return summary.getHasWarnings(); 245 } 246 247 public void addWarning(Violation warning) { 248 throw new UnsupportedOperationException (); 249 } 250 251 public void addMetric(SourceMarker source, String name, double value) { 252 throw new UnsupportedOperationException (); 253 } 254 255 private Map metrics; 256 257 public Map getMetrics() { 258 if (metrics==null) { 259 metrics=new TreeMap (); 260 Iterator it=collector.getEngine().getMetricSummary().iterator(); 261 while (it.hasNext()) { 262 final MetricSummary metric=(MetricSummary) it.next(); 263 metrics.put( 264 metric.getName(), 265 new com.pavelvlasov.metrics.Metric() { 266 267 public int getNumber() { 268 return (int) metric.getMeasurements(); 269 } 270 271 public double getMin() { 272 return metric.getMinValue(); 273 } 274 275 public double getMax() { 276 return metric.getMaxValue(); 277 } 278 279 public double getAvg() { 280 return metric.getMetricTotal()/metric.getMeasurements(); 281 } 282 283 public double getTotal() { 284 return metric.getMetricTotal(); 285 } 286 287 public void add(double value, long time) { 288 throw new UnsupportedOperationException (); 289 } 290 291 public void add(com.pavelvlasov.metrics.Metric metric) { 292 throw new UnsupportedOperationException (); 293 } 294 295 public Collection getMeasurements() { 296 return null; 297 } 298 299 public String getName() { 300 return metric.getName(); 301 } 302 303 public double getDeviation() { 304 return 0; 306 } 307 308 }); 309 } 310 } 311 return metrics; 312 } 313 314 public void aggregate(AggregatedResults agregee) { 315 throw new UnsupportedOperationException (); 316 } 317 318 public void setReviewsNumber(long reviews) { 319 throw new UnsupportedOperationException (); 320 } 321 322 public void setCodeBase(long codeBase) { 323 throw new UnsupportedOperationException (); 324 } 325 326 public void addAnnotation(Annotation annotation) { 327 throw new UnsupportedOperationException (); 328 } 329 330 public Collection getAnnotations() { 331 return violations; 332 } 333 334 public WaiverSet getWaiverSet() { 335 throw new UnsupportedOperationException (); 336 } 337 338 public void commit() { 339 } 341 342 public boolean isNew() { 343 return summary.getMinState()==QuickResultsCollector.RESULT_NEW; 344 } 345 346 public BasicResults getBaseLine() { 347 return null; 348 } 349 350 public long getCodeBase() { 351 return summary.getCodebase(); 352 } 353 354 public String getDPMO() { 355 if (summary.getReviews()==0) { 356 return "Not available, no reviews"; 357 } else { 358 return String.valueOf((int) (1000000*summary.getViolationLevel()/summary.getReviews())) + (summary.getHasWarnings() ? " (not accurate because of warnings)" : ""); 359 } 360 } 361 362 public String getSigma() { 363 double p=1.0-summary.getViolationLevel()/summary.getReviews(); 364 if (summary.getReviews()==0) { 365 return "No results"; 366 } else if (p<=0) { 367 return "Full incompliance"; 368 } else if (p>=1) { 369 return "Full compliance"; 370 } else { 371 return MessageFormat.format("{0,number,#.###}", new Object [] {new Double (SimpleAggregatedResults.normsinv(p)+1.5)}) + (summary.getHasWarnings() ? " (not accurate because of warnings)" : ""); 372 } 373 } 374 375 public Number getMaxSeverity() { 376 return summary.getMaxSeverity(); 377 } 378 379 public long getReviewsNumber() { 380 return summary.getReviews(); 381 } 382 383 public double getViolationLevel() { 384 return summary.getViolationLevel(); 385 } 386 387 public int getViolationsNumber() { 388 return (int) summary.getViolations(); 389 } 390 391 public int getWaivedViolationsNumber() { 392 return (int) summary.getWaivedViolations(); 393 } 394 395 public Date getDate() { 396 Timestamp resultDate = summary.getResultDate(); 397 return resultDate==null ? new Date () : new Date (resultDate.getTime()); 398 } 399 } 400 | Popular Tags |