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

com.link_intersystems.graph.tree.BreadthFirstTreeModelIterable Maven / Gradle / Ivy

Go to download

There is a newer version: 1.9.7
Show newest version
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();
                }
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy