51971e06c7589a01336a4bfce8b8ba0785f9a04b
[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.xml.BugInstance;
7
8 // Compute and store the delta (difference) between two analyses
9
10 public class Delta {
11         HashSet<BugInstance> m_fixed;           // bugs that have been fixed
12         HashSet<BugInstance> m_common;  // bugs that are present in both versions
13         HashSet<BugInstance> m_new;             // bugs introduced in the new version
14         
15         public Delta(Analysis before, Analysis after)
16         {
17                 m_fixed = new HashSet<BugInstance>();
18                 m_common = new HashSet<BugInstance>();
19                 m_new = new HashSet<BugInstance>();
20                 
21                 computeDelta(before, after);
22         }
23         
24         public BugInstance[] getFixed() { return m_fixed.toArray(new BugInstance[m_fixed.size()]); }
25         public int getNumFixed() { return m_fixed.size(); }
26         
27         public BugInstance[] getCommon() { return m_common.toArray(new BugInstance[m_common.size()]); }
28         public int getNumCommon() { return m_common.size(); }
29         
30         public BugInstance[] getNew() { return m_new.toArray(new BugInstance[m_new.size()]); }
31         public int getNumNew() { return m_new.size(); }
32         
33         public void dump(PrintWriter pw) {
34                 pw.println("=========================");
35                 pw.println("  NEW BUGS (" + m_new.size() + ")");
36                 pw.println("-------------------------");
37                 for (BugInstance bug : m_new) {
38                         bug.dump(pw, 2);
39                 }
40                 
41                 pw.println("=========================");
42                 pw.println("  FIXED BUGS (" + m_fixed.size() + ")");
43                 pw.println("-------------------------");
44                 for (BugInstance bug : m_fixed) {
45                         bug.dump(pw, 2);
46                 }
47                 
48                 pw.println("=========================");
49                 pw.println("  OLD BUGS (" + m_common.size() + ")");
50                 pw.println("-------------------------");
51                 for (BugInstance bug : m_common) {
52                         bug.dump(pw, 2);
53                 }
54         }
55         
56         void computeDelta(Analysis before, Analysis after)
57         {
58                 m_fixed.clear();
59                 m_common.clear();
60                 m_new.clear();
61                 
62                 HashSet<BugInstance> beforeBugs = new HashSet<BugInstance>();
63                 
64                 if (null != before) {
65                         beforeBugs.addAll(before.getBugCollection().getBugs());
66                 }
67                 
68                 for (BugInstance bug : after.getBugCollection().getBugs()) {
69                         if (beforeBugs.contains(bug)) {
70                                 m_common.add(bug);
71                         }
72                         else {
73                                 m_new.add(bug);
74                         }
75                 }
76                 
77                 if (null == before) {
78                         return;
79                 }
80                 
81                 for (BugInstance bug : before.getBugCollection().getBugs()) {
82                         if (! m_common.contains(bug)) {
83                                 m_fixed.add(bug);
84                         }
85                 }
86         }
87 }