@Override public Match<T> accept(Matcher matcher, Object object, Captures captures) { return matcher.matchCapture(this, object, captures); }
@Override public Match<T> accept(Matcher matcher, Object object, Captures captures) { return matcher.matchEquals(this, object, captures); }
@Override public Match<T> accept(Matcher matcher, Object object, Captures captures) { return matcher.matchFilter(this, object, captures); }
public boolean matches(Object object) { return DEFAULT_MATCHER.match(this, object).isPresent(); }
@Override public Match<T> accept(Matcher matcher, Object object, Captures captures) { return matcher.matchTypeOf(this, object, captures); }
@Override public Match<T> accept(Matcher matcher, Object object, Captures captures) { return matcher.matchWith(this, object, captures); }
<T> Match<T> match(Pattern<T> pattern, Object object, Captures captures);
private <T, R> Match<R> assertMatch(Pattern<R> pattern, T matchedAgainst, R expectedMatch) { Match<R> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, matchedAgainst); assertEquals(expectedMatch, match.value()); return match; }
private <T> void assertNoMatch(Pattern<T> pattern, Object expectedNoMatch) { Match<T> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, expectedNoMatch); assertEquals(Match.empty(), match); } }
@Test public void unknownCaptureIsAnError() { Pattern<?> pattern = any(); Capture<?> unknownCapture = newCapture(); Match<?> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(unknownCapture)); assertTrue(throwable.getMessage().contains("unknown Capture")); }
@Test public void noMatchMeansNoCaptures() { Capture<Void> impossible = newCapture(); Pattern<Void> pattern = typeOf(Void.class).capturedAs(impossible); Match<Void> match = DefaultMatcher.DEFAULT_MATCHER.match(pattern, 42); assertTrue(match.isEmpty()); Throwable throwable = expectThrows(NoSuchElementException.class, () -> match.capture(impossible)); assertTrue(throwable.getMessage().contains("Captures are undefined for an empty Match")); }
private <T> Rule.Result transform(PlanNode node, Rule<T> rule, Matcher matcher, Context context) { Rule.Result result; Match<T> match = matcher.match(rule.getPattern(), node); if (match.isEmpty()) { return Rule.Result.empty(); } long duration; try { long start = System.nanoTime(); result = rule.apply(match.value(), match.captures(), ruleContext(context)); duration = System.nanoTime() - start; } catch (RuntimeException e) { stats.recordFailure(rule); throw e; } stats.record(rule, duration, !result.isEmpty()); return result; }