1 /*******************************************************************************
2 * Copyright (c) 2010, 2012 Sonatype, Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Sonatype, Inc. - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.aether
.artifact
;
14 import java
.util
.Collections
;
15 import java
.util
.HashMap
;
17 import java
.util
.regex
.Matcher
;
18 import java
.util
.regex
.Pattern
;
21 * A skeleton class for artifacts.
23 public abstract class AbstractArtifact
27 private static final String SNAPSHOT
= "SNAPSHOT";
29 private static final Pattern SNAPSHOT_TIMESTAMP
= Pattern
.compile( "^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$" );
31 public boolean isSnapshot()
33 return isSnapshot( getVersion() );
36 private static boolean isSnapshot( String version
)
38 return version
.endsWith( SNAPSHOT
) || SNAPSHOT_TIMESTAMP
.matcher( version
).matches();
41 public String
getBaseVersion()
43 return toBaseVersion( getVersion() );
46 private static String
toBaseVersion( String version
)
50 if ( version
== null )
52 baseVersion
= version
;
54 else if ( version
.startsWith( "[" ) || version
.startsWith( "(" ) )
56 baseVersion
= version
;
60 Matcher m
= SNAPSHOT_TIMESTAMP
.matcher( version
);
63 if ( m
.group( 1 ) != null )
65 baseVersion
= m
.group( 1 ) + SNAPSHOT
;
69 baseVersion
= SNAPSHOT
;
74 baseVersion
= version
;
82 * Creates a new artifact with the specified coordinates, properties and file.
84 * @param version The version of the artifact, may be {@code null}.
85 * @param properties The properties of the artifact, may be {@code null} if none. The method may assume immutability
86 * of the supplied map, i.e. need not copy it.
87 * @param file The resolved file of the artifact, may be {@code null}.
88 * @return The new artifact instance, never {@code null}.
90 private Artifact
newInstance( String version
, Map
<String
, String
> properties
, File file
)
92 return new DefaultArtifact( getGroupId(), getArtifactId(), getClassifier(), getExtension(), version
, file
,
96 public Artifact
setVersion( String version
)
98 String current
= getVersion();
99 if ( current
.equals( version
) || ( version
== null && current
.length() <= 0 ) )
103 return newInstance( version
, getProperties(), getFile() );
106 public Artifact
setFile( File file
)
108 File current
= getFile();
109 if ( ( current
== null ) ? file
== null : current
.equals( file
) )
113 return newInstance( getVersion(), getProperties(), file
);
116 public Artifact
setProperties( Map
<String
, String
> properties
)
118 Map
<String
, String
> current
= getProperties();
119 if ( current
.equals( properties
) || ( properties
== null && current
.isEmpty() ) )
123 return newInstance( getVersion(), copyProperties( properties
), getFile() );
126 public String
getProperty( String key
, String defaultValue
)
128 String value
= getProperties().get( key
);
129 return ( value
!= null ) ? value
: defaultValue
;
133 * Copies the specified artifact properties. This utility method should be used when creating new artifact instances
134 * with caller-supplied properties.
136 * @param properties The properties to copy, may be {@code null}.
137 * @return The copied and read-only properties, never {@code null}.
139 protected static Map
<String
, String
> copyProperties( Map
<String
, String
> properties
)
141 if ( properties
!= null && !properties
.isEmpty() )
143 return Collections
.unmodifiableMap( new HashMap
<String
, String
>( properties
) );
147 return Collections
.emptyMap();
152 public String
toString()
154 StringBuilder buffer
= new StringBuilder( 128 );
155 buffer
.append( getGroupId() );
156 buffer
.append( ':' ).append( getArtifactId() );
157 buffer
.append( ':' ).append( getExtension() );
158 if ( getClassifier().length() > 0 )
160 buffer
.append( ':' ).append( getClassifier() );
162 buffer
.append( ':' ).append( getVersion() );
163 return buffer
.toString();
167 * Compares this artifact with the specified object.
169 * @param obj The object to compare this artifact against, may be {@code null}.
170 * @return {@code true} if and only if the specified object is another {@link Artifact} with equal coordinates,
171 * properties and file, {@code false} otherwise.
174 public boolean equals( Object obj
)
180 else if ( !( obj
instanceof Artifact
) )
185 Artifact that
= (Artifact
) obj
;
187 return getArtifactId().equals( that
.getArtifactId() ) && getGroupId().equals( that
.getGroupId() )
188 && getVersion().equals( that
.getVersion() ) && getExtension().equals( that
.getExtension() )
189 && getClassifier().equals( that
.getClassifier() ) && eq( getFile(), that
.getFile() )
190 && getProperties().equals( that
.getProperties() );
193 private static <T
> boolean eq( T s1
, T s2
)
195 return s1
!= null ? s1
.equals( s2
) : s2
== null;
199 * Returns a hash code for this artifact.
201 * @return A hash code for the artifact.
204 public int hashCode()
207 hash
= hash
* 31 + getGroupId().hashCode();
208 hash
= hash
* 31 + getArtifactId().hashCode();
209 hash
= hash
* 31 + getExtension().hashCode();
210 hash
= hash
* 31 + getClassifier().hashCode();
211 hash
= hash
* 31 + getVersion().hashCode();
212 hash
= hash
* 31 + hash( getFile() );
216 private static int hash( Object obj
)
218 return ( obj
!= null ) ? obj
.hashCode() : 0;