View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.CheckstyleException;
25  import com.puppycrawl.tools.checkstyle.api.Filter;
26  import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
27  
28  /**
29   * This is a very simple filter based on severity matching.
30   * The filter admits option severity and accepts an AuditEvent
31   * if its severity equals the filter's severity.
32   * @author Rick Giles
33   */
34  public class SeverityMatchFilter
35      extends AutomaticBean
36      implements Filter {
37      /** The severity level to accept. */
38      private SeverityLevel severity = SeverityLevel.ERROR;
39  
40      /** Whether to accept or reject on severity matches. */
41      private boolean acceptOnMatch = true;
42  
43      /**
44       * Sets the severity level.
45       *
46       * @param severity  The new severity level
47       * @see SeverityLevel
48       */
49      public final void setSeverity(SeverityLevel severity) {
50          this.severity = severity;
51      }
52  
53      /**
54       * Sets whether to accept or reject on matching severity level.
55       * @param acceptOnMatch if true, accept on matches; if
56       *     false, reject on matches.
57       */
58      public final void setAcceptOnMatch(boolean acceptOnMatch) {
59          this.acceptOnMatch = acceptOnMatch;
60      }
61  
62      @Override
63      protected void finishLocalSetup() throws CheckstyleException {
64          // No code by default
65      }
66  
67      @Override
68      public boolean accept(AuditEvent event) {
69          final boolean severityMatches = severity == event.getSeverityLevel();
70          return acceptOnMatch == severityMatches;
71      }
72  }