KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > artifact > ArtifactUtils


1 package org.apache.maven.artifact;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.artifact.versioning.VersionRange;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 public final class ArtifactUtils
29 {
30
31     private ArtifactUtils()
32     {
33     }
34
35     public static boolean isSnapshot( String JavaDoc version )
36     {
37         return version != null &&
38             ( version.toUpperCase().endsWith( "SNAPSHOT" ) || Artifact.VERSION_FILE_PATTERN.matcher( version )
39                 .matches() );
40     }
41
42     public static String JavaDoc versionlessKey( Artifact artifact )
43     {
44         return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
45     }
46
47     public static String JavaDoc versionlessKey( String JavaDoc groupId, String JavaDoc artifactId )
48     {
49         if ( groupId == null )
50         {
51             throw new NullPointerException JavaDoc( "groupId was null" );
52         }
53         if ( artifactId == null )
54         {
55             throw new NullPointerException JavaDoc( "artifactId was null" );
56         }
57         return groupId + ":" + artifactId;
58     }
59
60     public static String JavaDoc artifactId( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc type, String JavaDoc version )
61     {
62         return artifactId( groupId, artifactId, type, null, version );
63     }
64
65     public static String JavaDoc artifactId( String JavaDoc groupId, String JavaDoc artifactId, String JavaDoc type, String JavaDoc classifier,
66                                      String JavaDoc baseVersion )
67     {
68         return groupId + ":" + artifactId + ":" + type + ( classifier != null ? ":" + classifier : "" ) + ":" +
69             baseVersion;
70     }
71
72     public static Map JavaDoc artifactMapByVersionlessId( Collection JavaDoc artifacts )
73     {
74         Map JavaDoc artifactMap = new HashMap JavaDoc();
75
76         if ( artifacts != null )
77         {
78             for ( Iterator JavaDoc it = artifacts.iterator(); it.hasNext(); )
79             {
80                 Artifact artifact = (Artifact) it.next();
81
82                 artifactMap.put( versionlessKey( artifact ), artifact );
83             }
84         }
85
86         return artifactMap;
87     }
88
89     public static Map JavaDoc artifactMapByArtifactId( Collection JavaDoc artifacts )
90     {
91         Map JavaDoc artifactMap = new HashMap JavaDoc();
92
93         if ( artifacts != null )
94         {
95             for ( Iterator JavaDoc it = artifacts.iterator(); it.hasNext(); )
96             {
97                 Artifact artifact = (Artifact) it.next();
98
99                 artifactMap.put( artifact.getId(), artifact );
100             }
101         }
102
103         return artifactMap;
104     }
105
106     public static Artifact copyArtifact( Artifact artifact )
107     {
108         VersionRange range = artifact.getVersionRange();
109         DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(),
110                                                      artifact.getScope(), artifact.getType(), artifact.getClassifier(),
111                                                      artifact.getArtifactHandler(), artifact.isOptional() );
112         clone.setRelease( artifact.isRelease() );
113         clone.setResolvedVersion( artifact.getVersion() );
114         clone.setResolved( artifact.isResolved() );
115         clone.setFile( artifact.getFile() );
116
117         clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) );
118         clone.setBaseVersion( artifact.getBaseVersion() );
119         clone.setDependencyFilter( artifact.getDependencyFilter() );
120         clone.setDependencyTrail( copyList( artifact.getDependencyTrail() ) );
121         clone.setDownloadUrl( artifact.getDownloadUrl() );
122         clone.setRepository( artifact.getRepository() );
123
124         return clone;
125     }
126     
127     private static List JavaDoc copyList( List JavaDoc original )
128     {
129         List JavaDoc copy = null;
130         
131         if ( original != null )
132         {
133             copy = new ArrayList JavaDoc();
134             
135             if ( !original.isEmpty() )
136             {
137                 copy.addAll( original );
138             }
139         }
140         
141         return copy;
142     }
143
144 }
145
Popular Tags