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  package org.apache.maven.plugins.checkstyle.exec;
20  
21  import java.util.HashMap;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
27  import com.puppycrawl.tools.checkstyle.api.Configuration;
28  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
29  
30  /**
31   * Object holding the references to the CheckstyleResults.
32   * TODO: provide fallback to disk based storage if too many results.
33   *
34   * @author <a href="mailto:joakim@erdfelt.net">Joakim Erdfelt</a>
35   *
36   */
37  public class CheckstyleResults {
38      private Map<String, List<AuditEvent>> files;
39  
40      private Configuration configuration;
41  
42      public CheckstyleResults() {
43          files = new HashMap<>();
44      }
45  
46      public List<AuditEvent> getFileViolations(String file) {
47          List<AuditEvent> violations;
48  
49          if (this.files.containsKey(file)) {
50              violations = this.files.get(file);
51          } else {
52              violations = new LinkedList<>();
53              if (file != null) {
54                  this.files.put(file, violations);
55              }
56          }
57  
58          return violations;
59      }
60  
61      public void setFileViolations(String file, List<AuditEvent> violations) {
62          if (file != null) {
63              this.files.put(file, violations);
64          }
65      }
66  
67      public Map<String, List<AuditEvent>> getFiles() {
68          return files;
69      }
70  
71      public void setFiles(Map<String, List<AuditEvent>> files) {
72          this.files = files;
73      }
74  
75      public int getFileCount() {
76          return this.files.size();
77      }
78  
79      public long getSeverityCount(SeverityLevel level) {
80          long count = 0;
81  
82          for (List<AuditEvent> errors : this.files.values()) {
83              count = count + getSeverityCount(errors, level);
84          }
85  
86          return count;
87      }
88  
89      public long getSeverityCount(String file, SeverityLevel level) {
90          long count = 0;
91  
92          if (!this.files.containsKey(file)) {
93              return count;
94          }
95  
96          List<AuditEvent> violations = this.files.get(file);
97  
98          count = getSeverityCount(violations, level);
99  
100         return count;
101     }
102 
103     public long getSeverityCount(List<AuditEvent> violations, SeverityLevel level) {
104         long count = 0;
105 
106         for (AuditEvent event : violations) {
107             if (event.getSeverityLevel().equals(level)) {
108                 count++;
109             }
110         }
111 
112         return count;
113     }
114 
115     public Configuration getConfiguration() {
116         return configuration;
117     }
118 
119     public void setConfiguration(Configuration configuration) {
120         this.configuration = configuration;
121     }
122 }