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