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<ArtifactMetadata>( 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 MetadataTreeNode node = null;
91
92 for ( ArtifactMetadata md : classpath )
93 {
94 node = new MetadataTreeNode( md, parent, md.isResolved(), md.getArtifactScope() );
95 if ( tree == null )
96 {
97 tree = node;
98 }
99
100 if ( parent != null )
101 {
102 parent.setNChildren( 1 );
103 parent.addChild( 0, node );
104 }
105
106 parent = node;
107
108 }
109 return tree;
110 }
111
112 public void setClasspath( List<ArtifactMetadata> classpath )
113 {
114 this.classpath = classpath;
115 }
116
117 public ArtifactScopeEnum getScope()
118 {
119 return scope;
120 }
121
122 public void setScope( ArtifactScopeEnum scope )
123 {
124 this.scope = scope;
125 }
126
127
128 @Override
129 public String toString()
130 {
131 StringBuilder sb = new StringBuilder( 256 );
132 sb.append( "[scope=" + scope.getScope() );
133 if ( classpath != null )
134 {
135 for ( ArtifactMetadata md : classpath )
136 {
137 sb.append( ": " + md.toString() + '{' + md.getArtifactUri() + '}' );
138 }
139 }
140 sb.append( ']' );
141 return sb.toString();
142 }
143
144
145 }