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