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 024/** 025 * A utility class for artifact identifiers. 026 */ 027public final class ArtifactIdUtils 028{ 029 030 private static final char SEP = ':'; 031 032 private ArtifactIdUtils() 033 { 034 // hide constructor 035 } 036 037 /** 038 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<version>}. 039 * 040 * @param artifact The artifact to create an identifer for, may be {@code null}. 041 * @return The artifact identifier or {@code null} if the input was {@code null}. 042 */ 043 public static String toId( Artifact artifact ) 044 { 045 String id = null; 046 if ( artifact != null ) 047 { 048 id = 049 toId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(), 050 artifact.getClassifier(), artifact.getVersion() ); 051 } 052 return id; 053 } 054 055 /** 056 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<version>}. 057 * 058 * @param groupId The group id, may be {@code null}. 059 * @param artifactId The artifact id, may be {@code null}. 060 * @param extension The file extensiion, may be {@code null}. 061 * @param classifier The classifier, may be {@code null}. 062 * @param version The version, may be {@code null}. 063 * @return The artifact identifier, never {@code null}. 064 */ 065 public static String toId( String groupId, String artifactId, String extension, String classifier, String version ) 066 { 067 StringBuilder buffer = concat( groupId, artifactId, extension, classifier ); 068 buffer.append( SEP ); 069 if ( version != null ) 070 { 071 buffer.append( version ); 072 } 073 return buffer.toString(); 074 } 075 076 /** 077 * Creates an artifact identifier of the form 078 * {@code <groupId>:<artifactId>:<extension>[:<classifier>]:<baseVersion>}. 079 * 080 * @param artifact The artifact to create an identifer for, may be {@code null}. 081 * @return The artifact identifier or {@code null} if the input was {@code null}. 082 */ 083 public static String toBaseId( Artifact artifact ) 084 { 085 String id = null; 086 if ( artifact != null ) 087 { 088 id = 089 toId( artifact.getGroupId(), artifact.getArtifactId(), artifact.getExtension(), 090 artifact.getClassifier(), artifact.getBaseVersion() ); 091 } 092 return id; 093 } 094 095 /** 096 * Creates an artifact identifier of the form {@code <groupId>:<artifactId>:<extension>[:<classifier>]}. 097 * 098 * @param artifact The artifact to create an identifer for, may be {@code null}. 099 * @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}