/** * The method to recursively fetch the children of given parent. Used with * {@link Stream#flatMap} to expand a stream of parent nodes into a * flattened hierarchy. * * @param parent * the parent node * @return the stream of all children under the parent, includes the parent */ private Stream<T> getChildrenStream(T parent) { return getChildrenStream(parent, true); }
/** * Getst hte full hierarchy tree starting from given node. The starting node * can be omitted. * * @param parent * the parent node to start from * @param includeParent * {@code true} to include the parent; {@code false} if not * @return the flattened hierarchy as a stream */ private Stream<T> getHierarchy(T parent, boolean includeParent) { return Stream.of(parent) .flatMap(node -> getChildrenStream(node, includeParent)); }