-
Notifications
You must be signed in to change notification settings - Fork 227
/
MatcherRegistry.java
76 lines (65 loc) · 2.37 KB
/
MatcherRegistry.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
package com.chenenyu.router;
import com.chenenyu.router.matcher.AbsExplicitMatcher;
import com.chenenyu.router.matcher.AbsImplicitMatcher;
import com.chenenyu.router.matcher.AbsMatcher;
import com.chenenyu.router.matcher.BrowserMatcher;
import com.chenenyu.router.matcher.DirectMatcher;
import com.chenenyu.router.matcher.ImplicitMatcher;
import com.chenenyu.router.matcher.SchemeMatcher;
import com.chenenyu.router.util.RLog;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Matcher registry.
* <br>
* Created by chenenyu on 2017/1/5.
*/
public final class MatcherRegistry {
private static final List<AbsMatcher> ALL = new ArrayList<>();
private static final List<AbsExplicitMatcher> explicitMatcher = new ArrayList<>();
private static final List<AbsImplicitMatcher> implicitMatcher = new ArrayList<>();
static {
ALL.add(new DirectMatcher(0x1000));
ALL.add(new SchemeMatcher(0x0100));
ALL.add(new ImplicitMatcher(0x0010));
ALL.add(new BrowserMatcher(0x0000));
Collections.sort(ALL);
classifyMatcher();
}
public static void register(AbsMatcher matcher) {
if (matcher instanceof AbsExplicitMatcher || matcher instanceof AbsImplicitMatcher) {
ALL.add(matcher);
Collections.sort(ALL);
classifyMatcher();
} else {
RLog.e(String.format("%s must be a subclass of AbsExplicitMatcher or AbsImplicitMatcher",
matcher.getClass().getSimpleName()));
}
}
public static List<AbsMatcher> getMatcher() {
return ALL;
}
public static List<AbsExplicitMatcher> getExplicitMatcher() {
return explicitMatcher;
}
public static List<AbsImplicitMatcher> getImplicitMatcher() {
return implicitMatcher;
}
public static void clear() {
ALL.clear();
explicitMatcher.clear();
implicitMatcher.clear();
}
private static void classifyMatcher() {
explicitMatcher.clear();
implicitMatcher.clear();
for (AbsMatcher absMatcher : ALL) {
if (absMatcher instanceof AbsExplicitMatcher) {
explicitMatcher.add((AbsExplicitMatcher) absMatcher);
} else if (absMatcher instanceof AbsImplicitMatcher) {
implicitMatcher.add((AbsImplicitMatcher) absMatcher);
}
}
}
}