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 mLevel;
031
032    /** Keeps track of the number of counted events. */
033    private int mCount;
034
035    /**
036     * Creates a new counter.
037     * @param aLevel the severity level events need to have, must be non-null.
038     */
039    public SeverityLevelCounter(SeverityLevel aLevel)
040    {
041        if (aLevel == null) {
042            throw new IllegalArgumentException();
043        }
044        mLevel = aLevel;
045    }
046
047    /** {@inheritDoc} */
048    public void addError(AuditEvent aEvt)
049    {
050        if (mLevel.equals(aEvt.getSeverityLevel())) {
051            mCount++;
052        }
053    }
054
055    /** {@inheritDoc} */
056    public void addException(AuditEvent aEvt, Throwable aThrowable)
057    {
058        if (SeverityLevel.ERROR.equals(mLevel)) {
059            mCount++;
060        }
061    }
062
063    /** {@inheritDoc} */
064    public void auditStarted(AuditEvent aEvt)
065    {
066        mCount = 0;
067    }
068
069    /** {@inheritDoc} */
070    public void fileStarted(AuditEvent aEvt)
071    {
072    }
073
074    /** {@inheritDoc} */
075    public void auditFinished(AuditEvent aEvt)
076    {
077    }
078
079    /** {@inheritDoc} */
080    public void fileFinished(AuditEvent aEvt)
081    {
082    }
083
084    /**
085     * Returns the number of counted events since audit started.
086     * @return the number of counted events since audit started.
087     */
088    public int getCount()
089    {
090        return mCount;
091    }
092}