001package org.eclipse.aether.util.artifact; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.eclipse.aether.artifact.Artifact; 023 024import java.util.Objects; 025 026/** 027 * A utility class for artifact identifiers. 028 */ 029public final class ArtifactIdUtils 030{ 031 032 private static final char SEP = ':'; 033 034 private ArtifactIdUtils() 035 { 036 // hide constructor 037 } 038 039 /** 040 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<version>}. 041 * 042 * @param artifact The artifact to create an identifer for, may be {@code null}. 043 * @return The artifact identifier or {@code null} if the input was {@code null}. 044 */ 045 public static String toId( Artifact artifact ) 046 { 047 String id = null; 048 if ( artifact != null ) 049 { 050 id = 051 toId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(), 052 artifact.getClassifier(), artifact.getVersion() ); 053 } 054 return id; 055 } 056 057 /** 058 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<version>}. 059 * 060 * @param groupId The group id, may be {@code null}. 061 * @param artifactId The artifact id, may be {@code null}. 062 * @param extension The file extensiion, may be {@code null}. 063 * @param classifier The classifier, may be {@code null}. 064 * @param version The version, may be {@code null}. 065 * @return The artifact identifier, never {@code null}. 066 */ 067 public static String toId( String groupId, String artifactId, String extension, String classifier, String version ) 068 { 069 StringBuilder buffer = concat( groupId, artifactId, extension, classifier ); 070 buffer.append( SEP ); 071 if ( version != null ) 072 { 073 buffer.append( version ); 074 } 075 return buffer.toString(); 076 } 077 078 /** 079 * Creates an artifact identifier of the form 080 * {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<baseVersion>}. 081 * 082 * @param artifact The artifact to create an identifer for, may be {@code null}. 083 * @return The artifact identifier or {@code null} if the input was {@code null}. 084 */ 085 public static String toBaseId( Artifact artifact ) 086 { 087 String id = null; 088 if ( artifact != null ) 089 { 090 id = 091 toId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(), 092 artifact.getClassifier(), artifact.getBaseVersion() ); 093 } 094 return id; 095 } 096 097 /** 098 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]}. 099 * 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}