kendal.utils.ForestUtils Maven / Gradle / Ivy
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