1 package com.atlassian.plugin.predicate;
2
3 import java.util.Collection;
4 import java.util.regex.Matcher;
5 import java.util.regex.Pattern;
6
7 import com.atlassian.annotations.ExperimentalApi;
8 import com.atlassian.plugin.Plugin;
9 import com.atlassian.plugin.util.RegularExpressions;
10
11
12
13
14 @ExperimentalApi
15 public class PluginKeyPatternsPredicate implements PluginPredicate
16 {
17 public enum MatchType
18 {
19
20
21
22
23 MATCHES_ANY
24 {
25
26
27
28 public String buildRegularExpression(final Collection<String> parts)
29 {
30 return RegularExpressions.anyOf(parts);
31 }
32
33
34
35
36
37 public boolean processMatcher(final Matcher matcher)
38 {
39 return matcher.matches();
40 }
41 },
42
43
44
45
46
47 MATCHES_NONE
48 {
49
50
51
52 public String buildRegularExpression(final Collection<String> parts)
53 {
54 return RegularExpressions.anyOf(parts);
55 }
56
57
58
59
60
61 public boolean processMatcher(final Matcher matcher)
62 {
63 return !matcher.matches();
64 }
65 };
66
67 public abstract String buildRegularExpression(final Collection<String> parts);
68
69 public abstract boolean processMatcher(final Matcher matcher);
70 }
71
72 private final MatchType matchType;
73 private final Pattern pattern;
74
75 public PluginKeyPatternsPredicate(final MatchType matchType, final Collection<String> parts)
76 {
77 this.matchType = matchType;
78 this.pattern = Pattern.compile(matchType.buildRegularExpression(parts));
79 }
80
81 public boolean matches(final Plugin plugin)
82 {
83 return matchType.processMatcher(pattern.matcher(plugin.getKey()));
84 }
85 }