Coverage Report - com.puppycrawl.tools.checkstyle.api.SeverityLevelCounter
 
Classes in this File Line Coverage Branch Coverage Complexity
SeverityLevelCounter
100%
18/18
100%
6/6
1.5
 
 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.api;
 21  
 
 22  
 import java.util.concurrent.atomic.AtomicInteger;
 23  
 
 24  
 /**
 25  
  * An audit listener that counts how many {@link AuditEvent AuditEvents}
 26  
  * of a given severity have been generated.
 27  
  *
 28  
  * @author lkuehne
 29  
  */
 30  
 public final class SeverityLevelCounter implements AuditListener {
 31  
     /** The severity level to watch out for. */
 32  
     private final SeverityLevel level;
 33  
 
 34  
     /** Keeps track of the number of counted events. */
 35  2085
     private final AtomicInteger count = new AtomicInteger();
 36  
 
 37  
     /**
 38  
      * Creates a new counter.
 39  
      * @param level the severity level events need to have, must be non-null.
 40  
      */
 41  2085
     public SeverityLevelCounter(SeverityLevel level) {
 42  2085
         if (level == null) {
 43  1
             throw new IllegalArgumentException("'level' cannot be null");
 44  
         }
 45  2084
         this.level = level;
 46  2084
     }
 47  
 
 48  
     @Override
 49  
     public void addError(AuditEvent event) {
 50  72148
         if (level == event.getSeverityLevel()) {
 51  72120
             count.incrementAndGet();
 52  
         }
 53  72148
     }
 54  
 
 55  
     @Override
 56  
     public void addException(AuditEvent event, Throwable throwable) {
 57  3
         if (level == SeverityLevel.ERROR) {
 58  2
             count.incrementAndGet();
 59  
         }
 60  3
     }
 61  
 
 62  
     @Override
 63  
     public void auditStarted(AuditEvent event) {
 64  1564
         count.set(0);
 65  1564
     }
 66  
 
 67  
     @Override
 68  
     public void fileStarted(AuditEvent event) {
 69  
         // No code by default, should be overridden only by demand at subclasses
 70  1644
     }
 71  
 
 72  
     @Override
 73  
     public void auditFinished(AuditEvent event) {
 74  
         // No code by default, should be overridden only by demand at subclasses
 75  1551
     }
 76  
 
 77  
     @Override
 78  
     public void fileFinished(AuditEvent event) {
 79  
         // No code by default, should be overridden only by demand at subclasses
 80  1636
     }
 81  
 
 82  
     /**
 83  
      * Returns the number of counted events since audit started.
 84  
      * @return the number of counted events since audit started.
 85  
      */
 86  
     public int getCount() {
 87  1571
         return count.get();
 88  
     }
 89  
 }