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

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

Introduction

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

Prototype

TreeTraverser

Source Link

Usage

From source file:edu.buaa.satla.analysis.util.CPAs.java

/**
 * Creates an iterable that enumerates all the CPAs contained in
 * a single CPA, including the root CPA itself.
 * The tree of elements is traversed in pre-order.
 *//*from  w ww .  j a v a 2s .  c o  m*/
public static FluentIterable<ConfigurableProgramAnalysis> asIterable(final ConfigurableProgramAnalysis pCpa) {

    return new TreeTraverser<ConfigurableProgramAnalysis>() {
        @Override
        public Iterable<ConfigurableProgramAnalysis> children(ConfigurableProgramAnalysis cpa) {
            return (cpa instanceof WrapperCPA) ? ((WrapperCPA) cpa).getWrappedCPAs()
                    : ImmutableList.<ConfigurableProgramAnalysis>of();
        }
    }.preOrderTraversal(pCpa);
}

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 ww  .j a  v a  2  s.  c  om
 * 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:org.mule.config.spring.SpringLifecycleCallback.java

@Override
protected Collection<?> lookupObjectsForLifecycle(LifecycleObject lo) {
    Map<String, Object> objects = getSpringRegistry().lookupEntriesForLifecycle(lo.getType());

    final DependencyNode root = new DependencyNode(null);

    for (Map.Entry<String, Object> entry : objects.entrySet()) {
        addDependency(root, entry.getKey(), entry.getValue());
    }//from  w w  w.j a v  a2  s  .c o m

    Iterable<DependencyNode> orderedNodes = new TreeTraverser<DependencyNode>() {
        @Override
        public Iterable children(DependencyNode node) {
            return node.getChilds();
        }
    }.postOrderTraversal(root);

    List<Object> orderedObjects = new LinkedList<>();
    for (DependencyNode node : orderedNodes) {
        if (node == root) {
            break;
        }

        orderedObjects.add(node.getValue());
    }

    return orderedObjects;
}

From source file:org.sosy_lab.cpachecker.util.Precisions.java

/**
 * Creates a {@link FluentIterable} that enumerates all the <code>Precision</code>
 * contained in a given precision pre-order. The root precision itself is included, the
 * precisions are unwrapped recursively.
 *
 * <p><b>Example</b>: Precision A wraps precisions B and C. Precision B wraps precisions D and E.<br />
 *             The resulting tree (see below) is traversed pre-order.
 * <pre>// w  w  w  . j  av a 2s . c om
 *                  A
 *                 / \
 *                B   C
 *               / \
 *              D   E
 * </pre>
 * The returned <code>FluentIterable</code> iterates over the items in the following
 * order : A, B, D, E, C.
 * </p>
 *
 * @param as the root state
 *
 * @return a <code>FluentIterable</code> over the given root precision and all precisions
 *         that are wrapped in it, recursively
 */
public static FluentIterable<Precision> asIterable(final Precision prec) {

    return new TreeTraverser<Precision>() {

        @Override
        public Iterable<Precision> children(Precision precision) {
            if (precision instanceof WrapperPrecision) {
                return ((WrapperPrecision) precision).getWrappedPrecisions();
            }

            return ImmutableList.of();
        }
    }.preOrderTraversal(prec);
}

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  w  w.j a 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 + "]");
    }/*from w  ww . ja v  a  2s . 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);
}

From source file:org.sosy_lab.cpachecker.util.AbstractStates.java

/**
 * Creates a {@link FluentIterable} that enumerates all the <code>AbstractStates</code>
 * contained in a given state pre-order. The root state itself is included, the states
 * are unwrapped recursively./*w ww  . j  av  a  2s  .com*/
 *
 * <p><b>Example</b>: State A wraps states B and C. State B wraps states D and E.<br />
 *             The resulting tree (see below) is traversed pre-order.
 * <pre>
 *                  A
 *                 / \
 *                B   C
 *               / \
 *              D   E
 * </pre>
 * The returned <code>FluentIterable</code> iterates over the items in the following
 * order : A, B, D, E, C.
 * </p>
 *
 * @param as the root state
 *
 * @return a <code>FluentIterable</code> over the given root state and all states
 *         that are wrapped in it, recursively
 */
public static FluentIterable<AbstractState> asIterable(final AbstractState as) {

    return new TreeTraverser<AbstractState>() {

        @Override
        public Iterable<AbstractState> children(AbstractState state) {
            if (state instanceof AbstractSingleWrapperState) {
                AbstractState wrapped = ((AbstractSingleWrapperState) state).getWrappedState();
                return ImmutableList.of(wrapped);

            } else if (state instanceof AbstractWrapperState) {
                return ((AbstractWrapperState) state).getWrappedStates();
            }

            return ImmutableList.of();
        }
    }.preOrderTraversal(as);
}

From source file:org.eclipse.buildship.ui.view.execution.ExecutionPage.java

public FluentIterable<OperationItem> filterTreeNodes(Predicate<OperationItem> predicate) {
    OperationItem root = (OperationItem) getPageControl().getViewer().getInput();
    if (root == null) {
        return FluentIterable.from(ImmutableList.<OperationItem>of());
    }//  www. ja  v a2 s .c  o  m

    return new TreeTraverser<OperationItem>() {

        @Override
        public Iterable<OperationItem> children(OperationItem operationItem) {
            return operationItem.getChildren();
        }
    }.breadthFirstTraversal(root).filter(predicate);
}