001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.util.graph.visitor;
020
021import java.util.Collection;
022import java.util.function.Consumer;
023import java.util.function.Function;
024
025import org.eclipse.aether.graph.DependencyNode;
026
027/**
028 * A {@link DependencyGraphDumper} that is safe to use on a graph containing cycles: a dependency graph in
029 * {@code FULL} verbosity mode may contain nodes that (directly or transitively) depend on an ancestor of
030 * themselves, which makes the plain {@link DependencyGraphDumper} recurse forever. Wrapping it in
031 * {@link TreeDependencyVisitor} avoids the stack overflow but also hides every node that was already visited
032 * anywhere else in the graph, so a node reached a second time through a different path (a perfectly normal
033 * diamond dependency, not a cycle) is silently dropped from the output as well.
034 * <p>
035 * This dumper only checks the path from the root to the node currently being visited: a node is only treated as
036 * a cycle when it is already one of its own ancestors. Diamonds are still printed in full, and a true cycle is
037 * printed once, marked, with its subtree cut off instead of being recursed into.
038 *
039 * @since 2.0.25
040 */
041public class CycleAwareDependencyGraphDumper extends DependencyGraphDumper {
042
043    /**
044     * Creates instance with given consumer.
045     *
046     * @param consumer the string consumer, must not be {@code null}
047     */
048    public CycleAwareDependencyGraphDumper(Consumer<String> consumer) {
049        super(consumer);
050    }
051
052    /**
053     * Creates instance with given consumer and decorators.
054     *
055     * @param consumer the string consumer, must not be {@code null}
056     * @param decorators the decorators to apply, must not be {@code null}
057     */
058    public CycleAwareDependencyGraphDumper(
059            Consumer<String> consumer, Collection<Function<DependencyNode, String>> decorators) {
060        super(consumer, decorators);
061    }
062
063    @Override
064    public boolean visitEnter(DependencyNode node) {
065        if (isOnPath(node)) {
066            nodes.push(node);
067            consumer.accept(formatLine(nodes) + " (cycle)");
068            return false;
069        }
070        return super.visitEnter(node);
071    }
072
073    /**
074     * Returns {@code true} if {@code node} is already one of its own ancestors on the path from the root to the
075     * node currently being visited, i.e. descending into it would recurse forever.
076     */
077    private boolean isOnPath(DependencyNode node) {
078        for (DependencyNode ancestor : nodes) {
079            if (ancestor == node) {
080                return true;
081            }
082        }
083        return false;
084    }
085}