1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.eclipse.aether.util.graph.visitor; 20 21 import java.io.File; 22 import java.util.ArrayList; 23 import java.util.IdentityHashMap; 24 import java.util.List; 25 import java.util.Map; 26 27 import org.eclipse.aether.artifact.Artifact; 28 import org.eclipse.aether.graph.Dependency; 29 import org.eclipse.aether.graph.DependencyNode; 30 import org.eclipse.aether.graph.DependencyVisitor; 31 32 /** 33 * Abstract base class for depth first dependency tree traverses. Subclasses of this visitor will visit each node 34 * exactly once regardless how many paths within the dependency graph lead to the node such that the resulting node 35 * sequence is free of duplicates. 36 * <p> 37 * Actual vertex ordering (preorder, inorder, postorder) needs to be defined by subclasses through appropriate 38 * implementations for {@link #visitEnter(org.eclipse.aether.graph.DependencyNode)} and 39 * {@link #visitLeave(org.eclipse.aether.graph.DependencyNode)}. 40 * <p> 41 * Note: inorder vertex ordering is not provided out of the box, as resolver cannot partition (or does not know how to 42 * partition) the node children into "left" and "right" partitions. 43 * <p> 44 * The newer classes {@link AbstractDependencyNodeConsumerVisitor} and {@link NodeListGenerator} offer 45 * similar capabilities but are pluggable. Use of this class, while not deprecated, is discouraged. This class 46 * is not used in Resolver and is kept only for backward compatibility reasons. 47 * 48 * @see AbstractDependencyNodeConsumerVisitor 49 */ 50 abstract class AbstractDepthFirstNodeListGenerator implements DependencyVisitor { 51 52 private final Map<DependencyNode, Object> visitedNodes; 53 54 protected final List<DependencyNode> nodes; 55 56 AbstractDepthFirstNodeListGenerator() { 57 nodes = new ArrayList<>(128); 58 visitedNodes = new IdentityHashMap<>(512); 59 } 60 61 /** 62 * Gets the list of dependency nodes that was generated during the graph traversal. 63 * 64 * @return The list of dependency nodes, never {@code null}. 65 */ 66 public List<DependencyNode> getNodes() { 67 return nodes; 68 } 69 70 /** 71 * Gets the dependencies seen during the graph traversal. 72 * 73 * @param includeUnresolved Whether unresolved dependencies shall be included in the result or not. 74 * @return The list of dependencies, never {@code null}. 75 */ 76 public List<Dependency> getDependencies(boolean includeUnresolved) { 77 return NodeListGenerator.getDependencies(getNodes(), includeUnresolved); 78 } 79 80 /** 81 * Gets the artifacts associated with the list of dependency nodes generated during the graph traversal. 82 * 83 * @param includeUnresolved Whether unresolved artifacts shall be included in the result or not. 84 * @return The list of artifacts, never {@code null}. 85 */ 86 public List<Artifact> getArtifacts(boolean includeUnresolved) { 87 return NodeListGenerator.getArtifacts(getNodes(), includeUnresolved); 88 } 89 90 /** 91 * Gets the files of resolved artifacts seen during the graph traversal. 92 * 93 * @return The list of artifact files, never {@code null}. 94 */ 95 public List<File> getFiles() { 96 return NodeListGenerator.getFiles(getNodes()); 97 } 98 99 /** 100 * Gets a class path by concatenating the artifact files of the visited dependency nodes. Nodes with unresolved 101 * artifacts are automatically skipped. 102 * 103 * @return The class path, using the platform-specific path separator, never {@code null}. 104 */ 105 public String getClassPath() { 106 return NodeListGenerator.getClassPath(getNodes()); 107 } 108 109 /** 110 * Marks the specified node as being visited and determines whether the node has been visited before. 111 * 112 * @param node The node being visited, must not be {@code null}. 113 * @return {@code true} if the node has not been visited before, {@code false} if the node was already visited. 114 */ 115 protected boolean setVisited(DependencyNode node) { 116 return visitedNodes.put(node, Boolean.TRUE) == null; 117 } 118 119 @Override 120 public abstract boolean visitEnter(DependencyNode node); 121 122 @Override 123 public abstract boolean visitLeave(DependencyNode node); 124 }