1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2014 Oliver Burn
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 package com.puppycrawl.tools.checkstyle.api;
20
21 /**
22 * An audit listener that counts how many {@link AuditEvent AuditEvents}
23 * of a given severity have been generated.
24 *
25 * @author lkuehne
26 */
27 public final class SeverityLevelCounter implements AuditListener
28 {
29 /** The severity level to watch out for. */
30 private SeverityLevel level;
31
32 /** Keeps track of the number of counted events. */
33 private int count;
34
35 /**
36 * Creates a new counter.
37 * @param level the severity level events need to have, must be non-null.
38 */
39 public SeverityLevelCounter(SeverityLevel level)
40 {
41 if (level == null) {
42 throw new IllegalArgumentException();
43 }
44 this.level = level;
45 }
46
47 /** {@inheritDoc} */
48 @Override
49 public void addError(AuditEvent evt)
50 {
51 if (level.equals(evt.getSeverityLevel())) {
52 count++;
53 }
54 }
55
56 /** {@inheritDoc} */
57 @Override
58 public void addException(AuditEvent evt, Throwable throwable)
59 {
60 if (SeverityLevel.ERROR.equals(level)) {
61 count++;
62 }
63 }
64
65 /** {@inheritDoc} */
66 @Override
67 public void auditStarted(AuditEvent evt)
68 {
69 count = 0;
70 }
71
72 /** {@inheritDoc} */
73 @Override
74 public void fileStarted(AuditEvent evt)
75 {
76 }
77
78 /** {@inheritDoc} */
79 @Override
80 public void auditFinished(AuditEvent evt)
81 {
82 }
83
84 /** {@inheritDoc} */
85 @Override
86 public void fileFinished(AuditEvent evt)
87 {
88 }
89
90 /**
91 * Returns the number of counted events since audit started.
92 * @return the number of counted events since audit started.
93 */
94 public int getCount()
95 {
96 return count;
97 }
98 }