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 import java.util.Objects;
25
26 /**
27 * A utility class for artifact identifiers.
28 */
29 public final class ArtifactIdUtils
30 {
31
32 private static final char SEP = ':';
33
34 private ArtifactIdUtils()
35 {
36 // hide constructor
37 }
38
39 /**
40 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<version>}.
41 *
42 * @param artifact The artifact to create an identifer for, may be {@code null}.
43 * @return The artifact identifier or {@code null} if the input was {@code null}.
44 */
45 public static String toId( Artifact artifact )
46 {
47 String id = null;
48 if ( artifact != null )
49 {
50 id =
51 toId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(),
52 artifact.getClassifier(), artifact.getVersion() );
53 }
54 return id;
55 }
56
57 /**
58 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<version>}.
59 *
60 * @param groupId The group id, may be {@code null}.
61 * @param artifactId The artifact id, may be {@code null}.
62 * @param extension The file extensiion, may be {@code null}.
63 * @param classifier The classifier, may be {@code null}.
64 * @param version The version, may be {@code null}.
65 * @return The artifact identifier, never {@code null}.
66 */
67 public static String toId( String groupId, String artifactId, String extension, String classifier, String version )
68 {
69 StringBuilder buffer = concat( groupId, artifactId, extension, classifier );
70 buffer.append( SEP );
71 if ( version != null )
72 {
73 buffer.append( version );
74 }
75 return buffer.toString();
76 }
77
78 /**
79 * Creates an artifact identifier of the form
80 * {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<baseVersion>}.
81 *
82 * @param artifact The artifact to create an identifer for, may be {@code null}.
83 * @return The artifact identifier or {@code null} if the input was {@code null}.
84 */
85 public static String toBaseId( Artifact artifact )
86 {
87 String id = null;
88 if ( artifact != null )
89 {
90 id =
91 toId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(),
92 artifact.getClassifier(), artifact.getBaseVersion() );
93 }
94 return id;
95 }
96
97 /**
98 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]}.
99 *
100 * @param artifact The artifact to create an identifer for, may be {@code null}.
101 * @return The artifact identifier or {@code null} if the input was {@code null}.
102 */
103 public static String toVersionlessId( Artifact artifact )
104 {
105 String id = null;
106 if ( artifact != null )
107 {
108 id =
109 toVersionlessId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(),
110 artifact.getClassifier() );
111 }
112 return id;
113 }
114
115 /**
116 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]}.
117 *
118 * @param groupId The group id, may be {@code null}.
119 * @param artifactId The artifact id, may be {@code null}.
120 * @param extension The file extensiion, may be {@code null}.
121 * @param classifier The classifier, may be {@code null}.
122 * @return The artifact identifier, never {@code null}.
123 */
124 public static String toVersionlessId( String groupId, String artifactId, String extension, String classifier )
125 {
126 return concat( groupId, artifactId, extension, classifier ).toString();
127 }
128
129 private static StringBuilder concat( String groupId, String artifactId, String extension, String classifier )
130 {
131 StringBuilder buffer = new StringBuilder( 128 );
132
133 if ( groupId != null )
134 {
135 buffer.append( groupId );
136 }
137 buffer.append( SEP );
138 if ( artifactId != null )
139 {
140 buffer.append( artifactId );
141 }
142 buffer.append( SEP );
143 if ( extension != null )
144 {
145 buffer.append( extension );
146 }
147 if ( classifier != null && classifier.length() > 0 )
148 {
149 buffer.append( SEP ).append( classifier );
150 }
151
152 return buffer;
153 }
154
155 /**
156 * Determines whether two artifacts have the same identifier. This method is equivalent to calling
157 * {@link String#equals(Object)} on the return values from {@link #toId(Artifact)} for the artifacts but does not
158 * incur the overhead of creating temporary strings.
159 *
160 * @param artifact1 The first artifact, may be {@code null}.
161 * @param artifact2 The second artifact, may be {@code null}.
162 * @return {@code true} if both artifacts are not {@code null} and have equal ids, {@code false} otherwise.
163 */
164 public static boolean equalsId( Artifact artifact1, Artifact artifact2 )
165 {
166 if ( artifact1 == null || artifact2 == null )
167 {
168 return false;
169 }
170 if ( !Objects.equals( artifact1.getArtifactId(), artifact2.getArtifactId() ) )
171 {
172 return false;
173 }
174 if ( !Objects.equals( artifact1.getGroupId(), artifact2.getGroupId() ) )
175 {
176 return false;
177 }
178 if ( !Objects.equals( artifact1.getExtension(), artifact2.getExtension() ) )
179 {
180 return false;
181 }
182 if ( !Objects.equals( artifact1.getClassifier(), artifact2.getClassifier() ) )
183 {
184 return false;
185 }
186 if ( !Objects.equals( artifact1.getVersion(), artifact2.getVersion() ) )
187 {
188 return false;
189 }
190 return true;
191 }
192
193 /**
194 * Determines whether two artifacts have the same base identifier. This method is equivalent to calling
195 * {@link String#equals(Object)} on the return values from {@link #toBaseId(Artifact)} for the artifacts but does
196 * not incur the overhead of creating temporary strings.
197 *
198 * @param artifact1 The first artifact, may be {@code null}.
199 * @param artifact2 The second artifact, may be {@code null}.
200 * @return {@code true} if both artifacts are not {@code null} and have equal base ids, {@code false} otherwise.
201 */
202 public static boolean equalsBaseId( Artifact artifact1, Artifact artifact2 )
203 {
204 if ( artifact1 == null || artifact2 == null )
205 {
206 return false;
207 }
208 if ( !Objects.equals( artifact1.getArtifactId(), artifact2.getArtifactId() ) )
209 {
210 return false;
211 }
212 if ( !Objects.equals( artifact1.getGroupId(), artifact2.getGroupId() ) )
213 {
214 return false;
215 }
216 if ( !Objects.equals( artifact1.getExtension(), artifact2.getExtension() ) )
217 {
218 return false;
219 }
220 if ( !Objects.equals( artifact1.getClassifier(), artifact2.getClassifier() ) )
221 {
222 return false;
223 }
224 if ( !Objects.equals( artifact1.getBaseVersion(), artifact2.getBaseVersion() ) )
225 {
226 return false;
227 }
228 return true;
229 }
230
231 /**
232 * Determines whether two artifacts have the same versionless identifier. This method is equivalent to calling
233 * {@link String#equals(Object)} on the return values from {@link #toVersionlessId(Artifact)} for the artifacts but
234 * does not incur the overhead of creating temporary strings.
235 *
236 * @param artifact1 The first artifact, may be {@code null}.
237 * @param artifact2 The second artifact, may be {@code null}.
238 * @return {@code true} if both artifacts are not {@code null} and have equal versionless ids, {@code false}
239 * otherwise.
240 */
241 public static boolean equalsVersionlessId( Artifact artifact1, Artifact artifact2 )
242 {
243 if ( artifact1 == null || artifact2 == null )
244 {
245 return false;
246 }
247 if ( !Objects.equals( artifact1.getArtifactId(), artifact2.getArtifactId() ) )
248 {
249 return false;
250 }
251 if ( !Objects.equals( artifact1.getGroupId(), artifact2.getGroupId() ) )
252 {
253 return false;
254 }
255 if ( !Objects.equals( artifact1.getExtension(), artifact2.getExtension() ) )
256 {
257 return false;
258 }
259 if ( !Objects.equals( artifact1.getClassifier(), artifact2.getClassifier() ) )
260 {
261 return false;
262 }
263 return true;
264 }
265 }