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
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    /** 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     * @param level the severity level events need to have, must be non-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.incrementAndGet();
052        }
053    }
054
055    @Override
056    public void addException(AuditEvent event, Throwable throwable) {
057        if (level == SeverityLevel.ERROR) {
058            count.incrementAndGet();
059        }
060    }
061
062    @Override
063    public void auditStarted(AuditEvent event) {
064        count.set(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     * @return the number of counted events since audit started.
085     */
086    public int getCount() {
087        return count.get();
088    }
089}