1 package org.apache.maven.artifact;
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.Collection;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.regex.Matcher;
28
29 import org.apache.maven.artifact.versioning.VersionRange;
30
31 public final class ArtifactUtils
32 {
33
34 public static boolean isSnapshot( String version )
35 {
36 if ( version != null )
37 {
38 if ( version.regionMatches( true, version.length() - Artifact.SNAPSHOT_VERSION.length(),
39 Artifact.SNAPSHOT_VERSION, 0, Artifact.SNAPSHOT_VERSION.length() ) )
40 {
41 return true;
42 }
43 else if ( Artifact.VERSION_FILE_PATTERN.matcher( version ).matches() )
44 {
45 return true;
46 }
47 }
48 return false;
49 }
50
51 public static String toSnapshotVersion( String version )
52 {
53 if ( version == null )
54 {
55 throw new IllegalArgumentException( "version: null" );
56 }
57
58 Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( version );
59 if ( m.matches() )
60 {
61 return m.group( 1 ) + "-" + Artifact.SNAPSHOT_VERSION;
62 }
63 else
64 {
65 return version;
66 }
67 }
68
69 public static String versionlessKey( Artifact artifact )
70 {
71 return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
72 }
73
74 public static String versionlessKey( String groupId, String artifactId )
75 {
76 if ( groupId == null )
77 {
78 throw new NullPointerException( "groupId is null" );
79 }
80 if ( artifactId == null )
81 {
82 throw new NullPointerException( "artifactId is null" );
83 }
84 return groupId + ":" + artifactId;
85 }
86
87 public static String key( Artifact artifact )
88 {
89 return key( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
90 }
91
92 public static String key( String groupId, String artifactId, String version )
93 {
94 if ( groupId == null )
95 {
96 throw new NullPointerException( "groupId is null" );
97 }
98 if ( artifactId == null )
99 {
100 throw new NullPointerException( "artifactId is null" );
101 }
102 if ( version == null )
103 {
104 throw new NullPointerException( "version is null" );
105 }
106
107 return groupId + ":" + artifactId + ":" + version;
108 }
109
110 public static Map<String, Artifact> artifactMapByVersionlessId( Collection<Artifact> artifacts )
111 {
112 Map<String, Artifact> artifactMap = new LinkedHashMap<String, Artifact>();
113
114 if ( artifacts != null )
115 {
116 for ( Artifact artifact : artifacts )
117 {
118 artifactMap.put( versionlessKey( artifact ), artifact );
119 }
120 }
121
122 return artifactMap;
123 }
124
125 public static Artifact copyArtifactSafe( Artifact artifact )
126 {
127 return ( artifact != null ) ? copyArtifact( artifact ) : null;
128 }
129
130 public static Artifact copyArtifact( Artifact artifact )
131 {
132 VersionRange range = artifact.getVersionRange();
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 if ( range == null )
150 {
151 range = VersionRange.createFromVersion( artifact.getVersion() );
152 }
153
154 DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(),
155 artifact.getScope(), artifact.getType(), artifact.getClassifier(),
156 artifact.getArtifactHandler(), artifact.isOptional() );
157 clone.setRelease( artifact.isRelease() );
158 clone.setResolvedVersion( artifact.getVersion() );
159 clone.setResolved( artifact.isResolved() );
160 clone.setFile( artifact.getFile() );
161
162 clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) );
163 if ( artifact.getVersion() != null )
164 {
165 clone.setBaseVersion( artifact.getBaseVersion() );
166 }
167 clone.setDependencyFilter( artifact.getDependencyFilter() );
168 clone.setDependencyTrail( copyList( artifact.getDependencyTrail() ) );
169 clone.setDownloadUrl( artifact.getDownloadUrl() );
170 clone.setRepository( artifact.getRepository() );
171
172 return clone;
173 }
174
175
176 public static <T extends Collection<Artifact>> T copyArtifacts( Collection<Artifact> from, T to )
177 {
178 for ( Artifact artifact : from )
179 {
180 to.add( ArtifactUtils.copyArtifact( artifact ) );
181 }
182 return to;
183 }
184
185 public static <K, T extends Map<K, Artifact>> T copyArtifacts( Map<K, ? extends Artifact> from, T to )
186 {
187 if ( from != null )
188 {
189 for ( Map.Entry<K, ? extends Artifact> entry : from.entrySet() )
190 {
191 to.put( entry.getKey(), ArtifactUtils.copyArtifact( entry.getValue() ) );
192 }
193 }
194
195 return to;
196 }
197
198 private static <T> List<T> copyList( List<T> original )
199 {
200 List<T> copy = null;
201
202 if ( original != null )
203 {
204 copy = new ArrayList<T>();
205
206 if ( !original.isEmpty() )
207 {
208 copy.addAll( original );
209 }
210 }
211
212 return copy;
213 }
214
215 }