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

org.gradle.api.internalivyservice.resolveengine.graph.DependencyGraphPathResolver Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.api.internal.artifacts.ivyservice.resolveengine.graph;

import org.gradle.api.artifacts.component.ComponentIdentifier;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DependencyGraphPathResolver {

    public static Collection> calculatePaths(List fromNodes, DependencyGraphNode toNode) {
        // Include the shortest path from each version that has a direct dependency on the broken dependency, back to the root

        Map> shortestPaths = new LinkedHashMap<>();
        List rootPath = new ArrayList<>();
        rootPath.add(toNode.getOwner().getComponentId());
        shortestPaths.put(toNode.getOwner(), rootPath);

        Set directDependees = new LinkedHashSet<>();
        for (DependencyGraphNode node : fromNodes) {
            directDependees.add(node.getOwner());
        }

        Set seen = new HashSet<>();
        LinkedList queue = new LinkedList<>(directDependees);
        while (!queue.isEmpty()) {
            DependencyGraphComponent version = queue.getFirst();
            if (version == toNode.getOwner()) {
                queue.removeFirst();
            } else if (seen.add(version)) {
                for (DependencyGraphComponent incomingVersion : version.getDependents()) {
                    queue.add(0, incomingVersion);
                }
            } else {
                queue.remove();
                List shortest = null;
                for (DependencyGraphComponent incomingVersion : version.getDependents()) {
                    List candidate = shortestPaths.get(incomingVersion);
                    if (candidate == null) {
                        continue;
                    }
                    if (shortest == null) {
                        shortest = candidate;
                    } else if (shortest.size() > candidate.size()) {
                        shortest = candidate;
                    }

                }
                if (shortest == null) {
                    continue;
                }
                List path = new ArrayList<>(shortest);
                path.add(version.getComponentId());
                shortestPaths.put(version, path);
            }
        }

        List> paths = new ArrayList<>();
        for (DependencyGraphComponent version : directDependees) {
            List path = shortestPaths.get(version);
            paths.add(path);
        }
        return paths;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy