View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.api;
21  
22  /**
23   * An audit listener that counts how many {@link AuditEvent AuditEvents}
24   * of a given severity have been generated.
25   *
26   */
27  public final class SeverityLevelCounter implements AuditListener {
28  
29      /** The severity level to watch out for. */
30      private final SeverityLevel level;
31  
32      /** Keeps track of the number of counted events. */
33      private int count;
34  
35      /**
36       * Creates a new counter.
37       *
38       * @param level the severity level events need to have, must be non-null.
39       * @throws IllegalArgumentException when level is null
40       */
41      public SeverityLevelCounter(SeverityLevel level) {
42          if (level == null) {
43              throw new IllegalArgumentException("'level' cannot be null");
44          }
45          this.level = level;
46      }
47  
48      @Override
49      public void addError(AuditEvent event) {
50          if (level == event.getSeverityLevel()) {
51              count++;
52          }
53      }
54  
55      @Override
56      public void addException(AuditEvent event, Throwable throwable) {
57          if (level == SeverityLevel.ERROR) {
58              count++;
59          }
60      }
61  
62      @Override
63      public void auditStarted(AuditEvent event) {
64          count = 0;
65      }
66  
67      @Override
68      public void fileStarted(AuditEvent event) {
69          // No code by default, should be overridden only by demand at subclasses
70      }
71  
72      @Override
73      public void auditFinished(AuditEvent event) {
74          // No code by default, should be overridden only by demand at subclasses
75      }
76  
77      @Override
78      public void fileFinished(AuditEvent event) {
79          // No code by default, should be overridden only by demand at subclasses
80      }
81  
82      /**
83       * Returns the number of counted events since audit started.
84       *
85       * @return the number of counted events since audit started.
86       */
87      public int getCount() {
88          return count;
89      }
90  
91  }