All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kendal.utils.ForestUtils Maven / Gradle / Ivy

There is a newer version: 0.6
Show newest version
package kendal.utils;

import kendal.model.Node;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;

public class ForestUtils {

    private ForestUtils() {
        // prevent instantiation
    }

    public static void traverse(Collection roots, Consumer consumer) {
        new Traverser(roots, consumer).traverse();
    }

    private static class Traverser {
        private Collection roots;
        private Consumer consumer;
        private Set visited;

        Traverser(Collection roots, Consumer consumer) {
            this.roots = roots;
            this.consumer = consumer;
            this.visited = new HashSet<>();

        }

        private void traverse() {
            roots.forEach(this::traverse);
        }

        private void traverse(Node node) {
            if(!visited.contains(node)) {
                visited.add(node);
                consumer.accept(node);
                node.getChildren().forEach(this::traverse);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy