KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > QuickSummary


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.sql.SQLException JavaDoc;
26 import java.sql.Timestamp JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.TreeMap JavaDoc;
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 /**
50  * @author Pavel Vlasov
51  * @version $Revision: 1.6 $
52  */

53 public class QuickSummary implements CompositeResults {
54
55     private Summary summary;
56     private QuickResultsCollector collector;
57     private InspectorSet inspectorSet;
58     private String JavaDoc title;
59
60     /**
61      * @param total
62      * @param collector
63      * @throws SQLException
64      */

65     public QuickSummary(String JavaDoc title, QuickResultsCollector collector, InspectorSet inspectorSet, Collection JavaDoc children) throws SQLException JavaDoc {
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 JavaDoc children;
74     
75     public Collection JavaDoc getChildren() {
76         return children;
77     }
78
79     public void add(AggregatedResults child) {
80         throw new UnsupportedOperationException JavaDoc();
81     }
82
83     public int size() {
84         return summary.getResultSize();
85     }
86
87     private Collection JavaDoc violations=new ArrayList JavaDoc();
88     
89     public Collection JavaDoc getViolations() {
90         return violations;
91     }
92
93     public Collection JavaDoc getWaivedViolations() {
94         return violations;
95     }
96
97     public String JavaDoc getName() {
98         return title;
99     }
100
101     public Waiver addViolation(Violation violation) {
102         throw new UnsupportedOperationException JavaDoc();
103     }
104
105     private Map JavaDoc severitySummary;
106     
107     public Map JavaDoc getSeveritySummary() {
108         if (severitySummary==null) {
109             severitySummary=new TreeMap JavaDoc();
110             
111             Iterator JavaDoc 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 JavaDoc severity = new Integer JavaDoc(ps.getSeverity());
115                 Map JavaDoc imap=(Map JavaDoc) severitySummary.get(severity);
116                 if (imap==null) {
117                     imap=new TreeMap JavaDoc();
118                     severitySummary.put(severity, imap);
119                 }
120                 imap.put(ps.getName(),
121                         new InspectorSummary() {
122
123                             public String JavaDoc getDescription() {
124                                 return ps.getDescription();
125                             }
126
127                             public List JavaDoc getLocations() {
128                                 return null;
129                             }
130
131                             public int getLocationsCount() {
132                                 return (int) ps.getViolations();
133                             }
134
135                             public String JavaDoc getName() {
136                                 return ps.getName();
137                             }
138
139                             public String JavaDoc getVersion() {
140                                 return "";
141                             }
142
143                             public Number JavaDoc getSeverity() {
144                                 return new Integer JavaDoc(ps.getSeverity());
145                             }
146
147                             public String JavaDoc getConfigInfo() {
148                                 return ps.getConfigInfo();
149                             }
150
151                             public int getBaseLineLocationsCount() {
152                                 return -1;
153                             }
154
155                             public int compareTo(Object JavaDoc o) {
156                                 if (o instanceof InspectorSummary) {
157                                     return ps.getName().compareTo(((InspectorSummary) o).getName());
158                                 } else if (o instanceof Comparable JavaDoc) {
159                                     return -((Comparable JavaDoc) o).compareTo(this);
160                                 } else {
161                                     return this.hashCode()-o.hashCode();
162                                 }
163                             }
164                 });
165             }
166         }
167         return severitySummary;
168     }
169
170     private Collection JavaDoc warnings;
171     
172     public Collection JavaDoc getWarnings() {
173         if (warnings==null) {
174             warnings=new ArrayList JavaDoc();
175             Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc();
249     }
250
251     public void addMetric(SourceMarker source, String JavaDoc name, double value) {
252         throw new UnsupportedOperationException JavaDoc();
253     }
254
255     private Map JavaDoc metrics;
256     
257     public Map JavaDoc getMetrics() {
258         if (metrics==null) {
259             metrics=new TreeMap JavaDoc();
260             Iterator JavaDoc 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 JavaDoc();
289                             }
290     
291                             public void add(com.pavelvlasov.metrics.Metric metric) {
292                                 throw new UnsupportedOperationException JavaDoc();
293                             }
294     
295                             public Collection JavaDoc getMeasurements() {
296                                 return null;
297                             }
298     
299                             public String JavaDoc getName() {
300                                 return metric.getName();
301                             }
302                             
303                             public double getDeviation() {
304                                 // TODO - Calcualte deviation
305
return 0;
306                             }
307                             
308                         });
309             }
310         }
311         return metrics;
312     }
313
314     public void aggregate(AggregatedResults agregee) {
315         throw new UnsupportedOperationException JavaDoc();
316     }
317
318     public void setReviewsNumber(long reviews) {
319         throw new UnsupportedOperationException JavaDoc();
320     }
321
322     public void setCodeBase(long codeBase) {
323         throw new UnsupportedOperationException JavaDoc();
324     }
325
326     public void addAnnotation(Annotation annotation) {
327         throw new UnsupportedOperationException JavaDoc();
328     }
329
330     public Collection JavaDoc getAnnotations() {
331         return violations;
332     }
333
334     public WaiverSet getWaiverSet() {
335         throw new UnsupportedOperationException JavaDoc();
336     }
337
338     public void commit() {
339         // No action is required
340
}
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 JavaDoc 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 JavaDoc 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 JavaDoc[] {new Double JavaDoc(SimpleAggregatedResults.normsinv(p)+1.5)}) + (summary.getHasWarnings() ? " (not accurate because of warnings)" : "");
372         }
373     }
374
375     public Number JavaDoc 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 JavaDoc getDate() {
396         Timestamp JavaDoc resultDate = summary.getResultDate();
397         return resultDate==null ? new Date JavaDoc() : new Date JavaDoc(resultDate.getTime());
398     }
399 }
400
Popular Tags