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