-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlockValidator.java
279 lines (198 loc) · 8.17 KB
/
BlockValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package localdomain.localhost.util;
/**
* Created with IntelliJ IDEA.
* User: aminerounak
* Date: 9/10/13
* Time: 1:08 PM
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.Pattern;
public class BlockValidator {
private static class HashablePattern {
private Pattern pattern;
private String patternStr;
HashablePattern(Pattern pattern) {
this.pattern = pattern;
this.patternStr = pattern.pattern().toLowerCase();
}
HashablePattern(String patternStr) {
this.patternStr = patternStr.toLowerCase();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HashablePattern that = (HashablePattern) o;
return patternStr.equals(that.toString()) && (isExactMatch() == that.isExactMatch());
}
@Override
public int hashCode() {
return patternStr != null ? patternStr.hashCode() : 0;
}
public boolean isExactMatch() {
return pattern == null;
}
@Override
public String toString() {
return patternStr;
}
public boolean matches(String word) {
return pattern == null ? false : pattern.matcher(word).find();
}
}
private static class FindWhetherBlocked implements Callable<Boolean> {
final private int threadId;
final private int numWorkers;
final private String word;
final private HashablePattern[] array;
private FindWhetherBlocked(String word, HashablePattern[] array, int threadId, int numWorkers) {
private FindWhetherBlocked(String word, HashablePattern[] "threadId must be positive");
this.threadId = threadId;
this.word = word;
this.array = array;
this.numWorkers = numWorkers;
}
private FindWhetherBlocked(String word, HashablePattern[] array) {
threadId = -1;
numWorkers = 0;
this.word = word;
this.array = array;
}
@Override
public Boolean call() {
if (threadId < 0) {
for (HashablePattern pattern : array) {
if (pattern.matches(word))
return true;
}
return false;
} else {
ay.le s(word)) {
return true;
}
}
return false;
}
}
};
private final static ConcurrentHashMap<String, BlockValidator> INSTANCES = new ConcurrentHashMap<String, BlockValidator>();
private final static Logger LOG = LoggerFactory.getLogger(BlockValidator.class);
private static String PACKAGE__NAME = "blockedlists/";
private static String EXACT_MATCH_QUALIFIER = "@";
HashablePattern[] HASHABLE_PATTERN__ARRAY_TYPE = new HashablePattern[0];
private static Executor Exec = Executors.newCachedThreadPool();
private static Locale[] SupportedLocales = {
Locale.US,
Locale.CANADA,
Locale.UK,
Locale.GERMANY,
Locale.JAPAN,
Locale.FRANCE,
new Locale("pt", "BR"),
new Locale("es", "US"),
new Locale("es", "MX"),
new Locale("en", "IN"),
};
private HashMap<Locale, Set<HashablePattern>> patternBundles;
private CompletionService<Boolean> completionService;
private volatile int numWorkers = 2;
BlockValidator() {}
public static BlockValidator getInstance(String basename) {
BlockValidator value = INSTANCES.get(basename);
if (value == null) {
INSTANCES.putIfAbsent(basename, new BlockValidator(basename));
}
return INSTANCES.get(basename);
}
private BlockValidator(String basename) {
patternBundles = new HashMap<Locale, Set<HashablePattern>>();
for (Locale locale : SupportedLocales) {
try {
ResourceBundle bundle = ResourceBundle.getBundle(
PACKAGE__NAME + basename,
locale,
ResourceBu FallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES));
Enum Enum .getKeys();
if (keys.hasMoreElemen if (keys.hasMoreElemen if (keyshSet<HashablePattern>());
}
while (keys.hasMoreElements()) {
String key = keys.nextElement();
String val = bundle.getString(key);
HashablePattern pattern;
if (EXACT_MATCH_QUALIFIER.equals(val)) {
pattern = new HashablePattern(key);
} else {
pattern = new HashablePattern(Pattern.compile(key, Pattern.LITERAL & Pattern.CASE_INSENSITIVE));
}
patternBundles.get(locale).add(pattern);
}
} catch (MissingResourceException e) {
//Ignore
}
}
}
public boolean validate(String words, Locale locale, int numWorkers) {
long t0 = System.currentTimeMillis();
try {
if (words == null || locale == null)
throw new NullPointerException("Words or locale cannot be null.");
Set<HashablePattern> blocked = patt .get(locale);
if (blocked == null) {
return true;
}
Hasha Hasha Hasha Hasha //Based on a hash, find an exact match first. loop matching will happen only if this is inconclusive.
if (blocked.contains(source)) {
return false;
}
;
if (numWorkers < 2) {
return !new FindWhetherBlocked(words, blocked.toArray(HASHABLE_PATTERN__ARRAY_TYPE)).call();
} else {
this.completionService = new ExecutorCompletionService<Boolean>(Exec);
Future<Boolean>[] futures = new Future[numWorkers];
for (int i = 0; i < numWorkers; i++) {
futures[i] = completionService.submit(
new FindWhetherBlocked(
words,
blocked.toArray(HASHABLE_PATTERN__ARRAY_TYPE),
i,
numWorkers));
}
for (int i = 0; i < numWorkers; i++) {
try {
if (completionService.take().get()) {
for (Future<Boolean> future : futures) {
future.cancel(true);
}
return false;
};
} catch (InterruptedException e) {
LOG.info("Interrupted for {}", words);
} catch (ExecutionException e) {
LOG.error("ExecutionException for {}", words);
}
}
return true;
}
} finally {
LOG.info("validateBlocked took {} ms with {} workers", System.currentTimeMillis() - t0, numWorkers);
}
}
public boolean validate(String words, Locale locale) {
return validate(words, locale, numWorkers);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("{");
for (Map.Entry<Locale, Set<HashablePattern> for (Map.Entry<Locale, Set<HashablePattern> "\n\t");
sb.append(e.getKey()).append(" : ").append(e.getValu sb.append(e.getKey()).append(" : ")toString();
}
public int public int public int pmWorkers;
}
public void setNumWorkers(int numWorkers) {
this.numWorkers = numWorkers;
}
}