blob: 0c93a25f52e50f52e1e309e75f5f0cc5bd6d6b41 (
plain)
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
|
package bjc.utils.parserutils.splitterv2;
import bjc.utils.funcdata.FunctionalList;
import bjc.utils.funcdata.IList;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;
/**
* A token splitter that will not split certain tokens.
*
* @author EVE
*
*/
public class ExcludingTokenSplitter implements TokenSplitter {
private Set<String> literalExclusions;
private IList<Predicate<String>> predExclusions;
private TokenSplitter spliter;
/**
* Create a new excluding token splitter.
*
* @param splitter
* The splitter to apply to non-excluded strings.
*/
public ExcludingTokenSplitter(TokenSplitter splitter) {
spliter = splitter;
literalExclusions = new HashSet<>();
predExclusions = new FunctionalList<>();
}
/**
* Exclude a literal string from splitting.
*
* @param exclusion
* The string to exclude from splitting.
*/
public void addLiteralExclusion(String exclusion) {
literalExclusions.add(exclusion);
}
/**
* Exclude all of the strings matching a predicate from splitting.
*
* @param exclusion
* The predicate to use for exclusions.
*/
public void addPredicateExclusion(Predicate<String> exclusion) {
predExclusions.add(exclusion);
}
@Override
public IList<String> split(String input) {
if(literalExclusions.contains(input))
return new FunctionalList<>(input);
else if(predExclusions.anyMatch(pred -> pred.test(input))) return new FunctionalList<>(input);
return spliter.split(input);
}
}
|