1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.metadata;
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import org.apache.maven.artifact.ArtifactScopeEnum;
25
26
27
28
29
30
31
32 public class ClasspathContainer implements Iterable<ArtifactMetadata> {
33 private List<ArtifactMetadata> classpath;
34
35 private ArtifactScopeEnum scope;
36
37
38 public ClasspathContainer(ArtifactScopeEnum scope) {
39 this.scope = ArtifactScopeEnum.checkScope(scope);
40 }
41
42
43 public ClasspathContainer(List<ArtifactMetadata> classpath, ArtifactScopeEnum scope) {
44 this(scope);
45 this.classpath = classpath;
46 }
47
48
49 public Iterator<ArtifactMetadata> iterator() {
50 return classpath == null ? null : classpath.iterator();
51 }
52
53
54 public ClasspathContainer add(ArtifactMetadata md) {
55 if (classpath == null) {
56 classpath = new ArrayList<>(16);
57 }
58
59 classpath.add(md);
60
61 return this;
62 }
63
64
65 public List<ArtifactMetadata> getClasspath() {
66 return classpath;
67 }
68
69
70 public MetadataTreeNode getClasspathAsTree() throws MetadataResolutionException {
71 if (classpath == null || classpath.size() < 1) {
72 return null;
73 }
74
75 MetadataTreeNode tree = null;
76 MetadataTreeNode parent = null;
77
78 for (ArtifactMetadata md : classpath) {
79 MetadataTreeNode node = new MetadataTreeNode(md, parent, md.isResolved(), md.getArtifactScope());
80 if (tree == null) {
81 tree = node;
82 }
83
84 if (parent != null) {
85 parent.setNChildren(1);
86 parent.addChild(0, node);
87 }
88
89 parent = node;
90 }
91 return tree;
92 }
93
94 public void setClasspath(List<ArtifactMetadata> classpath) {
95 this.classpath = classpath;
96 }
97
98 public ArtifactScopeEnum getScope() {
99 return scope;
100 }
101
102 public void setScope(ArtifactScopeEnum scope) {
103 this.scope = scope;
104 }
105
106
107 @Override
108 public String toString() {
109 StringBuilder sb = new StringBuilder(256);
110 sb.append("[scope=").append(scope.getScope());
111 if (classpath != null) {
112 for (ArtifactMetadata md : classpath) {
113 sb.append(": ")
114 .append(md.toString())
115 .append('{')
116 .append(md.getArtifactUri())
117 .append('}');
118 }
119 }
120 sb.append(']');
121 return sb.toString();
122 }
123
124
125 }