001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.api; 021 022/** 023 * An audit listener that counts how many {@link AuditEvent AuditEvents} 024 * of a given severity have been generated. 025 * 026 * @author lkuehne 027 */ 028public final class SeverityLevelCounter implements AuditListener { 029 /** The severity level to watch out for. */ 030 private final SeverityLevel level; 031 032 /** Keeps track of the number of counted events. */ 033 private int count; 034 035 /** 036 * Creates a new counter. 037 * @param level the severity level events need to have, must be non-null. 038 */ 039 public SeverityLevelCounter(SeverityLevel level) { 040 if (level == null) { 041 throw new IllegalArgumentException("'level' cannot be null"); 042 } 043 this.level = level; 044 } 045 046 @Override 047 public void addError(AuditEvent event) { 048 if (level == event.getSeverityLevel()) { 049 count++; 050 } 051 } 052 053 @Override 054 public void addException(AuditEvent event, Throwable throwable) { 055 if (level == SeverityLevel.ERROR) { 056 count++; 057 } 058 } 059 060 @Override 061 public void auditStarted(AuditEvent event) { 062 count = 0; 063 } 064 065 @Override 066 public void fileStarted(AuditEvent event) { 067 // No code by default, should be overridden only by demand at subclasses 068 } 069 070 @Override 071 public void auditFinished(AuditEvent event) { 072 // No code by default, should be overridden only by demand at subclasses 073 } 074 075 @Override 076 public void fileFinished(AuditEvent event) { 077 // No code by default, should be overridden only by demand at subclasses 078 } 079 080 /** 081 * Returns the number of counted events since audit started. 082 * @return the number of counted events since audit started. 083 */ 084 public int getCount() { 085 return count; 086 } 087}