View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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   * @author lkuehne
29   */
30  public final class SeverityLevelCounter implements AuditListener {
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       * @param level the severity level events need to have, must be non-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.incrementAndGet();
52          }
53      }
54  
55      @Override
56      public void addException(AuditEvent event, Throwable throwable) {
57          if (level == SeverityLevel.ERROR) {
58              count.incrementAndGet();
59          }
60      }
61  
62      @Override
63      public void auditStarted(AuditEvent event) {
64          count.set(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       * @return the number of counted events since audit started.
85       */
86      public int getCount() {
87          return count.get();
88      }
89  }