001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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 022import java.util.concurrent.atomic.AtomicInteger; 023 024/** 025 * An audit listener that counts how many {@link AuditEvent AuditEvents} 026 * of a given severity have been generated. 027 * 028 * @author lkuehne 029 */ 030public final class SeverityLevelCounter implements AuditListener { 031 032 /** The severity level to watch out for. */ 033 private final SeverityLevel level; 034 035 /** Keeps track of the number of counted events. */ 036 private final AtomicInteger count = new AtomicInteger(); 037 038 /** 039 * Creates a new counter. 040 * @param level the severity level events need to have, must be non-null. 041 */ 042 public SeverityLevelCounter(SeverityLevel level) { 043 if (level == null) { 044 throw new IllegalArgumentException("'level' cannot be null"); 045 } 046 this.level = level; 047 } 048 049 @Override 050 public void addError(AuditEvent event) { 051 if (level == event.getSeverityLevel()) { 052 count.incrementAndGet(); 053 } 054 } 055 056 @Override 057 public void addException(AuditEvent event, Throwable throwable) { 058 if (level == SeverityLevel.ERROR) { 059 count.incrementAndGet(); 060 } 061 } 062 063 @Override 064 public void auditStarted(AuditEvent event) { 065 count.set(0); 066 } 067 068 @Override 069 public void fileStarted(AuditEvent event) { 070 // No code by default, should be overridden only by demand at subclasses 071 } 072 073 @Override 074 public void auditFinished(AuditEvent event) { 075 // No code by default, should be overridden only by demand at subclasses 076 } 077 078 @Override 079 public void fileFinished(AuditEvent event) { 080 // No code by default, should be overridden only by demand at subclasses 081 } 082 083 /** 084 * Returns the number of counted events since audit started. 085 * @return the number of counted events since audit started. 086 */ 087 public int getCount() { 088 return count.get(); 089 } 090 091}