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

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

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:neon.common.files.FileUtils.java

/**
 * Copy the contents of a folder to another folder. This method will 
 * overwrite existing items in the destination folder.
 * /*  ww  w  . jav a 2 s.c  o m*/
 * @param from
 * @param to
 */
public static void copyFolder(Path from, Path to) {
    logger.info("copying files from " + from + " to " + to);

    TreeTraverser<File> traverser = Files.fileTreeTraverser();
    for (File file : traverser.preOrderTraversal(from.toFile())) {
        // parent folder should not be copied!
        if (!file.isDirectory()) {
            // construct the destination path from the origin path
            Path origin = Paths.get(file.getPath());
            Path relative = from.relativize(origin);
            Path destination = to.resolve(relative);
            logger.finest("copying file " + relative + " to " + to);

            try {
                Files.copy(file, destination.toFile());
            } catch (IOException e) {
                logger.severe("could not write file " + destination);
            }
        } else if (!file.equals(from.toFile())) {
            Path origin = Paths.get(file.getPath());
            Path relative = from.relativize(origin);
            Path destination = to.resolve(relative);

            if (!destination.toFile().exists()) {
                destination.toFile().mkdirs();
            }
        }
    }
}

From source file:neon.common.files.FileUtils.java

/**
 * Moves a folder to a new destination. If the destination folder already
 * exists, the contents will be overwritten.
 * //from   w  w  w  .  ja  v  a  2s . co  m
 * @param from
 * @param to
 * @throws IOException 
 */
public static void moveFolder(Path from, Path to) {
    logger.info("moving files from " + from + " to " + to);

    TreeTraverser<File> traverser = Files.fileTreeTraverser();

    // first move all the files
    for (File file : traverser.preOrderTraversal(from.toFile())) {
        // parent folder should not be moved!
        if (!file.isDirectory()) {
            // construct the destination path from the origin path
            Path origin = Paths.get(file.getPath());
            Path relative = from.relativize(origin);
            Path destination = to.resolve(relative);

            try {
                destination.toFile().delete();
                Files.move(file, destination.toFile());
            } catch (IOException e) {
                logger.severe("could not write file " + destination);
            }
        } else if (!file.equals(from.toFile())) {
            Path origin = Paths.get(file.getPath());
            Path relative = from.relativize(origin);
            Path destination = to.resolve(relative);

            if (!destination.toFile().exists()) {
                destination.toFile().mkdirs();
            }
        }
    }

    // then clean up all the leftovers
    clearFolder(from);
}

From source file:com.android.build.gradle.shrinker.FullRunShrinker.java

private void handleInterfaceInheritance(@NonNull Set<T> interfaceInheritance) {
    for (final T klass : interfaceInheritance) {
        mExecutor.execute(new Callable<Void>() {
            @Override/*from   ww  w  . java  2 s  . c om*/
            public Void call() throws Exception {
                TreeTraverser<T> interfaceTraverser = TypeHierarchyTraverser.interfaces(mGraph,
                        mShrinkerLogger);

                if ((mGraph.getModifiers(klass) & Opcodes.ACC_INTERFACE) != 0) {

                    // The "children" name is unfortunate: in the type hierarchy tree traverser,
                    // these are the interfaces that klass (which is an interface itself)
                    // extends (directly).
                    Iterable<T> superinterfaces = interfaceTraverser.children(klass);

                    for (T superinterface : superinterfaces) {
                        if (!mGraph.isLibraryClass(superinterface)) {
                            // Add the arrow going "down", from the superinterface to this one.
                            mGraph.addDependency(superinterface, klass, DependencyType.SUPERINTERFACE_KEPT);
                        } else {
                            // The superinterface is part of the SDK, so it's always kept. As
                            // long as there's any class that implements this interface, it
                            // needs to be kept.
                            mGraph.incrementAndCheck(klass, DependencyType.SUPERINTERFACE_KEPT,
                                    CounterSet.SHRINK);
                        }
                    }
                }

                for (T iface : interfaceTraverser.preOrderTraversal(klass)) {
                    if (!mGraph.isLibraryClass(iface)) {
                        mGraph.addDependency(klass, iface, DependencyType.INTERFACE_IMPLEMENTED);
                    }
                }

                return null;
            }
        });
    }
}