info.javaspec.util.DfsSearch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javaspec Show documentation
Show all versions of javaspec Show documentation
Spec-style testing for Java
package info.javaspec.util;
import java.util.Stack;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
public final class DfsSearch {
private final N root;
private final Function> getChildren;
public DfsSearch(N root, Function> getChildren) {
this.root = root;
this.getChildren = getChildren;
}
public boolean anyNodeMatches(Predicate isMatchingNode) {
Stack toVisit = new Stack();
toVisit.push(root);
while(!toVisit.isEmpty()) {
N current = toVisit.pop();
if(isMatchingNode.test(current))
return true;
else
getChildren.apply(current).forEach(toVisit::push);
}
return false;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy