1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2024 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 * <div>
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 * </div>
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 *
45 * @param pattern the CSV string.
46 * @throws NumberFormatException if a component substring does not
47 * contain a parsable integer.
48 */
49 /* package */ CsvFilterElement(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 IntMatchFilterElement(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 IntRangeFilterElement(lowerBound, upperBound));
64 }
65 }
66 }
67
68 /**
69 * Adds a IntFilterElement to the set.
70 *
71 * @param filter the IntFilterElement to add.
72 */
73 private void addFilter(IntFilterElement filter) {
74 filters.add(filter);
75 }
76
77 /**
78 * Returns the IntFilters of the filter set.
79 *
80 * @return the IntFilters of the filter set.
81 */
82 protected Set<IntFilterElement> getFilters() {
83 return Collections.unmodifiableSet(filters);
84 }
85
86 /**
87 * Determines whether an Integer matches a CSV integer value.
88 *
89 * @param intValue the Integer to check.
90 * @return true if intValue is an Integer that matches a CSV value.
91 */
92 @Override
93 public boolean accept(int intValue) {
94 boolean result = false;
95 for (IntFilterElement filter : getFilters()) {
96 if (filter.accept(intValue)) {
97 result = true;
98 break;
99 }
100 }
101 return result;
102 }
103
104 @Override
105 public boolean equals(Object object) {
106 if (this == object) {
107 return true;
108 }
109 if (object == null || getClass() != object.getClass()) {
110 return false;
111 }
112 final CsvFilterElement csvFilter = (CsvFilterElement) object;
113 return Objects.equals(filters, csvFilter.filters);
114 }
115
116 @Override
117 public int hashCode() {
118 return Objects.hash(filters);
119 }
120
121 }