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

net.nemerosa.ontrack.model.support.tree.DefaultNode Maven / Gradle / Ivy

There is a newer version: 4.4.5
Show newest version
package net.nemerosa.ontrack.model.support.tree;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@ToString
public class DefaultNode implements Node {

    private final NodeFactory factory;
    private final D data;
    private final List> children;

    public DefaultNode(NodeFactory factory, D data) {
        this(factory, data, Collections.>emptyList());
    }

    public DefaultNode(NodeFactory factory, D data, Collection> children) {
        this.factory = factory;
        this.data = data;
        this.children = new ArrayList<>(children);
    }

    @Override
    public Iterable> getChildren() {
        return children;
    }

    @Override
    public Node append(Node child) {
        children.add(child);
        return this;
    }

    @Override
    public void visit(NodeVisitor nodeVisitor) {
        nodeVisitor.start(this);
        for (Node child : children) {
            child.visit(nodeVisitor);
        }
        nodeVisitor.end(this);
    }

    @Override
    public D getData() {
        return data;
    }

    @Override
    @JsonIgnore
    public NodeFactory getFactory() {
        return factory;
    }

    @Override
    @JsonIgnore
    public boolean isLeaf() {
        return children.isEmpty();
    }

    @Override
    public Node transform(NodeTransformer transformer) {
        return transform(transformer, factory);
    }

    @Override
    public Node transform(NodeTransformer transformer, NodeFactory factory) {
        Node t;
        if (isLeaf()) {
            t = factory.leaf(data);
        } else {
            List> newKids = new ArrayList<>();
            for (Node child : children) {
                Node newKid = child.transform(transformer, factory);
                if (newKid.getData() == null) {
                    // If the returned transformed node does not contain
                    // any data, only its oyn children are added
                    for (Node grandChild : newKid.getChildren()) {
                        newKids.add(grandChild);
                    }
                } else {
                    newKids.add(newKid);
                }
            }
            t = factory.node(data, newKids);
        }
        return transformer.transform(t);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy