001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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.AbstractAutomaticBean;
023import com.puppycrawl.tools.checkstyle.api.AuditEvent;
024import com.puppycrawl.tools.checkstyle.api.Filter;
025import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
026
027/**
028 * <div>
029 * Accept violation to be present in audit report according to the
030 * <a href="https://checkstyle.org/config.html#Severity">severity level</a>.
031 * </div>
032 *
033 * <p>
034 * Notes:
035 * SeverityMatchFilter can suppress Checks that have Treewalker or Checker as parent module.
036 * </p>
037 *
038 * <p>
039 * In Checkstyle, when a filter accepts a violation
040 * (i.e. {@code accept()} returns {@code true}), the violation is kept in the
041 * final report; to make a violation disappear from the report, the filter must
042 * reject it by returning {@code false}. For {@code SeverityMatchFilter}, set
043 * {@code severity} to the level you want to control and {@code acceptOnMatch}
044 * to {@code true} to keep matching violations in the final report, or to
045 * {@code false} to remove them from it.
046 * </p>
047 *
048 * @since 3.2
049 */
050public class SeverityMatchFilter
051    extends AbstractAutomaticBean
052    implements Filter {
053
054    /** Specify the severity level of this filter. */
055    private SeverityLevel severity = SeverityLevel.ERROR;
056
057    /**
058     * Control whether the filter accepts an audit event if and only if there
059     * is a match between the event's severity level and property severity.
060     * If acceptOnMatch is {@code false}, then the filter accepts an audit event
061     * if and only if there is not a match between the event's severity level
062     * and property severity.
063     */
064    private boolean acceptOnMatch = true;
065
066    /**
067     * Creates a new {@code SeverityMatchFilter} instance.
068     */
069    public SeverityMatchFilter() {
070        // no code by default
071    }
072
073    /**
074     * Setter to specify the severity level of this filter.
075     *
076     * @param severity  The new severity level
077     * @see SeverityLevel
078     * @since 3.2
079     */
080    public final void setSeverity(SeverityLevel severity) {
081        this.severity = severity;
082    }
083
084    /**
085     * Setter to control whether the filter accepts an audit event if and only if there
086     * is a match between the event's severity level and property severity.
087     * If acceptOnMatch is {@code false}, then the filter accepts an audit event
088     * if and only if there is not a match between the event's severity level and property severity.
089     *
090     * @param acceptOnMatch if true, accept on matches; if
091     *     false, reject on matches.
092     * @since 3.2
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}