View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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  import java.util.concurrent.atomic.AtomicInteger;
23  
24  /**
25   * An audit listener that counts how many {@link AuditEvent AuditEvents}
26   * of a given severity have been generated.
27   *
28   */
29  public final class SeverityLevelCounter implements AuditListener {
30  
31      /** The severity level to watch out for. */
32      private final SeverityLevel level;
33  
34      /** Keeps track of the number of counted events. */
35      private final AtomicInteger count = new AtomicInteger();
36  
37      /**
38       * Creates a new counter.
39       *
40       * @param level the severity level events need to have, must be non-null.
41       * @throws IllegalArgumentException when level is null
42       */
43      public SeverityLevelCounter(SeverityLevel level) {
44          if (level == null) {
45              throw new IllegalArgumentException("'level' cannot be null");
46          }
47          this.level = level;
48      }
49  
50      @Override
51      public void addError(AuditEvent event) {
52          if (level == event.getSeverityLevel()) {
53              count.incrementAndGet();
54          }
55      }
56  
57      @Override
58      public void addException(AuditEvent event, Throwable throwable) {
59          if (level == SeverityLevel.ERROR) {
60              count.incrementAndGet();
61          }
62      }
63  
64      @Override
65      public void auditStarted(AuditEvent event) {
66          count.set(0);
67      }
68  
69      @Override
70      public void fileStarted(AuditEvent event) {
71          // No code by default, should be overridden only by demand at subclasses
72      }
73  
74      @Override
75      public void auditFinished(AuditEvent event) {
76          // No code by default, should be overridden only by demand at subclasses
77      }
78  
79      @Override
80      public void fileFinished(AuditEvent event) {
81          // No code by default, should be overridden only by demand at subclasses
82      }
83  
84      /**
85       * Returns the number of counted events since audit started.
86       *
87       * @return the number of counted events since audit started.
88       */
89      public int getCount() {
90          return count.get();
91      }
92  
93  }