1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.eclipse.aether.artifact.Artifact;
29 import org.eclipse.aether.graph.Dependency;
30 import org.eclipse.aether.graph.DependencyNode;
31 import org.eclipse.aether.graph.DependencyVisitor;
32
33
34
35
36
37
38
39
40
41
42 abstract class AbstractDepthFirstNodeListGenerator implements DependencyVisitor {
43
44 private final Map<DependencyNode, Object> visitedNodes;
45
46 protected final List<DependencyNode> nodes;
47
48 AbstractDepthFirstNodeListGenerator() {
49 nodes = new ArrayList<>(128);
50 visitedNodes = new IdentityHashMap<>(512);
51 }
52
53
54
55
56
57
58 public List<DependencyNode> getNodes() {
59 return nodes;
60 }
61
62
63
64
65
66
67
68 public List<Dependency> getDependencies(boolean includeUnresolved) {
69 List<Dependency> dependencies = new ArrayList<>(getNodes().size());
70
71 for (DependencyNode node : getNodes()) {
72 Dependency dependency = node.getDependency();
73 if (dependency != null) {
74 if (includeUnresolved || dependency.getArtifact().getFile() != null) {
75 dependencies.add(dependency);
76 }
77 }
78 }
79
80 return dependencies;
81 }
82
83
84
85
86
87
88
89 public List<Artifact> getArtifacts(boolean includeUnresolved) {
90 List<Artifact> artifacts = new ArrayList<>(getNodes().size());
91
92 for (DependencyNode node : getNodes()) {
93 if (node.getDependency() != null) {
94 Artifact artifact = node.getDependency().getArtifact();
95 if (includeUnresolved || artifact.getFile() != null) {
96 artifacts.add(artifact);
97 }
98 }
99 }
100
101 return artifacts;
102 }
103
104
105
106
107
108
109 public List<File> getFiles() {
110 List<File> files = new ArrayList<>(getNodes().size());
111
112 for (DependencyNode node : getNodes()) {
113 if (node.getDependency() != null) {
114 File file = node.getDependency().getArtifact().getFile();
115 if (file != null) {
116 files.add(file);
117 }
118 }
119 }
120
121 return files;
122 }
123
124
125
126
127
128
129
130 public String getClassPath() {
131 StringBuilder buffer = new StringBuilder(1024);
132
133 for (Iterator<DependencyNode> it = getNodes().iterator(); it.hasNext(); ) {
134 DependencyNode node = it.next();
135 if (node.getDependency() != null) {
136 Artifact artifact = node.getDependency().getArtifact();
137 if (artifact.getFile() != null) {
138 buffer.append(artifact.getFile().getAbsolutePath());
139 if (it.hasNext()) {
140 buffer.append(File.pathSeparatorChar);
141 }
142 }
143 }
144 }
145
146 return buffer.toString();
147 }
148
149
150
151
152
153
154
155 protected boolean setVisited(DependencyNode node) {
156 return visitedNodes.put(node, Boolean.TRUE) == null;
157 }
158
159 @Override
160 public abstract boolean visitEnter(DependencyNode node);
161
162 @Override
163 public abstract boolean visitLeave(DependencyNode node);
164 }