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

com.link_intersystems.graph.tree.DepthFirstTreeModelIterable 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.NoSuchElementException;
import java.util.Stack;
import java.util.stream.Stream;

/**
 * @author René Link {@literal }
 */
public class DepthFirstTreeModelIterable extends AbstractTreeModelIterable {

    public DepthFirstTreeModelIterable(TreeModel treeModel, T rootElement) {
        super(treeModel, rootElement);
    }

    @Override
    protected Iterator createIterator(TreeModel treeModel, T rootElement) {
        Stack stack = new Stack<>();
        stack.push(rootElement);

        return new Iterator() {

            @Override
            public boolean hasNext() {
                return !stack.isEmpty();
            }

            @Override
            public T next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }

                T current = stack.pop();

                Stream children = treeModel.getChildren(current);
                int headIndex = stack.size();
                children.forEach(c -> stack.add(headIndex, c));

                return current;
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy