TreeWalker { public Stream<Component> ancestorStream() { return streamFromIterator(new AncestorIterator(myRoot)); } TreeWalker(@NotNull Component root); static boolean isAncestor(@NotNull Container ancestor, @NotNull Component child); Stream<Component> ancestorStream(); Iterable<Component> ancestors(); Stream<Component> descendantStream(DescendantOrder order); Iterable<Component> descendants(DescendantOrder order); Stream<Component> descendantStream(); Iterable<Component> descendants();  }
@Test public void canStreamAncestors() throws Exception { JPanel panel1 = new JPanel(); JPanel panel11 = new JPanel(); JPanel panel12 = new JPanel(); JPanel panel123 = new JPanel(); JPanel panel13 = new JPanel(); panel1.add(panel11); panel1.add(panel12); panel12.add(panel123); panel1.add(panel13); TreeWalker treeWalker = new TreeWalker(panel123); assertThat(treeWalker.ancestorStream().anyMatch(c -> c == panel12)).isTrue(); assertThat(treeWalker.ancestorStream().anyMatch(c -> c == panel1)).isTrue(); assertThat(treeWalker.ancestorStream().anyMatch(c -> c == panel11)).isFalse(); assertThat(treeWalker.ancestorStream().anyMatch(c -> c == panel13)).isFalse(); }
@Test public void findAncestorsIncludesSelf() throws Exception { JPanel panel1 = new JPanel(); JPanel panel11 = new JPanel(); JPanel panel12 = new JPanel(); panel1.add(panel11); panel1.add(panel12); TreeWalker treeWalker = new TreeWalker(panel12); assertThat(treeWalker.ancestorStream().anyMatch(c -> c == panel12)).isTrue(); }
@Test public void ancestorsAreReturnedInOrder() throws Exception { JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); panel1.add(panel2); panel2.add(panel3); TreeWalker treeWalker = new TreeWalker(panel3); assertThat(treeWalker.ancestorStream().collect(Collectors.toList())).containsExactly(panel3, panel2, panel1).inOrder(); }