View Javadoc
1   package org.codehaus.mojo.clirr;
2   
3   /*
4    * Copyright 2006 The Codehaus
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Listen to the Clirr events.
32   *
33   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
34   */
35  public class ClirrDiffListener
36      extends DiffListenerAdapter
37      implements MojoDiffListener
38  {
39      /**
40       * The list of differences that occurred.
41       */
42      private List<ApiDifference> apiDifferences = new LinkedList<ApiDifference>();
43      /**
44       * The list of ignored differences that occurred.
45       */
46      private Map<Difference, List<ApiDifference>> ignoredApiDifferences = new HashMap<Difference, List<ApiDifference>>();
47  
48      /**
49       * The number messages for each severity.
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                  // compare maximum severities - order highest to lowest.
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 }