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

com.synopsys.integration.bdio.graph.DependencyGraph Maven / Gradle / Ivy

Go to download

A library to allow for easy and clear creation of Black Duck I/O (bdio) documents.

The newest version!
/*
 * integration-bdio
 *
 * Copyright (c) 2024 Synopsys, Inc.
 *
 * Use subject to the terms and conditions of the Synopsys End User Software License and Maintenance Agreement. All rights reserved worldwide.
 */
package com.synopsys.integration.bdio.graph;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.jetbrains.annotations.Nullable;

import com.synopsys.integration.bdio.model.dependency.Dependency;
import com.synopsys.integration.bdio.model.externalid.ExternalId;

public abstract class DependencyGraph {
    protected final Map dependencies = new HashMap<>();
    protected final Map> relationships = new HashMap<>();

    /**
     * @deprecated (use addDirectDependency instead)
     */
    @Deprecated
    public void addChildToRoot(Dependency child) {
        addDirectDependency(child);
    }

    public abstract void addDirectDependency(Dependency child);

    public abstract Set getDirectDependencies();

    // Includes the "root" ProjectDependency node if the implementing graph contains one
    public abstract Set getRootDependencies();

    public void copyGraphToRoot(DependencyGraph sourceGraph) {
        DependencyGraphUtil.copyRootDependencies(this, sourceGraph);
    }

    public boolean hasDependency(ExternalId dependency) {
        return dependencies.containsKey(dependency);
    }

    public boolean hasDependency(Dependency dependency) {
        return dependencies.containsKey(dependency.getExternalId());
    }

    @Nullable
    public Dependency getDependency(ExternalId dependency) {
        return dependencies.getOrDefault(dependency, null);
    }

    public Set getChildrenForParent(ExternalId parent) {
        return getChildrenExternalIdsForParent(parent).stream()
            .map(dependencies::get)
            .collect(Collectors.toSet());
    }

    public Set getParentsForChild(ExternalId child) {
        return getParentExternalIdsForChild(child).stream()
            .map(dependencies::get)
            .collect(Collectors.toSet());
    }

    public Set getChildrenExternalIdsForParent(ExternalId parent) {
        return relationships.getOrDefault(parent, new HashSet<>());
    }

    public Set getChildrenForParent(Dependency parent) {
        return getChildrenForParent(parent.getExternalId());
    }

    public Set getParentsForChild(Dependency child) {
        return getParentsForChild(child.getExternalId());
    }

    public Set getChildrenExternalIdsForParent(Dependency parent) {
        return getChildrenExternalIdsForParent(parent.getExternalId());
    }

    public Set getParentExternalIdsForChild(Dependency child) {
        return getParentExternalIdsForChild(child.getExternalId());
    }

    public Set getParentExternalIdsForChild(ExternalId child) {
        return relationships.entrySet().stream()
            .filter(externalIdSetEntry ->
                externalIdSetEntry.getValue().stream()
                    .anyMatch(child::equals)
            )
            .map(Map.Entry::getKey)
            .collect(Collectors.toSet());
    }

    public void addParentWithChild(Dependency parent, Dependency child) {
        ensureDependencyAndRelationshipExists(parent);
        ensureDependencyExists(child);
        addRelationship(parent, child);
    }

    public void addChildWithParent(Dependency child, Dependency parent) {
        addParentWithChild(parent, child);
    }

    public void addParentWithChildren(Dependency parent, List children) {
        ensureDependencyAndRelationshipExists(parent);
        for (Dependency child : children) {
            ensureDependencyExists(child);
            addRelationship(parent, child);
        }
    }

    public void addChildWithParents(Dependency child, List parents) {
        ensureDependencyExists(child);
        for (Dependency parent : parents) {
            ensureDependencyAndRelationshipExists(parent);
            addRelationship(parent, child);
        }

    }

    public void addParentWithChildren(Dependency parent, Set children) {
        ensureDependencyAndRelationshipExists(parent);
        for (Dependency child : children) {
            ensureDependencyExists(child);
            addRelationship(parent, child);
        }
    }

    public void addChildWithParents(Dependency child, Set parents) {
        ensureDependencyExists(child);
        for (Dependency parent : parents) {
            ensureDependencyAndRelationshipExists(parent);
            addRelationship(parent, child);
        }
    }

    public void addParentWithChildren(Dependency parent, Dependency... children) {
        addParentWithChildren(parent, Arrays.asList(children));
    }

    public void addChildWithParents(Dependency child, Dependency... parents) {
        addChildWithParents(child, Arrays.asList(parents));
    }

    public void addChildrenToRoot(List children) {
        children.forEach(this::addDirectDependency);
    }

    public void addChildrenToRoot(Set children) {
        children.forEach(this::addDirectDependency);
    }

    public void addChildrenToRoot(Dependency... children) {
        Arrays.stream(children).forEach(this::addDirectDependency);
    }

    protected void ensureDependencyExists(Dependency dependency) {
        dependencies.putIfAbsent(dependency.getExternalId(), dependency);
    }

    protected void ensureDependencyAndRelationshipExists(Dependency dependency) {
        ensureDependencyExists(dependency);
        relationships.putIfAbsent(dependency.getExternalId(), new HashSet<>());
    }

    protected void addRelationship(Dependency parent, Dependency child) {
        relationships.get(parent.getExternalId())
            .add(child.getExternalId());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy