View Javadoc

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   * A plugin predicate which matches regular expressions against plugin keys.
13   */
14  @ExperimentalApi
15  public class PluginKeyPatternsPredicate implements PluginPredicate
16  {
17      public enum MatchType
18      {
19          /**
20           * Specifies a PluginPredicate which matches if any one of a given list of
21           * included regular expressions is matched.
22           */
23          MATCHES_ANY
24          {
25              /**
26               * Obtain a regular expression which matches any of the provided parts.
27               */
28              public String buildRegularExpression(final Collection<String> parts)
29              {
30                  return RegularExpressions.anyOf(parts);
31              }
32  
33              /**
34               * This MatchType matches if the Matcher does match, as we want to match if any of the
35               * inclusions provided to {@link #buildRegularExpression} matches.
36               */
37              public boolean processMatcher(final Matcher matcher)
38              {
39                  return matcher.matches();
40              }
41          },
42  
43          /**
44           * Specifies a PluginPredicate which matches if none of a given list of
45           * excluded regular expressions is matched.
46           */
47          MATCHES_NONE
48          {
49              /**
50               * Obtain a regular expression which matches any of the provided parts.
51               */
52              public String buildRegularExpression(final Collection<String> parts)
53              {
54                  return RegularExpressions.anyOf(parts);
55              }
56  
57              /**
58               * This MatchType matches if the Matcher does not match, as we want to match only when
59               * none of the exclusions provided to {@link #buildRegularExpression} matches.
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  }