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.text.MessageFormat;
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Date;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.TreeMap;
033:
034: import org.hammurapi.results.AggregatedResults;
035: import org.hammurapi.results.Annotation;
036: import org.hammurapi.results.BasicResults;
037: import org.hammurapi.results.CompositeResults;
038: import org.hammurapi.results.InspectorSummary;
039: import org.hammurapi.results.NamedResults;
040: import org.hammurapi.results.quick.MetricSummary;
041: import org.hammurapi.results.quick.PackageTotal;
042: import org.hammurapi.results.quick.Result;
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.5 $
052: */
053: public class QuickPackageResults implements CompositeResults {
054:
055: private PackageTotal total;
056: private QuickResultsCollector collector;
057: private InspectorSet inspectorSet;
058:
059: /**
060: * @param total
061: * @param collector
062: */
063: public QuickPackageResults(PackageTotal total,
064: QuickResultsCollector collector, InspectorSet inspectorSet) {
065: this .total = total;
066: this .collector = collector;
067: this .inspectorSet = inspectorSet;
068: }
069:
070: private List children;
071:
072: public Collection getChildren() {
073: if (children == null) {
074: children = new ArrayList();
075: Iterator rit = collector.getEngine().getPackageResults(
076: total.getPackageName()).iterator();
077: while (rit.hasNext()) {
078: final Result result = (Result) rit.next();
079: children.add(new NamedResults() {
080:
081: public Waiver addViolation(Violation violation)
082: throws HammurapiException {
083: throw new UnsupportedOperationException();
084: }
085:
086: public Map getSeveritySummary() {
087: return null;
088: }
089:
090: public Collection getWarnings() {
091: return null;
092: }
093:
094: public boolean hasWarnings() {
095: return result.getHasWarnings();
096: }
097:
098: public void addWarning(Violation warning) {
099: throw new UnsupportedOperationException();
100: }
101:
102: public void addMetric(SourceMarker source,
103: String name, double value) {
104: throw new UnsupportedOperationException();
105: }
106:
107: public Map getMetrics() {
108: return null;
109: }
110:
111: public void aggregate(AggregatedResults agregee) {
112: throw new UnsupportedOperationException();
113: }
114:
115: public void setReviewsNumber(long reviews) {
116: throw new UnsupportedOperationException();
117: }
118:
119: public void setCodeBase(long codeBase) {
120: throw new UnsupportedOperationException();
121: }
122:
123: public void addAnnotation(Annotation annotation) {
124: throw new UnsupportedOperationException();
125: }
126:
127: public Collection getAnnotations() {
128: return null;
129: }
130:
131: public WaiverSet getWaiverSet() {
132: return null;
133: }
134:
135: public void commit() throws HammurapiException {
136:
137: }
138:
139: public boolean isNew() {
140: return result.getState() == QuickResultsCollector.RESULT_NEW;
141: }
142:
143: public BasicResults getBaseLine() {
144: return null;
145: }
146:
147: public long getCodeBase() {
148: return result.getCodebase();
149: }
150:
151: public String getDPMO() {
152: if (result.getReviews() == 0) {
153: return "Not available, no reviews";
154: } else {
155: return String
156: .valueOf((int) (1000000 * result
157: .getViolationLevel() / result
158: .getReviews()))
159: + (result.getHasWarnings() ? " (not accurate because of warnings)"
160: : "");
161: }
162: }
163:
164: public Number getMaxSeverity() {
165: return result.getMaxSeverity();
166: }
167:
168: public long getReviewsNumber() {
169: return result.getReviews();
170: }
171:
172: public String getSigma() {
173: double p = 1.0 - result.getViolationLevel()
174: / result.getReviews();
175: if (result.getReviews() == 0) {
176: return "No results";
177: } else if (p <= 0) {
178: return "Full incompliance";
179: } else if (p >= 1) {
180: return "Full compliance";
181: } else {
182: return MessageFormat
183: .format(
184: "{0,number,#.###}",
185: new Object[] { new Double(
186: SimpleAggregatedResults
187: .normsinv(p) + 1.5) })
188: + (result.getHasWarnings() ? " (not accurate because of warnings)"
189: : "");
190: }
191: }
192:
193: public double getViolationLevel() {
194: return result.getViolationLevel();
195: }
196:
197: public int getViolationsNumber() {
198: return (int) result.getViolations();
199: }
200:
201: public int getWaivedViolationsNumber() {
202: return (int) result.getWaivedViolations();
203: }
204:
205: public Date getDate() {
206: return new Date(result.getResultDate()
207: .getTime());
208: }
209:
210: public String getName() {
211: return result.getName();
212: }
213:
214: });
215: }
216: }
217: return children;
218: }
219:
220: public void add(AggregatedResults child) {
221: throw new UnsupportedOperationException();
222: }
223:
224: public int size() {
225: return total.getResultSize();
226: }
227:
228: private Collection violations = new ArrayList();
229:
230: public Collection getViolations() {
231: return violations;
232: }
233:
234: public Collection getWaivedViolations() {
235: return violations;
236: }
237:
238: public String getName() {
239: return total.getPackageName();
240: }
241:
242: public Waiver addViolation(Violation violation)
243: throws HammurapiException {
244: throw new UnsupportedOperationException();
245: }
246:
247: private Map severitySummary;
248:
249: public Map getSeveritySummary() {
250: if (severitySummary == null) {
251: severitySummary = new TreeMap();
252:
253: Iterator it = collector.getEngine()
254: .getPackageInspectorSummary(total.getPackageName())
255: .iterator();
256: while (it.hasNext()) {
257: final org.hammurapi.results.quick.InspectorSummary ps = (org.hammurapi.results.quick.InspectorSummary) it
258: .next();
259: Integer severity = new Integer(ps.getSeverity());
260: Map imap = (Map) severitySummary.get(severity);
261: if (imap == null) {
262: imap = new TreeMap();
263: severitySummary.put(severity, imap);
264: }
265: imap.put(ps.getName(), new InspectorSummary() {
266:
267: public String getDescription() {
268: return ps.getDescription();
269: }
270:
271: public List getLocations() {
272: return null;
273: }
274:
275: public int getLocationsCount() {
276: return (int) ps.getViolations();
277: }
278:
279: public String getName() {
280: return ps.getName();
281: }
282:
283: public String getVersion() {
284: return "";
285: }
286:
287: public Number getSeverity() {
288: return new Integer(ps.getSeverity());
289: }
290:
291: public String getConfigInfo() {
292: return ps.getConfigInfo();
293: }
294:
295: public int getBaseLineLocationsCount() {
296: return -1;
297: }
298:
299: public int compareTo(Object o) {
300: if (o instanceof InspectorSummary) {
301: return ps.getName().compareTo(
302: ((InspectorSummary) o).getName());
303: } else if (o instanceof Comparable) {
304: return -((Comparable) o).compareTo(this );
305: } else {
306: return this .hashCode() - o.hashCode();
307: }
308: }
309: });
310: }
311: }
312: return severitySummary;
313: }
314:
315: private Collection warnings;
316:
317: public Collection getWarnings() {
318: if (warnings == null) {
319: warnings = new ArrayList();
320: Iterator it = collector.getEngine().getWarningPackageEQ(
321: total.getPackageName()).iterator();
322: while (it.hasNext()) {
323: final Warning warning = (Warning) it.next();
324: warnings.add(new Violation() {
325:
326: public String getMessage() {
327: return warning.getMessage();
328: }
329:
330: public InspectorDescriptor getDescriptor() {
331: return inspectorSet.getDescriptor(warning
332: .getInspector());
333: }
334:
335: private SimpleSourceMarker sourceMarker;
336:
337: {
338: if (warning.getSource() != null
339: && warning.getLine() != null
340: && warning.getCol() != null) {
341: sourceMarker = new SimpleSourceMarker(
342: warning.getCol().intValue(),
343: warning.getLine().intValue(),
344: warning.getSource(), null);
345:
346: sourceMarker.setSignature(warning
347: .getSourceSignature());
348: }
349: }
350:
351: public SourceMarker getSource() {
352: return sourceMarker;
353: }
354:
355: public int compareTo(Object o) {
356: if (o == this ) {
357: return 0;
358: } else if (o instanceof Violation) {
359: Violation v = (Violation) o;
360: int vline = v.getSource() == null ? 0 : v
361: .getSource().getLine();
362: int line = getSource() == null ? 0
363: : getSource().getLine();
364: if (vline == line) {
365: int vcolumn = v.getSource() == null ? 0
366: : v.getSource().getColumn();
367: int column = getSource() == null ? 0
368: : getSource().getColumn();
369: if (vcolumn == column) {
370: if (warning.getMessage() == null) {
371: return v.getMessage() == null ? 0
372: : 1;
373: } else {
374: if (v.getMessage() == null) {
375: return -1;
376: } else {
377: return warning
378: .getMessage()
379: .compareTo(
380: v
381: .getMessage());
382: }
383: }
384: } else {
385: return column - vcolumn;
386: }
387: } else {
388: return line - vline;
389: }
390: } else {
391: return 1;
392: }
393: }
394:
395: });
396: }
397: }
398: return warnings;
399: }
400:
401: public boolean hasWarnings() {
402: return total.getHasWarnings();
403: }
404:
405: public void addWarning(Violation warning) {
406: throw new UnsupportedOperationException();
407: }
408:
409: public void addMetric(SourceMarker source, String name, double value) {
410: throw new UnsupportedOperationException();
411: }
412:
413: private Map metrics;
414:
415: public Map getMetrics() {
416: if (metrics == null) {
417: metrics = new TreeMap();
418: Iterator it = collector.getEngine().getPackageMetrics(
419: total.getPackageName()).iterator();
420: while (it.hasNext()) {
421: final MetricSummary metric = (MetricSummary) it.next();
422: metrics.put(metric.getName(),
423: new com.pavelvlasov.metrics.Metric() {
424:
425: public int getNumber() {
426: return (int) metric.getMeasurements();
427: }
428:
429: public double getMin() {
430: return metric.getMinValue();
431: }
432:
433: public double getMax() {
434: return metric.getMaxValue();
435: }
436:
437: public double getAvg() {
438: return metric.getMetricTotal()
439: / metric.getMeasurements();
440: }
441:
442: public double getTotal() {
443: return metric.getMetricTotal();
444: }
445:
446: public void add(double value, long time) {
447: throw new UnsupportedOperationException();
448: }
449:
450: public void add(
451: com.pavelvlasov.metrics.Metric metric) {
452: throw new UnsupportedOperationException();
453: }
454:
455: public Collection getMeasurements() {
456: return null;
457: }
458:
459: public String getName() {
460: return metric.getName();
461: }
462:
463: public double getDeviation() {
464: // TODO - Calcualte deviation
465: return 0;
466: }
467:
468: });
469: }
470: }
471: return metrics;
472: }
473:
474: public void aggregate(AggregatedResults agregee) {
475: throw new UnsupportedOperationException();
476: }
477:
478: public void setReviewsNumber(long reviews) {
479: throw new UnsupportedOperationException();
480: }
481:
482: public void setCodeBase(long codeBase) {
483: throw new UnsupportedOperationException();
484: }
485:
486: public void addAnnotation(Annotation annotation) {
487: throw new UnsupportedOperationException();
488: }
489:
490: public Collection getAnnotations() {
491: return violations;
492: }
493:
494: public WaiverSet getWaiverSet() {
495: throw new UnsupportedOperationException();
496: }
497:
498: public void commit() throws HammurapiException {
499:
500: }
501:
502: public boolean isNew() {
503: return total.getMinState() == QuickResultsCollector.RESULT_NEW;
504: }
505:
506: public BasicResults getBaseLine() {
507: return null;
508: }
509:
510: public long getCodeBase() {
511: return total.getCodebase();
512: }
513:
514: public String getDPMO() {
515: if (total.getReviews() == 0) {
516: return "Not available, no reviews";
517: } else {
518: return String.valueOf((int) (1000000 * total
519: .getViolationLevel() / total.getReviews()))
520: + (total.getHasWarnings() ? " (not accurate because of warnings)"
521: : "");
522: }
523: }
524:
525: public String getSigma() {
526: double p = 1.0 - total.getViolationLevel() / total.getReviews();
527: if (total.getReviews() == 0) {
528: return "No results";
529: } else if (p <= 0) {
530: return "Full incompliance";
531: } else if (p >= 1) {
532: return "Full compliance";
533: } else {
534: return MessageFormat.format("{0,number,#.###}",
535: new Object[] { new Double(SimpleAggregatedResults
536: .normsinv(p) + 1.5) })
537: + (total.getHasWarnings() ? " (not accurate because of warnings)"
538: : "");
539: }
540: }
541:
542: public Number getMaxSeverity() {
543: return total.getMaxSeverity();
544: }
545:
546: public long getReviewsNumber() {
547: return total.getReviews();
548: }
549:
550: public double getViolationLevel() {
551: return total.getViolationLevel();
552: }
553:
554: public int getViolationsNumber() {
555: return (int) total.getViolations();
556: }
557:
558: public int getWaivedViolationsNumber() {
559: return (int) total.getWaivedViolations();
560: }
561:
562: public Date getDate() {
563: return new Date(total.getResultDate().getTime());
564: }
565:
566: }
|