1 package org.eclipse.aether.util.artifact;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.eclipse.aether.artifact.Artifact;
23
24 /**
25 * A utility class for artifact identifiers.
26 */
27 public final class ArtifactIdUtils
28 {
29
30 private static final char SEP = ':';
31
32 private ArtifactIdUtils()
33 {
34 // hide constructor
35 }
36
37 /**
38 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<version>}.
39 *
40 * @param artifact The artifact to create an identifer for, may be {@code null}.
41 * @return The artifact identifier or {@code null} if the input was {@code null}.
42 */
43 public static String toId( Artifact artifact )
44 {
45 String id = null;
46 if ( artifact != null )
47 {
48 id =
49 toId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(),
50 artifact.getClassifier(), artifact.getVersion() );
51 }
52 return id;
53 }
54
55 /**
56 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<version>}.
57 *
58 * @param groupId The group id, may be {@code null}.
59 * @param artifactId The artifact id, may be {@code null}.
60 * @param extension The file extensiion, may be {@code null}.
61 * @param classifier The classifier, may be {@code null}.
62 * @param version The version, may be {@code null}.
63 * @return The artifact identifier, never {@code null}.
64 */
65 public static String toId( String groupId, String artifactId, String extension, String classifier, String version )
66 {
67 StringBuilder buffer = concat( groupId, artifactId, extension, classifier );
68 buffer.append( SEP );
69 if ( version != null )
70 {
71 buffer.append( version );
72 }
73 return buffer.toString();
74 }
75
76 /**
77 * Creates an artifact identifier of the form
78 * {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<baseVersion>}.
79 *
80 * @param artifact The artifact to create an identifer for, may be {@code null}.
81 * @return The artifact identifier or {@code null} if the input was {@code null}.
82 */
83 public static String toBaseId( Artifact artifact )
84 {
85 String id = null;
86 if ( artifact != null )
87 {
88 id =
89 toId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(),
90 artifact.getClassifier(), artifact.getBaseVersion() );
91 }
92 return id;
93 }
94
95 /**
96 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]}.
97 *
98 * @param artifact The artifact to create an identifer for, may be {@code null}.
99 * @return The artifact identifier or {@code null} if the input was {@code null}.
100 */
101 public static String toVersionlessId( Artifact artifact )
102 {
103 String id = null;
104 if ( artifact != null )
105 {
106 id =
107 toVersionlessId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(),
108 artifact.getClassifier() );
109 }
110 return id;
111 }
112
113 /**
114 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]}.
115 *
116 * @param groupId The group id, may be {@code null}.
117 * @param artifactId The artifact id, may be {@code null}.
118 * @param extension The file extensiion, may be {@code null}.
119 * @param classifier The classifier, may be {@code null}.
120 * @return The artifact identifier, never {@code null}.
121 */
122 public static String toVersionlessId( String groupId, String artifactId, String extension, String classifier )
123 {
124 return concat( groupId, artifactId, extension, classifier ).toString();
125 }
126
127 private static StringBuilder concat( String groupId, String artifactId, String extension, String classifier )
128 {
129 StringBuilder buffer = new StringBuilder( 128 );
130
131 if ( groupId != null )
132 {
133 buffer.append( groupId );
134 }
135 buffer.append( SEP );
136 if ( artifactId != null )
137 {
138 buffer.append( artifactId );
139 }
140 buffer.append( SEP );
141 if ( extension != null )
142 {
143 buffer.append( extension );
144 }
145 if ( classifier != null && classifier.length() > 0 )
146 {
147 buffer.append( SEP ).append( classifier );
148 }
149
150 return buffer;
151 }
152
153 /**
154 * Determines whether two artifacts have the same identifier. This method is equivalent to calling
155 * {@link String#equals(Object)} on the return values from {@link #toId(Artifact)} for the artifacts but does not
156 * incur the overhead of creating temporary strings.
157 *
158 * @param artifact1 The first artifact, may be {@code null}.
159 * @param artifact2 The second artifact, may be {@code null}.
160 * @return {@code true} if both artifacts are not {@code null} and have equal ids, {@code false} otherwise.
161 */
162 public static boolean equalsId( Artifact artifact1, Artifact artifact2 )
163 {
164 if ( artifact1 == null || artifact2 == null )
165 {
166 return false;
167 }
168 if ( !eq( artifact1.getArtifactId(), artifact2.getArtifactId() ) )
169 {
170 return false;
171 }
172 if ( !eq( artifact1.getGroupId(), artifact2.getGroupId() ) )
173 {
174 return false;
175 }
176 if ( !eq( artifact1.getExtension(), artifact2.getExtension() ) )
177 {
178 return false;
179 }
180 if ( !eq( artifact1.getClassifier(), artifact2.getClassifier() ) )
181 {
182 return false;
183 }
184 if ( !eq( artifact1.getVersion(), artifact2.getVersion() ) )
185 {
186 return false;
187 }
188 return true;
189 }
190
191 /**
192 * Determines whether two artifacts have the same base identifier. This method is equivalent to calling
193 * {@link String#equals(Object)} on the return values from {@link #toBaseId(Artifact)} for the artifacts but does
194 * not incur the overhead of creating temporary strings.
195 *
196 * @param artifact1 The first artifact, may be {@code null}.
197 * @param artifact2 The second artifact, may be {@code null}.
198 * @return {@code true} if both artifacts are not {@code null} and have equal base ids, {@code false} otherwise.
199 */
200 public static boolean equalsBaseId( Artifact artifact1, Artifact artifact2 )
201 {
202 if ( artifact1 == null || artifact2 == null )
203 {
204 return false;
205 }
206 if ( !eq( artifact1.getArtifactId(), artifact2.getArtifactId() ) )
207 {
208 return false;
209 }
210 if ( !eq( artifact1.getGroupId(), artifact2.getGroupId() ) )
211 {
212 return false;
213 }
214 if ( !eq( artifact1.getExtension(), artifact2.getExtension() ) )
215 {
216 return false;
217 }
218 if ( !eq( artifact1.getClassifier(), artifact2.getClassifier() ) )
219 {
220 return false;
221 }
222 if ( !eq( artifact1.getBaseVersion(), artifact2.getBaseVersion() ) )
223 {
224 return false;
225 }
226 return true;
227 }
228
229 /**
230 * Determines whether two artifacts have the same versionless identifier. This method is equivalent to calling
231 * {@link String#equals(Object)} on the return values from {@link #toVersionlessId(Artifact)} for the artifacts but
232 * does not incur the overhead of creating temporary strings.
233 *
234 * @param artifact1 The first artifact, may be {@code null}.
235 * @param artifact2 The second artifact, may be {@code null}.
236 * @return {@code true} if both artifacts are not {@code null} and have equal versionless ids, {@code false}
237 * otherwise.
238 */
239 public static boolean equalsVersionlessId( Artifact artifact1, Artifact artifact2 )
240 {
241 if ( artifact1 == null || artifact2 == null )
242 {
243 return false;
244 }
245 if ( !eq( artifact1.getArtifactId(), artifact2.getArtifactId() ) )
246 {
247 return false;
248 }
249 if ( !eq( artifact1.getGroupId(), artifact2.getGroupId() ) )
250 {
251 return false;
252 }
253 if ( !eq( artifact1.getExtension(), artifact2.getExtension() ) )
254 {
255 return false;
256 }
257 if ( !eq( artifact1.getClassifier(), artifact2.getClassifier() ) )
258 {
259 return false;
260 }
261 return true;
262 }
263
264 private static <T> boolean eq( T s1, T s2 )
265 {
266 return s1 != null ? s1.equals( s2 ) : s2 == null;
267 }
268
269 }