下面列出了怎么用com.google.common.collect.TreeTraverser的API类实例代码及写法,或者点击链接到github查看源代码。
private Stream<DocumentSymbol> asStream(Collection<? extends DocumentSymbol> symbols) {
//@formatter:off
return symbols.stream()
.map(s -> TreeTraverser.<DocumentSymbol>using(ds -> ds.getChildren() == null
? Collections.<DocumentSymbol>emptyList()
: ds.getChildren()).breadthFirstTraversal(s).toList())
.flatMap(List::stream);
//@formatter:on
}
private static <T> TreeTraverser<T> treeTraverserOf(Function<T, Iterable<T>> childrenFunction) {
return new TreeTraverser<T>() {
@Override
public Iterable<T> children(T root) {
return childrenFunction.apply(root);
}
};
}
public static <CompositeT extends Composite<CompositeT>> Iterable<CompositeT> preOrderTraversal(CompositeT root) {
return TreeTraverser.using(new com.google.common.base.Function<CompositeT, Iterable<CompositeT>>() {
@Override
public Iterable<CompositeT> apply(CompositeT input) {
return input == null ? Collections.<CompositeT>emptyList() : input.children();
}
}).preOrderTraversal(root);
}
public static Stream<Node> preOrder(Node node)
{
return TreeTraverser.using((Node n) -> unmodifiableIterable(n.getChildren()))
.preOrderTraversal(requireNonNull(node, "node is null"))
.stream();
}
/**
* 直接使用Guava的TreeTraverser,获得更大的灵活度, 比如加入各类filter,前序/后序的选择,一边遍历一边操作
*
* <pre>
* FileUtil.fileTreeTraverser().preOrderTraversal(root).iterator();
* </pre>
*/
public static TreeTraverser<File> fileTreeTraverser() {
return Files.fileTreeTraverser();
}
/**
* 直接使用Guava的TreeTraverser,获得更大的灵活度, 比如加入各类filter,前序/后序的选择,一边遍历一边操作
*
* <pre>
* FileUtil.fileTreeTraverser().preOrderTraversal(root).iterator();
* </pre>
*/
public static TreeTraverser<File> fileTreeTraverser() {
return Files.fileTreeTraverser();
}
/**
* 直接使用Guava的TreeTraverser,获得更大的灵活度, 比如加入各类filter,前序/后序的选择,一边遍历一边操作
*
* <pre>
* FileUtil.fileTreeTraverser().preOrderTraversal(root).iterator();
* </pre>
*/
public static TreeTraverser<File> fileTreeTraverser() {
return Files.fileTreeTraverser();
}
/**
* Returns a {@link TreeTraverser} instance for {@link File} trees.
*
* <p><b>Warning:</b> {@code File} provides no support for symbolic links, and as such there is no
* way to ensure that a symbolic link to a directory is not followed when traversing the tree. In
* this case, iterables created by this traverser could contain files that are outside of the
* given directory or even be infinite if there is a symbolic link loop.
*
* @since 15.0
*/
public static TreeTraverser<File> fileTreeTraverser() {
return FILE_TREE_TRAVERSER;
}
/**
* Returns a {@link TreeTraverser} instance for {@link File} trees.
*
* <p><b>Warning:</b> {@code File} provides no support for symbolic links, and as such there is no
* way to ensure that a symbolic link to a directory is not followed when traversing the tree. In
* this case, iterables created by this traverser could contain files that are outside of the
* given directory or even be infinite if there is a symbolic link loop.
*
* @since 15.0
*/
public static TreeTraverser<File> fileTreeTraverser() {
return FILE_TREE_TRAVERSER;
}
/**
* Returns a {@link TreeTraverser} instance for {@link File} trees.
*
* <p><b>Warning:</b> {@code File} provides no support for symbolic links, and as such there is no
* way to ensure that a symbolic link to a directory is not followed when traversing the tree. In
* this case, iterables created by this traverser could contain files that are outside of the
* given directory or even be infinite if there is a symbolic link loop.
*
* @since 15.0
*/
public static TreeTraverser<File> fileTreeTraverser() {
return FILE_TREE_TRAVERSER;
}
/**
* Returns a {@link TreeTraverser} instance for {@link File} trees.
*
* <p><b>Warning:</b> {@code File} provides no support for symbolic links, and as such there is no
* way to ensure that a symbolic link to a directory is not followed when traversing the tree. In
* this case, iterables created by this traverser could contain files that are outside of the
* given directory or even be infinite if there is a symbolic link loop.
*
* @since 15.0
*/
public static TreeTraverser<File> fileTreeTraverser() {
return FILE_TREE_TRAVERSER;
}
/**
* Returns a {@link TreeTraverser} instance for {@link File} trees.
*
* <p><b>Warning:</b> {@code File} provides no support for symbolic links, and as such there is no
* way to ensure that a symbolic link to a directory is not followed when traversing the tree. In
* this case, iterables created by this traverser could contain files that are outside of the
* given directory or even be infinite if there is a symbolic link loop.
*
* @since 15.0
*/
public static TreeTraverser<File> fileTreeTraverser() {
return FILE_TREE_TRAVERSER;
}