Coverage Report - com.puppycrawl.tools.checkstyle.filters.CsvFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
CsvFilter
100%
34/34
100%
14/14
2.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.filters;
 21  
 
 22  
 import java.util.Collections;
 23  
 import java.util.HashSet;
 24  
 import java.util.Objects;
 25  
 import java.util.Set;
 26  
 import java.util.StringTokenizer;
 27  
 
 28  
 /**
 29  
  * <p>
 30  
  * This filter accepts an integer that matches a CSV value, where
 31  
  * each value is an integer or a range of integers.
 32  
  * </p>
 33  
  * @author Rick Giles
 34  
  * @author o_sukhodolsky
 35  
  */
 36  
 class CsvFilter implements IntFilter {
 37  
     /** Filter set. */
 38  27
     private final Set<IntFilter> filters = new HashSet<>();
 39  
 
 40  
     /**
 41  
      * Constructs a {@code CsvFilter} from a CSV, Comma-Separated Values,
 42  
      * string. Each value is an integer, or a range of integers. A range of
 43  
      * integers is of the form integer-integer, such as 1-10.
 44  
      * Note: integers must be non-negative.
 45  
      * @param pattern the CSV string.
 46  
      * @throws NumberFormatException if a component substring does not
 47  
      *     contain a parsable integer.
 48  
      */
 49  27
     CsvFilter(String pattern) {
 50  27
         final StringTokenizer tokenizer = new StringTokenizer(pattern, ",");
 51  63
         while (tokenizer.hasMoreTokens()) {
 52  37
             final String token = tokenizer.nextToken().trim();
 53  37
             final int index = token.indexOf('-');
 54  37
             if (index == -1) {
 55  20
                 final int matchValue = Integer.parseInt(token);
 56  19
                 addFilter(new IntMatchFilter(matchValue));
 57  19
             }
 58  
             else {
 59  17
                 final int lowerBound =
 60  17
                     Integer.parseInt(token.substring(0, index));
 61  17
                 final int upperBound =
 62  17
                     Integer.parseInt(token.substring(index + 1));
 63  17
                 addFilter(new IntRangeFilter(lowerBound, upperBound));
 64  
             }
 65  36
         }
 66  26
     }
 67  
 
 68  
     /**
 69  
      * Adds a IntFilter to the set.
 70  
      * @param filter the IntFilter to add.
 71  
      */
 72  
     public final void addFilter(IntFilter filter) {
 73  41
         filters.add(filter);
 74  41
     }
 75  
 
 76  
     /**
 77  
      * Returns the IntFilters of the filter set.
 78  
      * @return the IntFilters of the filter set.
 79  
      */
 80  
     protected Set<IntFilter> getFilters() {
 81  34
         return Collections.unmodifiableSet(filters);
 82  
     }
 83  
 
 84  
     /**
 85  
      * Determines whether an Integer matches a CSV integer value.
 86  
      * @param intValue the Integer to check.
 87  
      * @return true if intValue is an Integer that matches a CSV value.
 88  
      */
 89  
     @Override
 90  
     public boolean accept(int intValue) {
 91  33
         boolean result = false;
 92  33
         for (IntFilter filter : getFilters()) {
 93  41
             if (filter.accept(intValue)) {
 94  16
                 result = true;
 95  16
                 break;
 96  
             }
 97  25
         }
 98  33
         return result;
 99  
     }
 100  
 
 101  
     @Override
 102  
     public boolean equals(Object object) {
 103  373
         if (this == object) {
 104  42
             return true;
 105  
         }
 106  331
         if (object == null || getClass() != object.getClass()) {
 107  307
             return false;
 108  
         }
 109  24
         final CsvFilter csvFilter = (CsvFilter) object;
 110  24
         return Objects.equals(filters, csvFilter.filters);
 111  
     }
 112  
 
 113  
     @Override
 114  
     public int hashCode() {
 115  14
         return Objects.hash(filters);
 116  
     }
 117  
 }