Utility class to handle pattern-matching for different types of patterns ( e.g. hive SHOW patterns, JDBC patterns). It maps those patterns onto the java regex pattern objects.
| 31 | * It maps those patterns onto the java regex pattern objects. |
| 32 | */ |
| 33 | public class PatternMatcher implements Predicate<String> { |
| 34 | // Patterns to match against. A string is considered to match if it matches |
| 35 | // any of the patterns. |
| 36 | private List<Pattern> patterns_; |
| 37 | |
| 38 | // Returns true if patterns_ is null or the candidate matches. |
| 39 | // Returns false if patterns_ is empty or the candidate mismatches. |
| 40 | public boolean matches(String candidate) { |
| 41 | if (patterns_ == null) return true; |
| 42 | if (patterns_.isEmpty()) return false; |
| 43 | for (Pattern pattern: patterns_) { |
| 44 | if (pattern.matcher(candidate).matches()) return true; |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | @Override // Implementation of Predicate interface. |
| 50 | public boolean apply(String input) { |
| 51 | return matches(input); |
| 52 | } |
| 53 | |
| 54 | // Immutable pattern matcher that matches all |
| 55 | private final static class MatchAllPatternMatcher extends PatternMatcher { |
| 56 | MatchAllPatternMatcher() {} |
| 57 | public boolean matches(String candidate) { return true; } |
| 58 | } |
| 59 | |
| 60 | // Immutable pattern matcher that matches none |
| 61 | private final static class MatchNonePatternMatcher extends PatternMatcher { |
| 62 | MatchNonePatternMatcher() {} |
| 63 | public boolean matches(String candidate) { return false; } |
| 64 | } |
| 65 | |
| 66 | public static final PatternMatcher MATCHER_MATCH_ALL = new MatchAllPatternMatcher(); |
| 67 | public static final PatternMatcher MATCHER_MATCH_NONE = new MatchNonePatternMatcher(); |
| 68 | |
| 69 | /** |
| 70 | * Creates a pattern matcher for hive patterns. |
| 71 | * The only metacharacters are '*' which matches any string of characters, and '|' |
| 72 | * which denotes choice. |
| 73 | * If hivePattern is null, all strings are considered to match. If it is the |
| 74 | * empty string, no strings match. |
| 75 | */ |
| 76 | public static PatternMatcher createHivePatternMatcher(String hivePattern) { |
| 77 | if (hivePattern == null) return MATCHER_MATCH_ALL; |
| 78 | if (hivePattern.isEmpty()) return MATCHER_MATCH_NONE; |
| 79 | PatternMatcher result = new PatternMatcher(); |
| 80 | result.patterns_ = Lists.newArrayList(); |
| 81 | // Hive ignores pretty much all metacharacters, so we have to escape them. |
| 82 | final String metaCharacters = "+?.^()]\\/{}"; |
| 83 | final Pattern regex = Pattern.compile("([" + Pattern.quote(metaCharacters) + "])"); |
| 84 | |
| 85 | for (String pattern: Arrays.asList(hivePattern.split("\\|"))) { |
| 86 | Matcher matcher = regex.matcher(pattern); |
| 87 | pattern = matcher.replaceAll("\\\\$1").replace("*", ".*"); |
| 88 | result.patterns_.add(Pattern.compile(pattern)); |
| 89 | } |
| 90 | return result; |
nothing calls this directly
no outgoing calls
no test coverage detected