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