TaskTree { public List<Long> getRecursiveParentsUpstreamChildrenIdListFromFar(long id) { ImmutableList.Builder<Long> builder = ImmutableList.builder(); ImmutableList.copyOf(Iterables.concat( getRecursiveParentIdListFromRoot(id), ImmutableList.of(id))) .stream() .forEach(parentId -> { walkUpstreamSiblings(parentId, builder, (sameBuilder1, sib) -> { sameBuilder1.add(sib.getId()); walkChildrenRecursively(sib.getId(), sameBuilder1, (sameBuilder2, child) -> sameBuilder2.add(child.getId())); return builder; }); if (parentId != id) { builder.add(parentId); } }); return builder.build(); } TaskTree(List<TaskRelation> rels); long getRootTaskId(); List<Long> getRecursiveParentIdListFromRoot(long id); List<Long> getRecursiveParentsUpstreamChildrenIdListFromFar(long id); T walkChildrenRecursively(long id, T value, Walker<T> walker); List<Long> getRecursiveChildrenIdList(long id);  }
@Test public void testRecursiveParentsUpstreamChildrenIdList() throws Exception { assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(1), is(list())); assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(2), is(list(1))); assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(3), is(list(1))); assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(4), is(list(1, 2))); assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(5), is(list(1, 2))); assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(6), is(list(1, 2, 5, 13))); assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(7), is(list(1, 2, 5, 13, 6, 14))); assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(14), is(list(1, 2, 5, 13, 6))); assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(18), is(list(1, 3, 9, 16, 17, 20, 10))); assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(19), is(list(1, 3, 9, 16, 17, 20, 10, 18))); assertThat( tree.getRecursiveParentsUpstreamChildrenIdListFromFar(11), is(list(1, 3, 9, 16, 17, 20, 10, 18, 19))); }