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 * @deprecated See {@link AbstractDependencyNodeConsumerVisitor} that is more versatile. 51 */ 52 @Deprecated 53 abstract class AbstractDepthFirstNodeListGenerator implements DependencyVisitor { 54 55 private final Map<DependencyNode, Object> visitedNodes; 56 57 protected final List<DependencyNode> nodes; 58 59 AbstractDepthFirstNodeListGenerator() { 60 nodes = new ArrayList<>(128); 61 visitedNodes = new IdentityHashMap<>(512); 62 } 63 64 /** 65 * Gets the list of dependency nodes that was generated during the graph traversal. 66 * 67 * @return The list of dependency nodes, never {@code null}. 68 */ 69 public List<DependencyNode> getNodes() { 70 return nodes; 71 } 72 73 /** 74 * Gets the dependencies seen during the graph traversal. 75 * 76 * @param includeUnresolved Whether unresolved dependencies shall be included in the result or not. 77 * @return The list of dependencies, never {@code null}. 78 */ 79 public List<Dependency> getDependencies(boolean includeUnresolved) { 80 return NodeListGenerator.getDependencies(getNodes(), includeUnresolved); 81 } 82 83 /** 84 * Gets the artifacts associated with the list of dependency nodes generated during the graph traversal. 85 * 86 * @param includeUnresolved Whether unresolved artifacts shall be included in the result or not. 87 * @return The list of artifacts, never {@code null}. 88 */ 89 public List<Artifact> getArtifacts(boolean includeUnresolved) { 90 return NodeListGenerator.getArtifacts(getNodes(), includeUnresolved); 91 } 92 93 /** 94 * Gets the files of resolved artifacts seen during the graph traversal. 95 * 96 * @return The list of artifact files, never {@code null}. 97 */ 98 public List<File> getFiles() { 99 return NodeListGenerator.getFiles(getNodes()); 100 } 101 102 /** 103 * Gets a class path by concatenating the artifact files of the visited dependency nodes. Nodes with unresolved 104 * artifacts are automatically skipped. 105 * 106 * @return The class path, using the platform-specific path separator, never {@code null}. 107 */ 108 public String getClassPath() { 109 return NodeListGenerator.getClassPath(getNodes()); 110 } 111 112 /** 113 * Marks the specified node as being visited and determines whether the node has been visited before. 114 * 115 * @param node The node being visited, must not be {@code null}. 116 * @return {@code true} if the node has not been visited before, {@code false} if the node was already visited. 117 */ 118 protected boolean setVisited(DependencyNode node) { 119 return visitedNodes.put(node, Boolean.TRUE) == null; 120 } 121 122 @Override 123 public abstract boolean visitEnter(DependencyNode node); 124 125 @Override 126 public abstract boolean visitLeave(DependencyNode node); 127 }