1 package org.codehaus.mojo.clirr;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import net.sf.clirr.core.ApiDifference;
20 import net.sf.clirr.core.DiffListenerAdapter;
21 import net.sf.clirr.core.Severity;
22
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.HashMap;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Map;
29
30
31
32
33
34
35 public class ClirrDiffListener
36 extends DiffListenerAdapter
37 implements MojoDiffListener
38 {
39
40
41
42 private List<ApiDifference> apiDifferences = new LinkedList<ApiDifference>();
43
44
45
46 private Map<Difference, List<ApiDifference>> ignoredApiDifferences = new HashMap<Difference, List<ApiDifference>>();
47
48
49
50
51 private Map<Severity, Integer> counts = new HashMap<Severity, Integer>( 3 );
52
53 @Override
54 public void reportDiff( ApiDifference apiDifference )
55 {
56 incrementCount( apiDifference.getMaximumSeverity(), counts );
57
58 apiDifferences.add( apiDifference );
59 }
60
61 public void reportIgnoredDiff( ApiDifference ignoredDiff, Difference reason )
62 {
63 List<ApiDifference> diffs = ignoredApiDifferences.get( reason );
64 if ( diffs == null ) {
65 diffs = new LinkedList<ApiDifference>();
66 ignoredApiDifferences.put( reason, diffs );
67 }
68 diffs.add( ignoredDiff );
69 }
70
71 @Override
72 public void stop()
73 {
74 Collections.sort( apiDifferences, new Comparator<ApiDifference>()
75 {
76 public int compare( ApiDifference d1, ApiDifference d2 )
77 {
78
79 return d2.getMaximumSeverity().compareTo( d1.getMaximumSeverity() );
80 }
81 } );
82 }
83
84 private void incrementCount( Severity sev, Map<Severity, Integer> counts )
85 {
86 if ( sev != null )
87 {
88 int count = getCount( counts, sev );
89 counts.put( sev, count + 1 );
90 }
91 }
92
93 private int getCount( Map<Severity, Integer> counts, Severity sev )
94 {
95 Integer count = counts.get( sev );
96 if ( count == null )
97 {
98 count = 0;
99 }
100 return count.intValue();
101 }
102
103 public List<ApiDifference> getApiDifferences()
104 {
105 return Collections.unmodifiableList( apiDifferences );
106 }
107
108 public Map<Difference, List<ApiDifference>> getIgnoredApiDifferences()
109 {
110 return Collections.unmodifiableMap( ignoredApiDifferences);
111 }
112
113 public int getSeverityCount( Severity severity )
114 {
115 return getCount( counts, severity );
116 }
117
118 }