Example usage for com.google.common.collect TreeTraverser breadthFirstTraversal

List of usage examples for com.google.common.collect TreeTraverser breadthFirstTraversal

Introduction

In this page you can find the example usage for com.google.common.collect TreeTraverser breadthFirstTraversal.

Prototype

public final FluentIterable<T> breadthFirstTraversal(final T root) 

Source Link

Document

Returns an unmodifiable iterable over the nodes in a tree structure, using breadth-first traversal.

Usage

From source file:com.netflix.hystrix.contrib.javanica.utils.TypeHelper.java

/**
 * Unwinds parametrized type into plain list that contains all parameters for the given type including nested parameterized types,
 * for example calling the method for the following type
 * <code>/*from w  w w .  j a  va 2 s . co  m*/
 * GType<GType<GDoubleType<GType<GDoubleType<Parent, Parent>>, Parent>>>
 * </code>
 * will return list of 8 elements:
 * <code>
 * [GType, GType, GDoubleType, GType, GDoubleType, Parent, Parent, Parent]
 * </code>
 * if the given type is not parametrized then returns list with one element which is given type passed into method.
 *
 * @param type the parameterized type
 * @return list of {@link Type}
 */
@ParametersAreNonnullByDefault
public static List<Type> getAllParameterizedTypes(Type type) {
    Validate.notNull(type, "type cannot be null");
    List<Type> types = new ArrayList<Type>();
    TreeTraverser<Type> typeTraverser = new TreeTraverser<Type>() {
        @Override
        public Iterable<Type> children(Type root) {
            if (root instanceof ParameterizedType) {
                ParameterizedType pType = (ParameterizedType) root;
                return Arrays.asList(pType.getActualTypeArguments());

            }
            return Collections.emptyList();
        }
    };
    for (Type t : typeTraverser.breadthFirstTraversal(type)) {
        types.add(t);
    }
    return types;
}

From source file:io.macgyver.core.resource.provider.filesystem.FileSystemResourceProvider.java

@Override
public Iterable<Resource> findResources(ResourceMatcher matcher) throws IOException {
    Preconditions.checkNotNull(matcher);
    List<Resource> tmp = Lists.newArrayList();
    TreeTraverser<File> tt = Files.fileTreeTraverser();
    String rootPath = rootDir.getCanonicalPath();
    for (File f : tt.breadthFirstTraversal(rootDir.getCanonicalFile())) {
        if (f.isFile() && isApprovedPath(f)) {
            String canonicalPath = f.getCanonicalPath();

            String virtualPath = canonicalPath;
            if (canonicalPath.startsWith(rootPath)) {
                virtualPath = canonicalPath.substring(rootPath.length());
            }//from  w  ww  .j av  a  2  s .  c o  m
            while (virtualPath.startsWith("/")) {
                virtualPath = virtualPath.substring(1);
            }
            virtualPath = removePrefix(virtualPath);
            FileSystemResource fsr = new FileSystemResource(this, virtualPath, f.getCanonicalFile());
            if (matcher.matches(fsr)) {
                tmp.add(fsr);
            }
        }
    }

    return tmp;
}

From source file:io.blobkeeper.common.util.MerkleTree.java

public List<LeafNode> getLeafNodes() {
    TreeTraverser treeTraverser = new TreeTraverser();
    return treeTraverser.breadthFirstTraversal(root).toList().stream().filter(node -> node instanceof LeafNode)
            .map(node -> (LeafNode) node).collect(toImmutableList());
}

From source file:org.apache.jackrabbit.oak.plugins.index.property.jmx.PropertyIndexStats.java

