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