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