private String[] determineIndexedPaths(Iterable<? extends ChildNodeEntry> values, final int maxDepth,
        int maxPathCount) {
    Set<String> paths = Sets.newHashSet();
    Set<String> intermediatePaths = Sets.newHashSet();
    int maxPathLimitBreachedAtLevel = -1;
    topLevel: for (ChildNodeEntry cne : values) {
        Tree t = TreeFactory.createReadOnlyTree(cne.getNodeState());
        TreeTraverser<Tree> traverser = new TreeTraverser<Tree>() {
            @Override/*from w ww  .  ja  v  a  2 s .co  m*/
            public Iterable<Tree> children(@Nonnull Tree root) {
                //Break at maxLevel
                if (PathUtils.getDepth(root.getPath()) >= maxDepth) {
                    return Collections.emptyList();
                }
                return root.getChildren();
            }
        };
        for (Tree node : traverser.breadthFirstTraversal(t)) {
            PropertyState matchState = node.getProperty("match");
            boolean match = matchState == null ? false : matchState.getValue(Type.BOOLEAN);
            int depth = PathUtils.getDepth(node.getPath());

            //Intermediate nodes which are not leaf are not to be included
            if (depth < maxDepth && !match) {
                intermediatePaths.add(node.getPath());
                continue;
            }

            if (paths.size() < maxPathCount) {
                paths.add(node.getPath());
            } else {
                maxPathLimitBreachedAtLevel = depth;
                break topLevel;
            }
        }
    }

    if (maxPathLimitBreachedAtLevel < 0) {
        return Iterables.toArray(paths, String.class);
    }

    //If max limit for path is reached then we can safely
    //say about includedPaths upto depth = level at which limit reached - 1
    //As for that level we know *all* the path roots
    Set<String> result = Sets.newHashSet();
    int safeDepth = maxPathLimitBreachedAtLevel - 1;
    if (safeDepth > 0) {
        for (String path : intermediatePaths) {
            int pathDepth = PathUtils.getDepth(path);
            if (pathDepth == safeDepth) {
                result.add(path);
            }
        }
    }
    return Iterables.toArray(result, String.class);
}

From source file:org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexMBeanImpl.java

private String[] determineIndexedPaths(IndexSearcher searcher, final int maxLevel, int maxPathCount)
        throws IOException {
    Set<String> paths = Sets.newHashSet();
    int startDepth = getStartDepth(searcher, maxLevel);
    if (startDepth < 0) {
        return createMsg("startDepth cannot be determined after search for upto maxLevel [" + maxLevel + "]");
    }//  w  w  w  .  j  a  v  a  2 s .  c o  m

    SearchContext sc = new SearchContext(searcher, maxLevel, maxPathCount);
    List<LuceneDoc> docs = getDocsAtLevel(startDepth, sc);
    int maxPathLimitBreachedAtLevel = -1;
    topLevel: for (LuceneDoc doc : docs) {
        TreeTraverser<LuceneDoc> traverser = new TreeTraverser<LuceneDoc>() {
            @Override
            public Iterable<LuceneDoc> children(@Nonnull LuceneDoc root) {
                //Break at maxLevel
                if (root.depth >= maxLevel) {
                    return Collections.emptyList();
                }
                return root.getChildren();
            }
        };

        for (LuceneDoc node : traverser.breadthFirstTraversal(doc)) {
            if (paths.size() < maxPathCount) {
                paths.add(node.path);
            } else {
                maxPathLimitBreachedAtLevel = node.depth;
                break topLevel;
            }
        }
    }
    if (maxPathLimitBreachedAtLevel < 0) {
        return Iterables.toArray(paths, String.class);
    }

    //If max limit for path is reached then we can safely
    //say about includedPaths upto depth = level at which limit reached - 1
    //As for that level we know *all* the path roots
    Set<String> result = Sets.newHashSet();
    int safeDepth = maxPathLimitBreachedAtLevel - 1;
    if (safeDepth > 0) {
        for (String path : paths) {
            int pathDepth = PathUtils.getDepth(path);
            if (pathDepth == safeDepth) {
                result.add(path);
            }
        }
    }
    return Iterables.toArray(result, String.class);
}