]> gerrit.simantics Code Review - simantics/platform.git/blob
b3bfcd88902bcaf5f9ce731ee1a2b54f05797b0d
[simantics/platform.git] /
1 package org.simantics.scl.compiler.module.coverage;\r
2 \r
3 import org.simantics.scl.compiler.module.Module;\r
4 import org.simantics.scl.runtime.profiling.BranchPoint;\r
5 \r
6 import gnu.trove.map.hash.THashMap;\r
7 import gnu.trove.procedure.TObjectObjectProcedure;\r
8 \r
9 public class CoverageBuilder {\r
10     THashMap<String, THashMap<String, BranchPoint[]>> combined =\r
11             new THashMap<String, THashMap<String, BranchPoint[]>>();\r
12     \r
13     public void addCoverage(Module module) {\r
14         THashMap<String, BranchPoint[]> branchPointMap = module.getBranchPoints();\r
15         if(branchPointMap == null)\r
16             return;\r
17         THashMap<String, BranchPoint[]> oldBranchPointMap = combined.get(module.getName());\r
18         if(oldBranchPointMap == null) {\r
19             oldBranchPointMap = new THashMap<String, BranchPoint[]>();\r
20             combined.put(module.getName(), oldBranchPointMap);\r
21         }\r
22         THashMap<String, BranchPoint[]> oldBranchPointMap_ = oldBranchPointMap;\r
23         branchPointMap.forEachEntry(new TObjectObjectProcedure<String, BranchPoint[]>() {\r
24             @Override\r
25             public boolean execute(String name, BranchPoint[] branchPoints) {\r
26                 BranchPoint[] oldBranchPoints = oldBranchPointMap_.get(name);\r
27                 if(oldBranchPoints == null)\r
28                     oldBranchPointMap_.put(name, branchPoints);\r
29                 else\r
30                     combineCounters(oldBranchPoints, branchPoints);\r
31                 return true;\r
32             }\r
33         });\r
34     }\r
35     \r
36     private static void combineCounters(BranchPoint[] oldBranchPoints, BranchPoint[] branchPoints) {\r
37         if(oldBranchPoints.length != branchPoints.length)\r
38             throw new IllegalArgumentException("Incompatible branch points.");\r
39         for(int i=0;i<branchPoints.length;++i) {\r
40             BranchPoint oldBP = oldBranchPoints[i];\r
41             BranchPoint newBP = branchPoints[i];\r
42             if(oldBP.location != newBP.location)\r
43                 throw new IllegalArgumentException("Incompatible branch points.");\r
44             oldBP.visitCounter += newBP.visitCounter;\r
45             combineCounters(oldBP.children, newBP.children);\r
46         }\r
47     }\r
48     \r
49     public CombinedCoverage getCoverage() {\r
50         THashMap<String,ModuleCoverage> moduleCoverages =\r
51                 new THashMap<String,ModuleCoverage>();\r
52         combined.forEachEntry(new TObjectObjectProcedure<String, THashMap<String, BranchPoint[]>>() {\r
53             @Override\r
54             public boolean execute(String name, THashMap<String, BranchPoint[]> branchPoints) {\r
55                 moduleCoverages.put(name, CoverageUtils.getCoverage(name, branchPoints));\r
56                 return true;\r
57             }\r
58         });\r
59         return CoverageUtils.combineCoverages(moduleCoverages);\r
60     }\r
61 }\r