@GetMapping public String init(Model model) { List<UiMenuItem> menus = new TreeTraverser<UiMenuItem>() { @Override public Iterable<UiMenuItem> children(UiMenuItem root) { if (root.getType() == UiMenuItemType.MENU) { UiMenu menu = (UiMenu) root; return Iterables.filter(menu.getItems(), molgenisUiMenuItem -> molgenisUiMenuItem.getType() == UiMenuItemType.MENU); } else return Collections.emptyList(); } }.preOrderTraversal(molgenisUi.getMenu()).toList(); List<Plugin> plugins = Lists.newArrayList(menuManagerService.getPlugins()); plugins.sort(Comparator.comparing(Plugin::getId)); model.addAttribute("menus", menus); model.addAttribute("plugins", plugins); model.addAttribute("molgenis_ui", molgenisUi); return "view-menumanager"; }
@SuppressWarnings("unchecked") public void testWeCanGetTheSpecificationHierarchy() throws Exception { List<String> names = Arrays.asList("specs", "specs/dira", "specs/dira/spec1.html", "specs/dira/subdira", "specs/dira/subdira/spec2.html", "specs/dira/subdira/spec4.html", "specs/dirb", "specs/dirb/spec5.html", "spec3.html"); File hierarchy = new File(FileUtils.toFile(getClass().getResource(".")),"testWeCanGetTheSpecificationHierarchy"); hierarchy.mkdirs(); createSpecificationHierarchyFiles(hierarchy, names); FileSystemRepository fileSystemRepository = new FileSystemRepository(hierarchy); DocumentNode tree = fileSystemRepository.getSpecificationsHierarchy("TOTO","TATA"); TreeTraverser<List<?>> traverser = new TreeTraverser<List<?>>() { @Override public Iterable<List<?>> children(List<?> root) { return ((Hashtable<String,List<?>>)root.get(3)).values(); } }; Iterator<DocumentNode> listsIter = DocumentNode.traverser.preOrderTraversal(tree).iterator(); assertEquals("testWeCanGetTheSpecificationHierarchy", listsIter.next().getTitle()); assertEquals("/specs", listsIter.next().getTitle()); assertEquals("/specs/dirb", listsIter.next().getTitle()); assertEquals("/specs/dirb/spec5.html", listsIter.next().getTitle()); assertEquals("/specs/dira", listsIter.next().getTitle()); assertEquals("/specs/dira/subdira", listsIter.next().getTitle()); assertEquals("/specs/dira/subdira/spec2.html", listsIter.next().getTitle()); assertEquals("/specs/dira/subdira/spec4.html", listsIter.next().getTitle()); assertEquals("/specs/dira/spec1.html", listsIter.next().getTitle()); assertEquals("/spec3.html", listsIter.next().getTitle()); }
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(); }
GuavaTraversal(final TreeTraverser<E> traverser) { this.traverser = traverser; }
public Iterable<FactoryBase<?,V>> breadthFirstTraversalFromRoot(){ factory.prepareIterationRunFromRoot(); final TreeTraverser<FactoryBase<?,V>> factoryTraverser = new FactoryTreeTraverser<>(); return factoryTraverser.breadthFirstTraversal(factory); }
public Iterable<FactoryBase<?,V>> postOrderTraversalFromRoot(){ factory.prepareIterationRunFromRoot(); final TreeTraverser<FactoryBase<?,V>> factoryTraverser = new FactoryTreeTraverser<>(); return factoryTraverser.postOrderTraversal(factory); }
public TreeTraverser<TreeNode<K,V>> getTraverser() { return new Traverser(); }
/** * Return a stream of nodes in a requested order * * @param root the root of the tree * @param traverser the {@link TreeTraverser} instance * @param traversal the requested traversal order * @param <E> type of the nodes in the tree * @return a stream of the nodes in the requested traversal order */ public static <E> Stream<E> traverse(final E root, final TreeTraverser<E> traverser, final Traversal traversal) { final GuavaTraversal<E> gt = new GuavaTraversal<>(traverser); final Iterable<E> iterable = gt.forTraversal(traversal).apply(root); return StreamSupport.stream(iterable.spliterator(), false); }
/** * Returns a {@link TreeTraverser} for traversing a directory tree. The returned traverser * attempts to avoid following symbolic links to directories. However, the traverser cannot * guarantee that it will not follow symbolic links to directories as it is possible for a * directory to be replaced with a symbolic link between checking if the file is a directory and * actually reading the contents of that directory. * * <p>Note that if the {@link Path} passed to one of the traversal methods does not exist, no * exception will be thrown and the returned {@link Iterable} will contain a single element: that * path. * * <p>{@link DirectoryIteratorException} may be thrown when iterating {@link Iterable} instances * created by this traverser if an {@link IOException} is thrown by a call to * {@link #listFiles(Path)}. */ public static TreeTraverser<Path> directoryTreeTraverser() { return DirectoryTreeTraverser.INSTANCE; }
/** * 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; }
/** * 直接使用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} for traversing a directory tree. The returned traverser * attempts to avoid following symbolic links to directories. However, the traverser cannot * guarantee that it will not follow symbolic links to directories as it is possible for a * directory to be replaced with a symbolic link between checking if the file is a directory and * actually reading the contents of that directory. * * <p>Note that if the {@link Path} passed to one of the traversal methods does not exist, no * exception will be thrown and the returned {@link Iterable} will contain a single element: that * path. * * <p>{@link DirectoryIteratorException} may be thrown when iterating {@link Iterable} instances * created by this traverser if an {@link IOException} is thrown by a call to {@link * #listFiles(Path)}. * * @deprecated The returned {@link TreeTraverser} type is deprecated. Use the replacement method * {@link #fileTraverser()} instead with the same semantics as this method. This method is * scheduled to be removed in April 2018. */ @Deprecated public static TreeTraverser<Path> directoryTreeTraverser() { return DirectoryTreeTraverser.INSTANCE; }
/** * 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 * @deprecated The returned {@link TreeTraverser} type is deprecated. Use the replacement method * {@link #fileTraverser()} instead with the same semantics as this method. This method is * scheduled to be removed in April 2018. */ @Deprecated public static TreeTraverser<File> fileTreeTraverser() { return FILE_TREE_TRAVERSER; }
protected abstract TreeTraverser<T> getTreeTraverser();