Add ability to output HTML report of differences found between two versions.
[cfb.git] / prod / net / jaekl / cfb / analyze / Delta.java
1 package net.jaekl.cfb.analyze;
2
3 import java.io.PrintWriter;
4 import java.util.HashSet;
5
6 import net.jaekl.cfb.store.Run;
7 import net.jaekl.cfb.xml.BugInstance;
8
9 // Compute and store the delta (difference) between two analyses
10
11 public class Delta {
12         Run                  m_earlier;
13         Run                  m_later;
14         
15         HashSet<BugInstance> m_fixed;           // bugs that have been fixed
16         HashSet<BugInstance> m_common;  // bugs that are present in both versions
17         HashSet<BugInstance> m_new;             // bugs introduced in the new version
18         
19         public Delta(Analysis before, Analysis after)
20         {
21                 m_earlier = new Run(before);
22                 m_later = new Run(after);
23                 
24                 m_fixed = new HashSet<BugInstance>();
25                 m_common = new HashSet<BugInstance>();
26                 m_new = new HashSet<BugInstance>();
27                 
28                 computeDelta(before, after);
29         }
30         
31         public BugInstance[] getFixed() { return m_fixed.toArray(new BugInstance[m_fixed.size()]); }
32         public int getNumFixed() { return m_fixed.size(); }
33         
34         public BugInstance[] getCommon() { return m_common.toArray(new BugInstance[m_common.size()]); }
35         public int getNumCommon() { return m_common.size(); }
36         
37         public BugInstance[] getNew() { return m_new.toArray(new BugInstance[m_new.size()]); }
38         public int getNumNew() { return m_new.size(); }
39         
40         public Run getEarlier() { return m_earlier; } 
41         public Run getLater() { return m_later; }
42         
43         public void dump(PrintWriter pw) {
44                 pw.println("=========================");
45                 pw.println("  NEW BUGS (" + m_new.size() + ")");
46                 pw.println("-------------------------");
47                 for (BugInstance bug : m_new) {
48                         bug.dump(pw, 2);
49                 }
50                 
51                 pw.println("=========================");
52                 pw.println("  FIXED BUGS (" + m_fixed.size() + ")");
53                 pw.println("-------------------------");
54                 for (BugInstance bug : m_fixed) {
55                         bug.dump(pw, 2);
56                 }
57                 
58                 pw.println("=========================");
59                 pw.println("  OLD BUGS (" + m_common.size() + ")");
60                 pw.println("-------------------------");
61                 for (BugInstance bug : m_common) {
62                         bug.dump(pw, 2);
63                 }
64         }
65         
66         void computeDelta(Analysis before, Analysis after)
67         {
68                 m_fixed.clear();
69                 m_common.clear();
70                 m_new.clear();
71                 
72                 HashSet<BugInstance> beforeBugs = new HashSet<BugInstance>();
73                 
74                 if (null != before) {
75                         beforeBugs.addAll(before.getBugCollection().getBugs());
76                 }
77                 
78                 for (BugInstance bug : after.getBugCollection().getBugs()) {
79                         if (beforeBugs.contains(bug)) {
80                                 m_common.add(bug);
81                         }
82                         else {
83                                 m_new.add(bug);
84                         }
85                 }
86                 
87                 if (null == before) {
88                         return;
89                 }
90                 
91                 for (BugInstance bug : before.getBugCollection().getBugs()) {
92                         if (! m_common.contains(bug)) {
93                                 m_fixed.add(bug);
94                         }
95                 }
96         }
97 }