1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * classpath container that is aware of the classpath scope
29 *
30 *
31 */
32 @Deprecated
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 @Override
51 public Iterator<ArtifactMetadata> iterator() {
52 return classpath == null ? null : classpath.iterator();
53 }
54
55 // -------------------------------------------------------------------------------------------
56 public ClasspathContainer add(ArtifactMetadata md) {
57 if (classpath == null) {
58 classpath = new ArrayList<>(16);
59 }
60
61 classpath.add(md);
62
63 return this;
64 }
65
66 // -------------------------------------------------------------------------------------------
67 public List<ArtifactMetadata> getClasspath() {
68 return classpath;
69 }
70
71 // -------------------------------------------------------------------------------------------
72 public MetadataTreeNode getClasspathAsTree() throws MetadataResolutionException {
73 if (classpath == null || classpath.size() < 1) {
74 return null;
75 }
76
77 MetadataTreeNode tree = null;
78 MetadataTreeNode parent = null;
79
80 for (ArtifactMetadata md : classpath) {
81 MetadataTreeNode node = new MetadataTreeNode(md, parent, md.isResolved(), md.getArtifactScope());
82 if (tree == null) {
83 tree = node;
84 }
85
86 if (parent != null) {
87 parent.setNChildren(1);
88 parent.addChild(0, node);
89 }
90
91 parent = node;
92 }
93 return tree;
94 }
95
96 public void setClasspath(List<ArtifactMetadata> classpath) {
97 this.classpath = classpath;
98 }
99
100 public ArtifactScopeEnum getScope() {
101 return scope;
102 }
103
104 public void setScope(ArtifactScopeEnum scope) {
105 this.scope = scope;
106 }
107
108 // -------------------------------------------------------------------------------------------
109 @Override
110 public String toString() {
111 StringBuilder sb = new StringBuilder(256);
112 sb.append("[scope=").append(scope.getScope());
113 if (classpath != null) {
114 for (ArtifactMetadata md : classpath) {
115 sb.append(": ")
116 .append(md.toString())
117 .append('{')
118 .append(md.getArtifactUri())
119 .append('}');
120 }
121 }
122 sb.append(']');
123 return sb.toString();
124 }
125 // -------------------------------------------------------------------------------------------
126 // -------------------------------------------------------------------------------------------
127 }