001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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.filters;
021
022import com.puppycrawl.tools.checkstyle.api.AuditEvent;
023import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
024import com.puppycrawl.tools.checkstyle.api.Filter;
025import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
026
027/**
028 * <p>
029 * Filter {@code SeverityMatchFilter} decides audit events according to the
030 * <a href="https://checkstyle.org/config.html#Severity">severity level</a> of the event.
031 * </p>
032 * <ul>
033 * <li>
034 * Property {@code severity} - Specify the severity level of this filter.
035 * Default value is {@code error}.
036 * </li>
037 * <li>
038 * Property {@code acceptOnMatch} - Control whether the filter accepts an audit
039 * event if and only if there is a match between the event's severity level and
040 * property severity. If acceptOnMatch is {@code false}, then the filter accepts
041 * an audit event if and only if there is not a match between the event's severity
042 * level and property severity.
043 * Default value is {@code true}.
044 * </li>
045 * </ul>
046 * <p>
047 * For example, the following configuration fragment directs the Checker to not
048 * report audit events with severity level {@code info}:
049 * </p>
050 * <pre>
051 * &lt;module name=&quot;SeverityMatchFilter&quot;&gt;
052 *   &lt;property name=&quot;severity&quot; value=&quot;info&quot;/&gt;
053 *   &lt;property name=&quot;acceptOnMatch&quot; value=&quot;false&quot;/&gt;
054 * &lt;/module&gt;
055 * </pre>
056 *
057 * @since 3.2
058 */
059public class SeverityMatchFilter
060    extends AutomaticBean
061    implements Filter {
062
063    /** Specify the severity level of this filter. */
064    private SeverityLevel severity = SeverityLevel.ERROR;
065
066    /**
067     * Control whether the filter accepts an audit event if and only if there
068     * is a match between the event's severity level and property severity.
069     * If acceptOnMatch is {@code false}, then the filter accepts an audit event
070     * if and only if there is not a match between the event's severity level
071     * and property severity.
072     */
073    private boolean acceptOnMatch = true;
074
075    /**
076     * Setter to specify the severity level of this filter.
077     *
078     * @param severity  The new severity level
079     * @see SeverityLevel
080     */
081    public final void setSeverity(SeverityLevel severity) {
082        this.severity = severity;
083    }
084
085    /**
086     * Setter to control whether the filter accepts an audit event if and only if there
087     * is a match between the event's severity level and property severity.
088     * If acceptOnMatch is {@code false}, then the filter accepts an audit event
089     * if and only if there is not a match between the event's severity level and property severity.
090     *
091     * @param acceptOnMatch if true, accept on matches; if
092     *     false, reject on matches.
093     */
094    public final void setAcceptOnMatch(boolean acceptOnMatch) {
095        this.acceptOnMatch = acceptOnMatch;
096    }
097
098    @Override
099    protected void finishLocalSetup() {
100        // No code by default
101    }
102
103    @Override
104    public boolean accept(AuditEvent event) {
105        final boolean severityMatches = severity == event.getSeverityLevel();
106        return acceptOnMatch == severityMatches;
107    }
108
109}