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