Coverage Report - com.puppycrawl.tools.checkstyle.filters.SeverityMatchFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
SeverityMatchFilter
100%
10/10
100%
4/4
1
 
 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  10
 public class SeverityMatchFilter
 35  
     extends AutomaticBean
 36  
     implements Filter {
 37  
     /** The severity level to accept. */
 38  10
     private SeverityLevel severity = SeverityLevel.ERROR;
 39  
 
 40  
     /** Whether to accept or reject on severity matches. */
 41  10
     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  5
         this.severity = severity;
 51  5
     }
 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  2
         this.acceptOnMatch = acceptOnMatch;
 60  2
     }
 61  
 
 62  
     @Override
 63  
     protected void finishLocalSetup() throws CheckstyleException {
 64  
         // No code by default
 65  3
     }
 66  
 
 67  
     @Override
 68  
     public boolean accept(AuditEvent event) {
 69  9
         final boolean severityMatches = severity == event.getSeverityLevel();
 70  9
         return acceptOnMatch == severityMatches;
 71  
     }
 72  
 }