001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.api;
020
021/**
022 * An audit listener that counts how many {@link AuditEvent AuditEvents}
023 * of a given severity have been generated.
024 *
025 * @author lkuehne
026 */
027public final class SeverityLevelCounter implements AuditListener
028{
029    /** The severity level to watch out for. */
030    private 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    {
041        if (level == null) {
042            throw new IllegalArgumentException();
043        }
044        this.level = level;
045    }
046
047    /** {@inheritDoc} */
048    @Override
049    public void addError(AuditEvent evt)
050    {
051        if (level.equals(evt.getSeverityLevel())) {
052            count++;
053        }
054    }
055
056    /** {@inheritDoc} */
057    @Override
058    public void addException(AuditEvent evt, Throwable throwable)
059    {
060        if (SeverityLevel.ERROR.equals(level)) {
061            count++;
062        }
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public void auditStarted(AuditEvent evt)
068    {
069        count = 0;
070    }
071
072    /** {@inheritDoc} */
073    @Override
074    public void fileStarted(AuditEvent evt)
075    {
076    }
077
078    /** {@inheritDoc} */
079    @Override
080    public void auditFinished(AuditEvent evt)
081    {
082    }
083
084    /** {@inheritDoc} */
085    @Override
086    public void fileFinished(AuditEvent evt)
087    {
088    }
089
090    /**
091     * Returns the number of counted events since audit started.
092     * @return the number of counted events since audit started.
093     */
094    public int getCount()
095    {
096        return count;
097    }
098}