com.link_intersystems.graph.tree.BreadthFirstTreeModelIterable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lis-commons-graph Show documentation
Show all versions of lis-commons-graph Show documentation
Link Intersystems Commons Graph (lis-commons-graph) is a collection of reusable software components
that provide support for graph and tree related programming tasks, such as models and search algorithms.
package com.link_intersystems.graph.tree;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
/**
* @author René Link {@literal }
*/
public class BreadthFirstTreeModelIterable extends AbstractTreeModelIterable {
public BreadthFirstTreeModelIterable(TreeModel treeModel, T rootElement) {
super(treeModel, rootElement);
}
@Override
protected Iterator createIterator(TreeModel treeModel, T rootElement) {
Queue queue = new LinkedList<>();
queue.offer(rootElement);
return new Iterator() {
@Override
public boolean hasNext() {
return !queue.isEmpty();
}
@Override
public T next() {
if (hasNext()) {
T next = queue.poll();
treeModel.getChildren(next).forEach(queue::add);
return next;
} else {
throw new NoSuchElementException();
}
}
};
}
}