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 String filePattern;
48
49
50 private final Pattern checkRegexp;
51
52
53 private final String checkPattern;
54
55
56 private final Pattern messageRegexp;
57
58
59 private final String messagePattern;
60
61
62 private final String moduleId;
63
64
65 private final CsvFilterElement lineFilter;
66
67
68 private final String linesCsv;
69
70
71 private final CsvFilterElement columnFilter;
72
73
74 private final String columnsCsv;
75
76
77
78
79
80
81
82
83
84
85
86
87 public SuppressFilterElement(String files, String checks,
88 String message, String modId, String lines, String columns) {
89 filePattern = files;
90 if (files == null) {
91 fileRegexp = null;
92 }
93 else {
94 fileRegexp = Pattern.compile(files);
95 }
96 checkPattern = checks;
97 if (checks == null) {
98 checkRegexp = null;
99 }
100 else {
101 checkRegexp = Pattern.compile(checks);
102 }
103 messagePattern = message;
104 if (message == null) {
105 messageRegexp = null;
106 }
107 else {
108 messageRegexp = Pattern.compile(message);
109 }
110 moduleId = modId;
111 linesCsv = lines;
112 if (lines == null) {
113 lineFilter = null;
114 }
115 else {
116 lineFilter = new CsvFilterElement(lines);
117 }
118 columnsCsv = columns;
119 if (columns == null) {
120 columnFilter = null;
121 }
122 else {
123 columnFilter = new CsvFilterElement(columns);
124 }
125 }
126
127 @Override
128 public boolean accept(AuditEvent event) {
129 return !isFileNameAndModuleNameMatching(event)
130 || !isMessageNameMatching(event)
131 || !isLineAndColumnMatching(event);
132 }
133
134
135
136
137
138
139 private boolean isFileNameAndModuleNameMatching(AuditEvent event) {
140 return event.getFileName() != null
141 && (fileRegexp == null || fileRegexp.matcher(event.getFileName()).find())
142 && event.getLocalizedMessage() != null
143 && (moduleId == null || moduleId.equals(event.getModuleId()))
144 && (checkRegexp == null || checkRegexp.matcher(event.getSourceName()).find());
145 }
146
147
148
149
150
151
152 private boolean isMessageNameMatching(AuditEvent event) {
153 return messageRegexp == null || messageRegexp.matcher(event.getMessage()).find();
154 }
155
156
157
158
159
160
161 private boolean isLineAndColumnMatching(AuditEvent event) {
162 return lineFilter == null && columnFilter == null
163 || lineFilter != null && lineFilter.accept(event.getLine())
164 || columnFilter != null && columnFilter.accept(event.getColumn());
165 }
166
167 @Override
168 public int hashCode() {
169 return Objects.hash(filePattern, checkPattern, messagePattern, moduleId, linesCsv,
170 columnsCsv);
171 }
172
173 @Override
174 public boolean equals(Object other) {
175 if (this == other) {
176 return true;
177 }
178 if (other == null || getClass() != other.getClass()) {
179 return false;
180 }
181 final SuppressFilterElement suppressElement = (SuppressFilterElement) other;
182 return Objects.equals(filePattern, suppressElement.filePattern)
183 && Objects.equals(checkPattern, suppressElement.checkPattern)
184 && Objects.equals(messagePattern, suppressElement.messagePattern)
185 && Objects.equals(moduleId, suppressElement.moduleId)
186 && Objects.equals(linesCsv, suppressElement.linesCsv)
187 && Objects.equals(columnsCsv, suppressElement.columnsCsv);
188 }
189
190 }