001package org.eclipse.aether.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 java.io.File; 023import java.util.Collections; 024import java.util.HashMap; 025import java.util.Map; 026import java.util.Objects; 027import java.util.regex.Matcher; 028import java.util.regex.Pattern; 029 030/** 031 * A skeleton class for artifacts. 032 */ 033public abstract class AbstractArtifact 034 implements Artifact 035{ 036 037 private static final String SNAPSHOT = "SNAPSHOT"; 038 039 private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile( "^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$" ); 040 041 public boolean isSnapshot() 042 { 043 return isSnapshot( getVersion() ); 044 } 045 046 private static boolean isSnapshot( String version ) 047 { 048 return version.endsWith( SNAPSHOT ) || SNAPSHOT_TIMESTAMP.matcher( version ).matches(); 049 } 050 051 public String getBaseVersion() 052 { 053 return toBaseVersion( getVersion() ); 054 } 055 056 private static String toBaseVersion( String version ) 057 { 058 String baseVersion; 059 060 if ( version == null ) 061 { 062 baseVersion = version; 063 } 064 else if ( version.startsWith( "[" ) || version.startsWith( "(" ) ) 065 { 066 baseVersion = version; 067 } 068 else 069 { 070 Matcher m = SNAPSHOT_TIMESTAMP.matcher( version ); 071 if ( m.matches() ) 072 { 073 if ( m.group( 1 ) != null ) 074 { 075 baseVersion = m.group( 1 ) + SNAPSHOT; 076 } 077 else 078 { 079 baseVersion = SNAPSHOT; 080 } 081 } 082 else 083 { 084 baseVersion = version; 085 } 086 } 087 088 return baseVersion; 089 } 090 091 /** 092 * Creates a new artifact with the specified coordinates, properties and file. 093 * 094 * @param version The version of the artifact, may be {@code null}. 095 * @param properties The properties of the artifact, may be {@code null} if none. The method may assume immutability 096 * of the supplied map, i.e. need not copy it. 097 * @param file The resolved file of the artifact, may be {@code null}. 098 * @return The new artifact instance, never {@code null}. 099 */ 100 private Artifact newInstance( String version, Map<String, String> properties, File file ) 101 { 102 return new DefaultArtifact( getGroupId(), getArtifactId(), getClassifier(), getExtension(), version, file, 103 properties ); 104 } 105 106 public Artifact setVersion( String version ) 107 { 108 String current = getVersion(); 109 if ( current.equals( version ) || ( version == null && current.length() <= 0 ) ) 110 { 111 return this; 112 } 113 return newInstance( version, getProperties(), getFile() ); 114 } 115 116 public Artifact setFile( File file ) 117 { 118 File current = getFile(); 119 if ( Objects.equals( current, file ) ) 120 { 121 return this; 122 } 123 return newInstance( getVersion(), getProperties(), file ); 124 } 125 126 public Artifact setProperties( Map<String, String> properties ) 127 { 128 Map<String, String> current = getProperties(); 129 if ( current.equals( properties ) || ( properties == null && current.isEmpty() ) ) 130 { 131 return this; 132 } 133 return newInstance( getVersion(), copyProperties( properties ), getFile() ); 134 } 135 136 public String getProperty( String key, String defaultValue ) 137 { 138 String value = getProperties().get( key ); 139 return ( value != null ) ? value : defaultValue; 140 } 141 142 /** 143 * Copies the specified artifact properties. This utility method should be used when creating new artifact instances 144 * with caller-supplied properties. 145 * 146 * @param properties The properties to copy, may be {@code null}. 147 * @return The copied and read-only properties, never {@code null}. 148 */ 149 protected static Map<String, String> copyProperties( Map<String, String> properties ) 150 { 151 if ( properties != null && !properties.isEmpty() ) 152 { 153 return Collections.unmodifiableMap( new HashMap<>( properties ) ); 154 } 155 else 156 { 157 return Collections.emptyMap(); 158 } 159 } 160 161 @Override 162 public String toString() 163 { 164 StringBuilder buffer = new StringBuilder( 128 ); 165 buffer.append( getGroupId() ); 166 buffer.append( ':' ).append( getArtifactId() ); 167 buffer.append( ':' ).append( getExtension() ); 168 if ( getClassifier().length() > 0 ) 169 { 170 buffer.append( ':' ).append( getClassifier() ); 171 } 172 buffer.append( ':' ).append( getVersion() ); 173 return buffer.toString(); 174 } 175 176 /** 177 * Compares this artifact with the specified object. 178 * 179 * @param obj The object to compare this artifact against, may be {@code null}. 180 * @return {@code true} if and only if the specified object is another {@link Artifact} with equal coordinates, 181 * properties and file, {@code false} otherwise. 182 */ 183 @Override 184 public boolean equals( Object obj ) 185 { 186 if ( obj == this ) 187 { 188 return true; 189 } 190 else if ( !( obj instanceof Artifact ) ) 191 { 192 return false; 193 } 194 195 Artifact that = (Artifact) obj; 196 197 return Objects.equals( getArtifactId(), that.getArtifactId() ) 198 && Objects.equals( getGroupId(), that.getGroupId() ) 199 && Objects.equals( getVersion(), that.getVersion() ) 200 && Objects.equals( getExtension(), that.getExtension() ) 201 && Objects.equals( getClassifier(), that.getClassifier() ) 202 && Objects.equals( getFile(), that.getFile() ) 203 && Objects.equals( getProperties(), that.getProperties() ); 204 } 205 206 /** 207 * Returns a hash code for this artifact. 208 * 209 * @return A hash code for the artifact. 210 */ 211 @Override 212 public int hashCode() 213 { 214 int hash = 17; 215 hash = hash * 31 + getGroupId().hashCode(); 216 hash = hash * 31 + getArtifactId().hashCode(); 217 hash = hash * 31 + getExtension().hashCode(); 218 hash = hash * 31 + getClassifier().hashCode(); 219 hash = hash * 31 + getVersion().hashCode(); 220 hash = hash * 31 + hash( getFile() ); 221 return hash; 222 } 223 224 private static int hash( Object obj ) 225 { 226 return ( obj != null ) ? obj.hashCode() : 0; 227 } 228 229}