1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.filters;
21
22 import java.util.Objects;
23 import java.util.regex.Pattern;
24
25 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
26 import com.puppycrawl.tools.checkstyle.api.Filter;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class SuppressFilterElement
41 implements Filter {
42
43
44 private final Pattern fileRegexp;
45
46
47 private final Pattern checkRegexp;
48
49
50 private final Pattern messageRegexp;
51
52
53 private final String moduleId;
54
55
56 private final CsvFilterElement lineFilter;
57
58
59 private final String linesCsv;
60
61
62 private final CsvFilterElement columnFilter;
63
64
65 private final String columnsCsv;
66
67
68
69
70
71
72
73
74
75
76
77
78 public SuppressFilterElement(String files, String checks,
79 String message, String modId, String lines, String columns) {
80 if (files == null) {
81 fileRegexp = null;
82 }
83 else {
84 fileRegexp = Pattern.compile(files);
85 }
86 if (checks == null) {
87 checkRegexp = null;
88 }
89 else {
90 checkRegexp = Pattern.compile(checks);
91 }
92 if (message == null) {
93 messageRegexp = null;
94 }
95 else {
96 messageRegexp = Pattern.compile(message);
97 }
98 moduleId = modId;
99 linesCsv = lines;
100 if (lines == null) {
101 lineFilter = null;
102 }
103 else {
104 lineFilter = new CsvFilterElement(lines);
105 }
106 columnsCsv = columns;
107 if (columns == null) {
108 columnFilter = null;
109 }
110 else {
111 columnFilter = new CsvFilterElement(columns);
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124
125 public SuppressFilterElement(Pattern files, Pattern checks, Pattern message, String moduleId,
126 String lines, String columns) {
127 fileRegexp = files;
128 checkRegexp = checks;
129 messageRegexp = message;
130 this.moduleId = moduleId;
131 if (lines == null) {
132 linesCsv = null;
133 lineFilter = null;
134 }
135 else {
136 linesCsv = lines;
137 lineFilter = new CsvFilterElement(lines);
138 }
139 if (columns == null) {
140 columnsCsv = null;
141 columnFilter = null;
142 }
143 else {
144 columnsCsv = columns;
145 columnFilter = new CsvFilterElement(columns);
146 }
147 }
148
149 @Override
150 public boolean accept(AuditEvent event) {
151 return !isFileNameAndModuleNameMatching(event)
152 || !isMessageNameMatching(event)
153 || !isLineAndColumnMatching(event);
154 }
155
156
157
158
159
160
161
162 private boolean isFileNameAndModuleNameMatching(AuditEvent event) {
163 return event.getFileName() != null
164 && (fileRegexp == null || fileRegexp.matcher(event.getFileName()).find())
165 && event.getViolation() != null
166 && (moduleId == null || moduleId.equals(event.getModuleId()))
167 && (checkRegexp == null || checkRegexp.matcher(event.getSourceName()).find());
168 }
169
170
171
172
173
174
175
176 private boolean isMessageNameMatching(AuditEvent event) {
177 return messageRegexp == null || messageRegexp.matcher(event.getMessage()).find();
178 }
179
180
181
182
183
184
185
186 private boolean isLineAndColumnMatching(AuditEvent event) {
187 return lineFilter == null && columnFilter == null
188 || lineFilter != null && lineFilter.accept(event.getLine())
189 || columnFilter != null && columnFilter.accept(event.getColumn());
190 }
191
192 @Override
193 public int hashCode() {
194 return Objects.hash(getPatternSafely(fileRegexp), getPatternSafely(checkRegexp),
195 getPatternSafely(messageRegexp), moduleId, linesCsv, columnsCsv);
196 }
197
198 @Override
199 public boolean equals(Object other) {
200 if (this == other) {
201 return true;
202 }
203 if (other == null || getClass() != other.getClass()) {
204 return false;
205 }
206 final SuppressFilterElement suppressElement = (SuppressFilterElement) other;
207 return Objects.equals(getPatternSafely(fileRegexp),
208 getPatternSafely(suppressElement.fileRegexp))
209 && Objects.equals(getPatternSafely(checkRegexp),
210 getPatternSafely(suppressElement.checkRegexp))
211 && Objects.equals(getPatternSafely(messageRegexp),
212 getPatternSafely(suppressElement.messageRegexp))
213 && Objects.equals(moduleId, suppressElement.moduleId)
214 && Objects.equals(linesCsv, suppressElement.linesCsv)
215 && Objects.equals(columnsCsv, suppressElement.columnsCsv);
216 }
217
218
219
220
221
222
223
224
225 private static String getPatternSafely(Pattern pattern) {
226 String result = null;
227 if (pattern != null) {
228 result = pattern.pattern();
229 }
230 return result;
231 }
232 }