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

com.link_intersystems.graph.tree.FlatTreeModel 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.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * A {@link TreeModel} that flattens the tree structure for a given parent down to its leaves.
 * 

* E.g. if the base model describes the following tree: *

 *
 *          +-(B)--(E)
 *          |
 *      (A)-+-(C)
 *          |
 *          |     +-(F)
 *          |     |
 *          +-(D)-+
 *                |
 *                +-(G)
 * 
*

* A {@link FlatTreeModel} of that base model will look like this: * *

 *          +-(E)
 *          |
 *      (A)-+-(C)
 *          |
 *          +-(F)
 *          |
 *          +-(G)
 * 
* * @author René Link {@literal } */ public class FlatTreeModel implements TreeModel { private TreeModel baseModel; public FlatTreeModel(TreeModel baseModel) { this.baseModel = baseModel; } @Override public Stream getChildren(T parent) { if (baseModel.hasChildren(parent)) { List allChildren = new ArrayList<>(); Stream children = baseModel.getChildren(parent); children.forEach(c -> { if (baseModel.hasChildren(c)) { getChildren(c).forEach(allChildren::add); } else { allChildren.add(c); } }); return (Stream) allChildren.stream(); } else { return Stream.empty(); } } }