1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.filters;
21  
22  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
23  import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
24  import com.puppycrawl.tools.checkstyle.api.Filter;
25  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
26  
27  /**
28   * This is a very simple filter based on severity matching.
29   * The filter admits option severity and accepts an AuditEvent
30   * if its severity equals the filter's severity.
31   */
32  public class SeverityMatchFilter
33      extends AutomaticBean
34      implements Filter {
35  
36      /** The severity level to accept. */
37      private SeverityLevel severity = SeverityLevel.ERROR;
38  
39      /** Whether to accept or reject on severity matches. */
40      private boolean acceptOnMatch = true;
41  
42      /**
43       * Sets the severity level.
44       *
45       * @param severity  The new severity level
46       * @see SeverityLevel
47       */
48      public final void setSeverity(SeverityLevel severity) {
49          this.severity = severity;
50      }
51  
52      /**
53       * Sets whether to accept or reject on matching severity level.
54       * @param acceptOnMatch if true, accept on matches; if
55       *     false, reject on matches.
56       */
57      public final void setAcceptOnMatch(boolean acceptOnMatch) {
58          this.acceptOnMatch = acceptOnMatch;
59      }
60  
61      @Override
62      protected void finishLocalSetup() {
63          // No code by default
64      }
65  
66      @Override
67      public boolean accept(AuditEvent event) {
68          final boolean severityMatches = severity == event.getSeverityLevel();
69          return acceptOnMatch == severityMatches;
70      }
71  
72  }