|
8 | 8 | import org.hamcrest.TypeSafeMatcher; |
9 | 9 | import org.hamcrest.text.pattern.internal.naming.GroupNamespace; |
10 | 10 |
|
11 | | - |
12 | 11 | public class PatternMatcher extends TypeSafeMatcher<String> implements PatternComponent { |
13 | | - private GroupNamespace groups = new GroupNamespace(); |
14 | | - private PatternComponent root; |
15 | | - private Pattern pattern; |
16 | | - |
17 | | - public PatternMatcher(PatternComponent root) { |
18 | | - this.root = root; |
19 | | - this.pattern = compile(root, groups); |
20 | | - } |
21 | | - |
22 | | - @Factory |
23 | | - public static PatternMatcher matchesPattern(PatternComponent pattern) { |
24 | | - return new PatternMatcher(pattern); |
25 | | - } |
26 | | - |
27 | | - @Factory |
28 | | - public static PatternMatcher matchesPattern(PatternMatcher pattern) { |
29 | | - return pattern; |
30 | | - } |
31 | | - |
32 | | - public String toString() { |
33 | | - return pattern.toString(); |
34 | | - } |
35 | | - |
36 | | - public void describeTo(Description description) { |
37 | | - description.appendText("a string matching "); |
38 | | - description.appendValue(this.toString()); |
39 | | - } |
40 | | - |
41 | | - public boolean matchesSafely(String s) { |
42 | | - return pattern.matcher(s).matches(); |
43 | | - } |
44 | | - |
45 | | - public Parse parse(String input) throws PatternMatchException { |
46 | | - Matcher matcher = pattern.matcher(input); |
47 | | - if (matcher.matches()) { |
48 | | - return new Parse(groups, matcher); |
49 | | - } else { |
50 | | - throw new PatternMatchException("did not match input: " + input); |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - public void buildRegex(StringBuilder builder, GroupNamespace groups) { |
55 | | - root.buildRegex(builder, groups); |
56 | | - } |
57 | | - |
58 | | - private static Pattern compile(PatternComponent root, GroupNamespace groups) { |
59 | | - StringBuilder builder = new StringBuilder(); |
60 | | - root.buildRegex(builder, groups); |
61 | | - return Pattern.compile(builder.toString()); |
62 | | - } |
| 12 | + private GroupNamespace groups = new GroupNamespace(); |
| 13 | + private PatternComponent root; |
| 14 | + private Pattern pattern; |
| 15 | + |
| 16 | + public PatternMatcher(PatternComponent root) { |
| 17 | + this.root = root; |
| 18 | + this.pattern = compile(root, groups); |
| 19 | + } |
| 20 | + |
| 21 | + @Factory |
| 22 | + public static PatternMatcher matchesPattern(PatternComponent pattern) { |
| 23 | + return new PatternMatcher(pattern); |
| 24 | + } |
| 25 | + |
| 26 | + @Factory |
| 27 | + public static PatternMatcher matchesPattern(PatternMatcher pattern) { |
| 28 | + return pattern; |
| 29 | + } |
| 30 | + |
| 31 | + public String toString() { |
| 32 | + return pattern.toString(); |
| 33 | + } |
| 34 | + |
| 35 | + public void describeTo(Description description) { |
| 36 | + description.appendText("a string matching "); |
| 37 | + description.appendValue(this.toString()); |
| 38 | + } |
| 39 | + |
| 40 | + public boolean matchesSafely(String s) { |
| 41 | + return pattern.matcher(s).matches(); |
| 42 | + } |
| 43 | + |
| 44 | + public Parse parse(String input) throws PatternMatchException { |
| 45 | + Matcher matcher = pattern.matcher(input); |
| 46 | + if (matcher.matches()) { |
| 47 | + return new Parse(groups, matcher); |
| 48 | + } else { |
| 49 | + throw new PatternMatchException("did not match input: " + input); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public void buildRegex(StringBuilder builder, GroupNamespace groups) { |
| 54 | + root.buildRegex(builder, groups); |
| 55 | + } |
| 56 | + |
| 57 | + private static Pattern compile(PatternComponent root, GroupNamespace groups) { |
| 58 | + StringBuilder builder = new StringBuilder(); |
| 59 | + root.buildRegex(builder, groups); |
| 60 | + return Pattern.compile(builder.toString()); |
| 61 | + } |
63 | 62 | } |
0 commit comments