aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/component/chain/dependencies/ordering/CycleDependenciesException.java
blob: ede0cacf18bde75349ea0ec10dd207f298e138ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.component.chain.dependencies.ordering;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Signals that the dependency graph contains cycles. A DOT language
 * representation of the cycle is available to help solve the problem (<a
 * href="http://graphviz.org/">GraphViz</a>).
 *
 * @author Tony Vaagenes
 */
@SuppressWarnings("serial")
public class CycleDependenciesException extends RuntimeException {

    public Map<String, NameProvider> cycleNodes;

    CycleDependenciesException(Map<String, NameProvider> cycleNodes) {
        super("The following set of dependencies lead to a cycle:\n"
                + createDotString(cycleNodes));
        this.cycleNodes = cycleNodes;
    }

    private static String createDotString(Map<String, NameProvider> cycleNodes) {
        StringBuilder res = new StringBuilder();
        res.append("digraph dependencyGraph {\n");

        Set<Node> used = new HashSet<>();
        for (Node node: cycleNodes.values()) {
            if (!node.ready())
                node.dotDependenciesString(res, used);

        }
        res.append("}");
        return res.toString();
    }


    public String dotString() {
        return createDotString(cycleNodes);
    }

}