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 * 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 * </div>
032 *
033 * <p>
034 * Notes:
035 * SeverityMatchFilter can suppress Checks that have Treewalker or Checker as parent module.
036 * </p>
037 *
038 * @since 3.2
039 */
040public class SeverityMatchFilter
041    extends AbstractAutomaticBean
042    implements Filter {
043
044    /** Specify the severity level of this filter. */
045    private SeverityLevel severity = SeverityLevel.ERROR;
046
047    /**
048     * Control whether the filter accepts an audit event if and only if there
049     * is a match between the event's severity level and property severity.
050     * If acceptOnMatch is {@code false}, then the filter accepts an audit event
051     * if and only if there is not a match between the event's severity level
052     * and property severity.
053     */
054    private boolean acceptOnMatch = true;
055
056    /**
057     * Creates a new {@code SeverityMatchFilter} instance.
058     */
059    public SeverityMatchFilter() {
060        // no code by default
061    }
062
063    /**
064     * Setter to specify the severity level of this filter.
065     *
066     * @param severity  The new severity level
067     * @see SeverityLevel
068     * @since 3.2
069     */
070    public final void setSeverity(SeverityLevel severity) {
071        this.severity = severity;
072    }
073
074    /**
075     * Setter to control whether the filter accepts an audit event if and only if there
076     * is a match between the event's severity level and property severity.
077     * If acceptOnMatch is {@code false}, then the filter accepts an audit event
078     * if and only if there is not a match between the event's severity level and property severity.
079     *
080     * @param acceptOnMatch if true, accept on matches; if
081     *     false, reject on matches.
082     * @since 3.2
083     */
084    public final void setAcceptOnMatch(boolean acceptOnMatch) {
085        this.acceptOnMatch = acceptOnMatch;
086    }
087
088    @Override
089    protected void finishLocalSetup() {
090        // No code by default
091    }
092
093    @Override
094    public boolean accept(AuditEvent event) {
095        final boolean severityMatches = severity == event.getSeverityLevel();
096        return acceptOnMatch == severityMatches;
097    }
098
099}