001: /*
002: * Hammurapi
003: * Automated Java code review system.
004: * Copyright (C) 2004 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.org
021: * e-Mail: support@hammurapi.biz
022: */
023: package org.hammurapi;
024:
025: import java.sql.SQLException;
026: import java.sql.Timestamp;
027: import java.text.MessageFormat;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Date;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034: import java.util.TreeMap;
035:
036: import org.hammurapi.results.AggregatedResults;
037: import org.hammurapi.results.Annotation;
038: import org.hammurapi.results.BasicResults;
039: import org.hammurapi.results.CompositeResults;
040: import org.hammurapi.results.InspectorSummary;
041: import org.hammurapi.results.quick.MetricSummary;
042: import org.hammurapi.results.quick.Summary;
043: import org.hammurapi.results.quick.Warning;
044: import org.hammurapi.results.simple.SimpleAggregatedResults;
045:
046: import com.pavelvlasov.review.SimpleSourceMarker;
047: import com.pavelvlasov.review.SourceMarker;
048:
049: /**
050: * @author Pavel Vlasov
051: * @version $Revision: 1.6 $
052: */
053: public class QuickSummary implements CompositeResults {
054:
055: private Summary summary;
056: private QuickResultsCollector collector;
057: private InspectorSet inspectorSet;
058: private String title;
059:
060: /**
061: * @param total
062: * @param collector
063: * @throws SQLException
064: */
065: public QuickSummary(String title, QuickResultsCollector collector,
066: InspectorSet inspectorSet, Collection children)
067: throws SQLException {
068: this .collector = collector;
069: this .inspectorSet = inspectorSet;
070: this .summary = collector.getEngine().getSummary();
071: this .children = children;
072: this .title = title;
073: }
074:
075: private Collection children;
076:
077: public Collection getChildren() {
078: return children;
079: }
080:
081: public void add(AggregatedResults child) {
082: throw new UnsupportedOperationException();
083: }
084:
085: public int size() {
086: return summary.getResultSize();
087: }
088:
089: private Collection violations = new ArrayList();
090:
091: public Collection getViolations() {
092: return violations;
093: }
094:
095: public Collection getWaivedViolations() {
096: return violations;
097: }
098:
099: public String getName() {
100: return title;
101: }
102:
103: public Waiver addViolation(Violation violation) {
104: throw new UnsupportedOperationException();
105: }
106:
107: private Map severitySummary;
108:
109: public Map getSeveritySummary() {
110: if (severitySummary == null) {
111: severitySummary = new TreeMap();
112:
113: Iterator it = collector.getEngine().getInspectorSummary()
114: .iterator();
115: while (it.hasNext()) {
116: final org.hammurapi.results.quick.InspectorSummary ps = (org.hammurapi.results.quick.InspectorSummary) it
117: .next();
118: Integer severity = new Integer(ps.getSeverity());
119: Map imap = (Map) severitySummary.get(severity);
120: if (imap == null) {
121: imap = new TreeMap();
122: severitySummary.put(severity, imap);
123: }
124: imap.put(ps.getName(), new InspectorSummary() {
125:
126: public String getDescription() {
127: return ps.getDescription();
128: }
129:
130: public List getLocations() {
131: return null;
132: }
133:
134: public int getLocationsCount() {
135: return (int) ps.getViolations();
136: }
137:
138: public String getName() {
139: return ps.getName();
140: }
141:
142: public String getVersion() {
143: return "";
144: }
145:
146: public Number getSeverity() {
147: return new Integer(ps.getSeverity());
148: }
149:
150: public String getConfigInfo() {
151: return ps.getConfigInfo();
152: }
153:
154: public int getBaseLineLocationsCount() {
155: return -1;
156: }
157:
158: public int compareTo(Object o) {
159: if (o instanceof InspectorSummary) {
160: return ps.getName().compareTo(
161: ((InspectorSummary) o).getName());
162: } else if (o instanceof Comparable) {
163: return -((Comparable) o).compareTo(this );
164: } else {
165: return this .hashCode() - o.hashCode();
166: }
167: }
168: });
169: }
170: }
171: return severitySummary;
172: }
173:
174: private Collection warnings;
175:
176: public Collection getWarnings() {
177: if (warnings == null) {
178: warnings = new ArrayList();
179: Iterator it = collector.getEngine().getWarning().iterator();
180: while (it.hasNext()) {
181: final Warning warning = (Warning) it.next();
182: warnings.add(new Violation() {
183:
184: public String getMessage() {
185: return warning.getMessage();
186: }
187:
188: public InspectorDescriptor getDescriptor() {
189: return inspectorSet.getDescriptor(warning
190: .getInspector());
191: }
192:
193: private SimpleSourceMarker sourceMarker;
194:
195: {
196: if (warning.getSource() != null
197: && warning.getLine() != null
198: && warning.getCol() != null) {
199: sourceMarker = new SimpleSourceMarker(
200: warning.getCol().intValue(),
201: warning.getLine().intValue(),
202: warning.getSource(), null);
203:
204: sourceMarker.setSignature(warning
205: .getSourceSignature());
206: }
207: }
208:
209: public SourceMarker getSource() {
210: return sourceMarker;
211: }
212:
213: public int compareTo(Object o) {
214: if (o == this ) {
215: return 0;
216: } else if (o instanceof Violation) {
217: Violation v = (Violation) o;
218: int vline = v.getSource() == null ? 0 : v
219: .getSource().getLine();
220: int line = getSource() == null ? 0
221: : getSource().getLine();
222: if (vline == line) {
223: int vcolumn = v.getSource() == null ? 0
224: : v.getSource().getColumn();
225: int column = getSource() == null ? 0
226: : getSource().getColumn();
227: if (vcolumn == column) {
228: if (warning.getMessage() == null) {
229: return v.getMessage() == null ? 0
230: : 1;
231: } else {
232: if (v.getMessage() == null) {
233: return -1;
234: } else {
235: return warning
236: .getMessage()
237: .compareTo(
238: v
239: .getMessage());
240: }
241: }
242: } else {
243: return column - vcolumn;
244: }
245: } else {
246: return line - vline;
247: }
248: } else {
249: return 1;
250: }
251: }
252:
253: });
254: }
255: }
256: return warnings;
257: }
258:
259: public boolean hasWarnings() {
260: return summary.getHasWarnings();
261: }
262:
263: public void addWarning(Violation warning) {
264: throw new UnsupportedOperationException();
265: }
266:
267: public void addMetric(SourceMarker source, String name, double value) {
268: throw new UnsupportedOperationException();
269: }
270:
271: private Map metrics;
272:
273: public Map getMetrics() {
274: if (metrics == null) {
275: metrics = new TreeMap();
276: Iterator it = collector.getEngine().getMetricSummary()
277: .iterator();
278: while (it.hasNext()) {
279: final MetricSummary metric = (MetricSummary) it.next();
280: metrics.put(metric.getName(),
281: new com.pavelvlasov.metrics.Metric() {
282:
283: public int getNumber() {
284: return (int) metric.getMeasurements();
285: }
286:
287: public double getMin() {
288: return metric.getMinValue();
289: }
290:
291: public double getMax() {
292: return metric.getMaxValue();
293: }
294:
295: public double getAvg() {
296: return metric.getMetricTotal()
297: / metric.getMeasurements();
298: }
299:
300: public double getTotal() {
301: return metric.getMetricTotal();
302: }
303:
304: public void add(double value, long time) {
305: throw new UnsupportedOperationException();
306: }
307:
308: public void add(
309: com.pavelvlasov.metrics.Metric metric) {
310: throw new UnsupportedOperationException();
311: }
312:
313: public Collection getMeasurements() {
314: return null;
315: }
316:
317: public String getName() {
318: return metric.getName();
319: }
320:
321: public double getDeviation() {
322: // TODO - Calcualte deviation
323: return 0;
324: }
325:
326: });
327: }
328: }
329: return metrics;
330: }
331:
332: public void aggregate(AggregatedResults agregee) {
333: throw new UnsupportedOperationException();
334: }
335:
336: public void setReviewsNumber(long reviews) {
337: throw new UnsupportedOperationException();
338: }
339:
340: public void setCodeBase(long codeBase) {
341: throw new UnsupportedOperationException();
342: }
343:
344: public void addAnnotation(Annotation annotation) {
345: throw new UnsupportedOperationException();
346: }
347:
348: public Collection getAnnotations() {
349: return violations;
350: }
351:
352: public WaiverSet getWaiverSet() {
353: throw new UnsupportedOperationException();
354: }
355:
356: public void commit() {
357: // No action is required
358: }
359:
360: public boolean isNew() {
361: return summary.getMinState() == QuickResultsCollector.RESULT_NEW;
362: }
363:
364: public BasicResults getBaseLine() {
365: return null;
366: }
367:
368: public long getCodeBase() {
369: return summary.getCodebase();
370: }
371:
372: public String getDPMO() {
373: if (summary.getReviews() == 0) {
374: return "Not available, no reviews";
375: } else {
376: return String.valueOf((int) (1000000 * summary
377: .getViolationLevel() / summary.getReviews()))
378: + (summary.getHasWarnings() ? " (not accurate because of warnings)"
379: : "");
380: }
381: }
382:
383: public String getSigma() {
384: double p = 1.0 - summary.getViolationLevel()
385: / summary.getReviews();
386: if (summary.getReviews() == 0) {
387: return "No results";
388: } else if (p <= 0) {
389: return "Full incompliance";
390: } else if (p >= 1) {
391: return "Full compliance";
392: } else {
393: return MessageFormat.format("{0,number,#.###}",
394: new Object[] { new Double(SimpleAggregatedResults
395: .normsinv(p) + 1.5) })
396: + (summary.getHasWarnings() ? " (not accurate because of warnings)"
397: : "");
398: }
399: }
400:
401: public Number getMaxSeverity() {
402: return summary.getMaxSeverity();
403: }
404:
405: public long getReviewsNumber() {
406: return summary.getReviews();
407: }
408:
409: public double getViolationLevel() {
410: return summary.getViolationLevel();
411: }
412:
413: public int getViolationsNumber() {
414: return (int) summary.getViolations();
415: }
416:
417: public int getWaivedViolationsNumber() {
418: return (int) summary.getWaivedViolations();
419: }
420:
421: public Date getDate() {
422: Timestamp resultDate = summary.getResultDate();
423: return resultDate == null ? new Date() : new Date(resultDate
424: .getTime());
425: }
426: }
|