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 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      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      CsvFilter(String pattern) {
50          final StringTokenizer tokenizer = new StringTokenizer(pattern, ",");
51          while (tokenizer.hasMoreTokens()) {
52              final String token = tokenizer.nextToken().trim();
53              final int index = token.indexOf('-');
54              if (index == -1) {
55                  final int matchValue = Integer.parseInt(token);
56                  addFilter(new IntMatchFilter(matchValue));
57              }
58              else {
59                  final int lowerBound =
60                      Integer.parseInt(token.substring(0, index));
61                  final int upperBound =
62                      Integer.parseInt(token.substring(index + 1));
63                  addFilter(new IntRangeFilter(lowerBound, upperBound));
64              }
65          }
66      }
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          filters.add(filter);
74      }
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          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          boolean result = false;
92          for (IntFilter filter : getFilters()) {
93              if (filter.accept(intValue)) {
94                  result = true;
95                  break;
96              }
97          }
98          return result;
99      }
100 
101     @Override
102     public boolean equals(Object object) {
103         if (this == object) {
104             return true;
105         }
106         if (object == null || getClass() != object.getClass()) {
107             return false;
108         }
109         final CsvFilter csvFilter = (CsvFilter) object;
110         return Objects.equals(filters, csvFilter.filters);
111     }
112 
113     @Override
114     public int hashCode() {
115         return Objects.hash(filters);
116     }
117 }