public static <T> Capture<T> newCapture() { return newCapture(""); }
public static <T> Capture<T> newCapture(String description) { return new Capture<>(description + "@" + sequenceCounter.incrementAndGet()); }
@Override public void visitCapture(CapturePattern<?> pattern) { visitPrevious(pattern); appendLine("capturedAs(%s)", pattern.capture().description()); }
@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")); }
@Override public void visitCapture(CapturePattern<?> pattern) { visitPrevious(pattern); appendLine("capturedAs(%s)", pattern.capture().description()); }
public static <T> Capture<T> newCapture(String description) { return new Capture<>(description + "@" + sequenceCounter.incrementAndGet()); }
protected ManipulateAdjacentWindowsOverProjects(int numProjects) { PropertyPattern<PlanNode, ?> childPattern = source().matching(window().capturedAs(childCapture)); ImmutableList.Builder<Capture<ProjectNode>> projectCapturesBuilder = ImmutableList.builder(); for (int i = 0; i < numProjects; ++i) { Capture<ProjectNode> projectCapture = newCapture(); projectCapturesBuilder.add(projectCapture); childPattern = source().matching(project().capturedAs(projectCapture).with(childPattern)); } this.projectCaptures = projectCapturesBuilder.build(); this.pattern = window().with(childPattern); }
@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")); }
@Test public void capturingMatchesInATypesafeManner() { Capture<FilterNode> filter = newCapture(); Capture<ScanNode> scan = newCapture(); Capture<String> name = newCapture(); Pattern<ProjectNode> pattern = project() .with(source().matching(filter().capturedAs(filter) .with(source().matching(scan().capturedAs(scan) .with(tableName().capturedAs(name)))))); ProjectNode tree = new ProjectNode(new FilterNode(new ScanNode("orders"), null)); Match<ProjectNode> match = assertMatch(pattern, tree); //notice the concrete type despite no casts: FilterNode capturedFilter = match.capture(filter); assertEquals(tree.getSource(), capturedFilter); assertEquals(((FilterNode) tree.getSource()).getSource(), match.capture(scan)); assertEquals("orders", match.capture(name)); }
public static <T> Capture<T> newCapture() { return newCapture(""); }
@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")); }
@Test public void capturingMatchesInATypesafeManner() { Capture<FilterNode> filter = newCapture(); Capture<ScanNode> scan = newCapture(); Capture<String> name = newCapture(); Pattern<ProjectNode> pattern = project() .with(source().matching(filter().capturedAs(filter) .with(source().matching(scan().capturedAs(scan) .with(tableName().capturedAs(name)))))); ProjectNode tree = new ProjectNode(new FilterNode(new ScanNode("orders"), null)); Match<ProjectNode> match = assertMatch(pattern, tree); //notice the concrete type despite no casts: FilterNode capturedFilter = match.capture(filter); assertEquals(tree.getSource(), capturedFilter); assertEquals(((FilterNode) tree.getSource()).getSource(), match.capture(scan)); assertEquals("orders", match.capture(name)); }