KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > QuickPackageResults


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi;
24
25 import java.text.MessageFormat JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.TreeMap JavaDoc;
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 /**
50  * @author Pavel Vlasov
51  * @version $Revision: 1.5 $
52  */

53 public class QuickPackageResults implements CompositeResults {
54
55     private PackageTotal total;
56     private QuickResultsCollector collector;
57     private InspectorSet inspectorSet;
58
59     /**
60      * @param total
61      * @param collector
62      */

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 JavaDoc children;
70     
71     public Collection JavaDoc getChildren() {
72         if (children==null) {
73             children=new ArrayList JavaDoc();
74             Iterator JavaDoc 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 JavaDoc();
81                     }
82
83                     public Map JavaDoc getSeveritySummary() {
84                         return null;
85                     }
86
87                     public Collection JavaDoc 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 JavaDoc();
97                     }
98
99                     public void addMetric(SourceMarker source, String JavaDoc name, double value) {
100                         throw new UnsupportedOperationException JavaDoc();
101                     }
102
103                     public Map JavaDoc getMetrics() {
104                         return null;
105                     }
106
107                     public void aggregate(AggregatedResults agregee) {
108                         throw new UnsupportedOperationException JavaDoc();
109                     }
110
111                     public void setReviewsNumber(long reviews) {
112                         throw new UnsupportedOperationException JavaDoc();
113                     }
114
115                     public void setCodeBase(long codeBase) {
116                         throw new UnsupportedOperationException JavaDoc();
117                     }
118
119                     public void addAnnotation(Annotation annotation) {
120                         throw new UnsupportedOperationException JavaDoc();
121                     }
122
123                     public Collection JavaDoc 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 JavaDoc 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 JavaDoc getMaxSeverity() {
156                         return result.getMaxSeverity();
157                     }
158
159                     public long getReviewsNumber() {
160                         return result.getReviews();
161                     }
162
163                     public String JavaDoc 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 JavaDoc[] {new Double JavaDoc(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 JavaDoc getDate() {
189                         return new Date JavaDoc(result.getResultDate().getTime());
190                     }
191
192                     public String JavaDoc 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 JavaDoc();
204     }
205
206     public int size() {
207         return total.getResultSize();
208     }
209
210     private Collection JavaDoc violations=new ArrayList JavaDoc();
211     
212     public Collection JavaDoc getViolations() {
213         return violations;
214     }
215
216     public Collection JavaDoc getWaivedViolations() {
217         return violations;
218     }
219
220     public String JavaDoc getName() {
221         return total.getPackageName();
222     }
223
224     public Waiver addViolation(Violation violation) throws HammurapiException {
225         throw new UnsupportedOperationException JavaDoc();
226     }
227
228     private Map JavaDoc severitySummary;
229     
230     public Map JavaDoc getSeveritySummary() {
231         if (severitySummary==null) {
232             severitySummary=new TreeMap JavaDoc();
233             
234             Iterator JavaDoc 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 JavaDoc severity = new Integer JavaDoc(ps.getSeverity());
238                 Map JavaDoc imap=(Map JavaDoc) severitySummary.get(severity);
239                 if (imap==null) {
240                     imap=new TreeMap JavaDoc();
241                     severitySummary.put(severity, imap);
242                 }
243                 imap.put(ps.getName(),
244                         new InspectorSummary() {
245
246                             public String JavaDoc getDescription() {
247                                 return ps.getDescription();
248                             }
249
250                             public List JavaDoc getLocations() {
251                                 return null;
252                             }
253
254                             public int getLocationsCount() {
255                                 return (int) ps.getViolations();
256                             }
257
258                             public String JavaDoc getName() {
259                                 return ps.getName();
260                             }
261
262                             public String JavaDoc getVersion() {
263                                 return "";
264                             }
265
266                             public Number JavaDoc getSeverity() {
267                                 return new Integer JavaDoc(ps.getSeverity());
268                             }
269
270                             public String JavaDoc getConfigInfo() {
271                                 return ps.getConfigInfo();
272                             }
273
274                             public int getBaseLineLocationsCount() {
275                                 return -1;
276                             }
277
278                             public int compareTo(Object JavaDoc o) {
279                                 if (o instanceof InspectorSummary) {
280                                     return ps.getName().compareTo(((InspectorSummary) o).getName());
281                                 } else if (o instanceof Comparable JavaDoc) {
282                                     return -((Comparable JavaDoc) o).compareTo(this);
283                                 } else {
284                                     return this.hashCode()-o.hashCode();
285                                 }
286                             }
287                 });
288             }
289         }
290         return severitySummary;
291     }
292
293     private Collection JavaDoc warnings;
294     
295     public Collection JavaDoc getWarnings() {
296         if (warnings==null) {
297             warnings=new ArrayList JavaDoc();
298             Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc();
372     }
373
374     public void addMetric(SourceMarker source, String JavaDoc name, double value) {
375         throw new UnsupportedOperationException JavaDoc();
376     }
377
378     private Map JavaDoc metrics;
379     
380     public Map JavaDoc getMetrics() {
381         if (metrics==null) {
382             metrics=new TreeMap JavaDoc();
383             Iterator JavaDoc 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 JavaDoc();
412                             }
413     
414                             public void add(com.pavelvlasov.metrics.Metric metric) {
415                                 throw new UnsupportedOperationException JavaDoc();
416                             }
417     
418                             public Collection JavaDoc getMeasurements() {
419                                 return null;
420                             }
421     
422                             public String JavaDoc getName() {
423                                 return metric.getName();
424                             }
425
426                             public double getDeviation() {
427                                 // TODO - Calcualte deviation
428
return 0;
429                             }
430                             
431                         });
432             }
433         }
434         return metrics;
435     }
436
437     public void aggregate(AggregatedResults agregee) {
438         throw new UnsupportedOperationException JavaDoc();
439     }
440
441     public void setReviewsNumber(long reviews) {
442         throw new UnsupportedOperationException JavaDoc();
443     }
444
445     public void setCodeBase(long codeBase) {
446         throw new UnsupportedOperationException JavaDoc();
447     }
448
449     public void addAnnotation(Annotation annotation) {
450         throw new UnsupportedOperationException JavaDoc();
451     }
452
453     public Collection JavaDoc getAnnotations() {
454         return violations;
455     }
456
457     public WaiverSet getWaiverSet() {
458         throw new UnsupportedOperationException JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc[] {new Double JavaDoc(SimpleAggregatedResults.normsinv(p)+1.5)}) + (total.getHasWarnings() ? " (not accurate because of warnings)" : "");
495         }
496     }
497
498     public Number JavaDoc 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 JavaDoc getDate() {
519         return new Date JavaDoc(total.getResultDate().getTime());
520     }
521
522 }
523
Popular Tags