View Javadoc
1   package com.atlassian.plugin.predicate;
2   
3   import com.atlassian.annotations.ExperimentalApi;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.util.RegularExpressions;
6   
7   import java.util.Collection;
8   import java.util.function.Predicate;
9   import java.util.regex.Matcher;
10  import java.util.regex.Pattern;
11  
12  /**
13   * A plugin predicate which matches regular expressions against plugin keys.
14   */
15  @ExperimentalApi
16  public class PluginKeyPatternsPredicate implements Predicate<Plugin> {
17      public enum MatchType {
18          /**
19           * Specifies a PluginPredicate which matches if any one of a given list of
20           * included regular expressions is matched.
21           */
22          MATCHES_ANY {
23              /**
24               * Obtain a regular expression which matches any of the provided parts.
25               */
26              public String buildRegularExpression(final Collection<String> parts) {
27                  return RegularExpressions.anyOf(parts);
28              }
29  
30              /**
31               * This MatchType matches if the Matcher does match, as we want to match if any of the
32               * inclusions provided to {@link #buildRegularExpression} matches.
33               */
34              public boolean processMatcher(final Matcher matcher) {
35                  return matcher.matches();
36              }
37          },
38  
39          /**
40           * Specifies a PluginPredicate which matches if none of a given list of
41           * excluded regular expressions is matched.
42           */
43          MATCHES_NONE {
44              /**
45               * Obtain a regular expression which matches any of the provided parts.
46               */
47              public String buildRegularExpression(final Collection<String> parts) {
48                  return RegularExpressions.anyOf(parts);
49              }
50  
51              /**
52               * This MatchType matches if the Matcher does not match, as we want to match only when
53               * none of the exclusions provided to {@link #buildRegularExpression} matches.
54               */
55              public boolean processMatcher(final Matcher matcher) {
56                  return !matcher.matches();
57              }
58          };
59  
60          public abstract String buildRegularExpression(final Collection<String> parts);
61  
62          public abstract boolean processMatcher(final Matcher matcher);
63      }
64  
65      private final MatchType matchType;
66      private final Pattern pattern;
67  
68      public PluginKeyPatternsPredicate(final MatchType matchType, final Collection<String> parts) {
69          this.matchType = matchType;
70          this.pattern = Pattern.compile(matchType.buildRegularExpression(parts));
71      }
72  
73      @Override
74      public boolean test(final Plugin plugin) {
75          return matchType.processMatcher(pattern.matcher(plugin.getKey()));
76      }
77  }