1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2019 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 element is immutable and accepts an integer that matches a CSV value, where
31 * each value is an integer or a range of integers.
32 * </p>
33 */
34 class CsvFilterElement implements IntFilterElement {
35
36 /** Filter set. */
37 private final Set<IntFilterElement> filters = new HashSet<>();
38
39 /**
40 * Constructs a {@code CsvFilterElement} from a CSV, Comma-Separated Values,
41 * string. Each value is an integer, or a range of integers. A range of
42 * integers is of the form integer-integer, such as 1-10.
43 * Note: integers must be non-negative.
44 * @param pattern the CSV string.
45 * @throws NumberFormatException if a component substring does not
46 * contain a parsable integer.
47 */
48 /* package */ CsvFilterElement(String pattern) {
49 final StringTokenizer tokenizer = new StringTokenizer(pattern, ",");
50 while (tokenizer.hasMoreTokens()) {
51 final String token = tokenizer.nextToken().trim();
52 final int index = token.indexOf('-');
53 if (index == -1) {
54 final int matchValue = Integer.parseInt(token);
55 addFilter(new IntMatchFilterElement(matchValue));
56 }
57 else {
58 final int lowerBound =
59 Integer.parseInt(token.substring(0, index));
60 final int upperBound =
61 Integer.parseInt(token.substring(index + 1));
62 addFilter(new IntRangeFilterElement(lowerBound, upperBound));
63 }
64 }
65 }
66
67 /**
68 * Adds a IntFilterElement to the set.
69 * @param filter the IntFilterElement to add.
70 */
71 private void addFilter(IntFilterElement filter) {
72 filters.add(filter);
73 }
74
75 /**
76 * Returns the IntFilters of the filter set.
77 * @return the IntFilters of the filter set.
78 */
79 protected Set<IntFilterElement> getFilters() {
80 return Collections.unmodifiableSet(filters);
81 }
82
83 /**
84 * Determines whether an Integer matches a CSV integer value.
85 * @param intValue the Integer to check.
86 * @return true if intValue is an Integer that matches a CSV value.
87 */
88 @Override
89 public boolean accept(int intValue) {
90 boolean result = false;
91 for (IntFilterElement filter : getFilters()) {
92 if (filter.accept(intValue)) {
93 result = true;
94 break;
95 }
96 }
97 return result;
98 }
99
100 @Override
101 public boolean equals(Object object) {
102 if (this == object) {
103 return true;
104 }
105 if (object == null || getClass() != object.getClass()) {
106 return false;
107 }
108 final CsvFilterElement csvFilter = (CsvFilterElement) object;
109 return Objects.equals(filters, csvFilter.filters);
110 }
111
112 @Override
113 public int hashCode() {
114 return Objects.hash(filters);
115 }
116
117 }