View Javadoc
1   /*
2   * Licensed to the Apache Software Foundation (ASF) under one   *
3   * or more contributor license agreements.  See the NOTICE file *
4   * distributed with this work for additional information        *
5   * regarding copyright ownership.  The ASF licenses this file   *
6   * to you under the Apache License, Version 2.0 (the            *
7   * "License"); you may not use this file except in compliance   *
8   * with the License.  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,   *
13  * software distributed under the License is distributed on an  *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15  * KIND, either express or implied.  See the License for the    *
16  * specific language governing permissions and limitations      *
17  * under the License.                                           *
18  */
19  
20  package org.apache.rat.report.claim.impl;
21  
22  import org.apache.rat.api.MetaData;
23  import org.apache.rat.api.RatException;
24  import org.apache.rat.report.claim.ClaimStatistic;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  
30  /**
31   * The aggregator is used to create a numerical statistic
32   * of claims.
33   */
34  public class ClaimAggregator extends AbstractClaimReporter {
35      private final ClaimStatistic statistic;
36      private final Map<String, Integer> numsByLicenseFamilyName = new HashMap<>();
37      private final Map<String, Integer> numsByLicenseFamilyCode = new HashMap<>();
38      private final Map<String, Integer> numsByFileType = new HashMap<>();
39      private int numApproved, numUnApproved, numGenerated, numUnknown;
40  
41      public ClaimAggregator(ClaimStatistic pStatistic) {
42          statistic = pStatistic;
43      }
44      
45      private void incMapValue(Map<String, Integer> pMap, String pKey) {
46          final Integer num = pMap.get(pKey);
47          final int newNum;
48          if (num == null) {
49              newNum = 1;
50          } else {
51              newNum = num + 1;
52          }
53          pMap.put(pKey, newNum);
54      }
55      
56      @Override
57      protected void handleDocumentCategoryClaim(String documentCategoryName) {
58          incMapValue(numsByFileType, documentCategoryName);
59      }
60  
61      @Override
62      protected void handleApprovedLicenseClaim(String licenseApproved) {
63          if (MetaData.RAT_APPROVED_LICENSE_VALUE_TRUE.equals(licenseApproved)) {
64              numApproved++;
65          } else {
66              numUnApproved++;
67          }
68      }
69  
70      @Override
71      protected void handleLicenseFamilyNameClaim(String licenseFamilyName) {
72          incMapValue(numsByLicenseFamilyName, licenseFamilyName);
73      }
74  
75      @Override
76      protected void handleHeaderCategoryClaim(String headerCategory) {
77          
78          if (MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_GEN.equals(headerCategory)) {
79              numGenerated++;
80              incMapValue(numsByLicenseFamilyCode, MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_GEN);
81          } else if (MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN.equals(headerCategory)) {
82              numUnknown++;
83              incMapValue(numsByLicenseFamilyCode, MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN);
84          }
85      }
86  
87      public void fillClaimStatistic(ClaimStatistic pStatistic) {
88          pStatistic.setDocumentCategoryMap(numsByFileType);
89          pStatistic.setLicenseFileCodeMap(numsByLicenseFamilyCode);
90          pStatistic.setLicenseFileNameMap(numsByLicenseFamilyName);
91          pStatistic.setNumApproved(numApproved);
92          pStatistic.setNumGenerated(numGenerated);
93          pStatistic.setNumUnApproved(numUnApproved);
94          pStatistic.setNumUnknown(numUnknown);
95      }
96  
97      @Override
98      public void endReport() throws RatException {
99          super.endReport();
100         fillClaimStatistic(statistic);
101     }
102 }