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;
21  
22  import java.util.Collections;
23  import java.util.Set;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  import org.apache.rat.api.Document;
27  
28  /**
29   * This class provides a numerical overview about
30   * the report.
31   */
32  public class ClaimStatistic {
33      /** The counter types */
34      public enum Counter { 
35          /** count of approved files */
36          APPROVED, 
37          /** count of unapproved files */
38          UNAPPROVED, 
39          /** count of generated files */
40          GENERATED, 
41          /** count of unknown files */
42          UNKNOWN }
43      
44      private final ConcurrentHashMap<String, IntCounter> licenseFamilyNameMap = new ConcurrentHashMap<>();
45      private final ConcurrentHashMap<String, IntCounter> licenseFamilyCategoryMap = new ConcurrentHashMap<>();
46      private final ConcurrentHashMap<Document.Type, IntCounter> documentCategoryMap = new ConcurrentHashMap<>();
47      private final ConcurrentHashMap<ClaimStatistic.Counter, IntCounter> counterMap = new ConcurrentHashMap<>();
48  
49      /** converts null counter to 0.
50       *
51       * @param counter the Counter to retrieve the value from.
52       * @return 0 if counter is {@code null} or counter value otherwise.
53       */
54      private int getValue(IntCounter counter) {
55          return counter == null ? 0 : counter.value();
56      }
57      /**
58       * Returns the counts for the counter.
59       * @param counter the counter to get the value for.
60       * @return Returns the number times the Counter type was seen.
61       */
62      public int getCounter(Counter counter) {
63          return getValue(counterMap.get(counter));
64      }
65  
66      /**
67       * Increments the counts for hte counter.
68       * @param counter the counter to increment.
69       * @param value the value to increment the counter by.
70       */
71      public void incCounter(Counter counter, int value) {
72          counterMap.compute(counter, (k,v)-> v == null? new IntCounter().increment(value) : v.increment(value));
73      }
74  
75      /**
76       * Gets the counts for the Document.Type.
77       * @param documentType the Document.Type to get the counter for.
78       * @return Returns the number times the Document.Type was seen
79       */
80      public int getCounter(Document.Type documentType) {
81          return getValue(documentCategoryMap.get(documentType));
82      }
83  
84      /**
85       * Increments the number of times the Document.Type was seen.
86       * @param documentType the Document.Type to increment.
87       * @param value the vlaue to increment the counter by.
88       */
89      public void incCounter(Document.Type documentType, int value) {
90          documentCategoryMap.compute(documentType, (k,v)-> v == null? new IntCounter().increment(value) : v.increment(value));
91      }
92  
93      /**
94       * Gets the counts for hte license category.
95       * @param licenseFamilyCategory the license family category to get the count for.
96       * @return the number of times the license family category was seen.
97       */
98      public int getLicenseCategoryCount(String licenseFamilyCategory) {
99          return getValue(licenseFamilyCategoryMap.get(licenseFamilyCategory));
100     }
101 
102     /**
103      * Increments the number of times a license family category was seen.
104      * @param licenseFamilyCategory the License family category to incmrement.
105      * @param value the value to increment the count by.
106      */
107     public void incLicenseCategoryCount(String licenseFamilyCategory, int value) {
108         licenseFamilyCategoryMap.compute(licenseFamilyCategory, (k, v)-> v == null? new IntCounter().increment(value) : v.increment(value));
109     }
110 
111     /**
112      * Gets the set of license family categories that were seen.
113      * @return A set of license family categories.
114      */
115     public Set<String> getLicenseFamilyCategories() {
116         return Collections.unmodifiableSet(licenseFamilyCategoryMap.keySet());
117     }
118 
119     /**
120      * Gets the set of license family names that were seen.
121      * @return a Set of license family names that were seen.
122      */
123     public Set<String> getLicenseFamilyNames() {
124         return Collections.unmodifiableSet(licenseFamilyNameMap.keySet());
125     }
126 
127     /**
128      * Retrieves the number of times a license family name was seen.
129      * @param licenseFilename the license family name to look for.
130      * @return the number of times the license family name was seen.
131      */
132     public int getLicenseFamilyNameCount(String licenseFilename) {
133         return getValue(licenseFamilyNameMap.get(licenseFilename));
134     }
135 
136     /**
137      * Increments the license family name count.
138      * @param licenseFamilyName the license family name to increment.
139      * @param value the value to increment the count by.
140      */
141     public void incLicenseFamilyNameCount(String licenseFamilyName, int value) {
142         licenseFamilyNameMap.compute(licenseFamilyName, (k,v)-> v == null? new IntCounter().increment(value) : v.increment(value));
143     }
144 
145     /**
146      * A class that wraps and int and allows easy increment and retrieval.
147      */
148     static class IntCounter {
149         int value = 0;
150 
151         /**
152          * Increment the count.
153          * @param count the count to increment by (may be negative)
154          * @return this.
155          */
156         public IntCounter increment(int count) {
157             value += count;
158             return this;
159         }
160 
161         /**
162          * Retrieves the count.
163          * @return the count contained by this counter.
164          */
165         public int value() {
166             return value;
167         }
168     }
169 }