]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.repo/src/org/eclipse/aether/artifact/DefaultArtifact.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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 simple artifact. <em>Note:</em> Instances of this class are immutable and the exposed mutators return new objects
22 * rather than changing the current instance.
24 public final class DefaultArtifact
25 extends AbstractArtifact
28 private final String groupId
;
30 private final String artifactId
;
32 private final String version
;
34 private final String classifier
;
36 private final String extension
;
38 private final File file
;
40 private final Map
<String
, String
> properties
;
43 * Creates a new artifact with the specified coordinates. If not specified in the artifact coordinates, the
44 * artifact's extension defaults to {@code jar} and classifier to an empty string.
46 * @param coords The artifact coordinates in the format
47 * {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}, must not be {@code null}.
49 public DefaultArtifact( String coords
)
51 this( coords
, Collections
.<String
, String
> emptyMap() );
55 * Creates a new artifact with the specified coordinates and properties. If not specified in the artifact
56 * coordinates, the artifact's extension defaults to {@code jar} and classifier to an empty string.
58 * @param coords The artifact coordinates in the format
59 * {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}, must not be {@code null}.
60 * @param properties The artifact properties, may be {@code null}.
62 public DefaultArtifact( String coords
, Map
<String
, String
> properties
)
64 Pattern p
= Pattern
.compile( "([^: ]+):([^: ]+)(:([^: ]*)(:([^: ]+))?)?:([^: ]+)" );
65 Matcher m
= p
.matcher( coords
);
68 throw new IllegalArgumentException( "Bad artifact coordinates " + coords
69 + ", expected format is <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>" );
71 groupId
= m
.group( 1 );
72 artifactId
= m
.group( 2 );
73 extension
= get( m
.group( 4 ), "jar" );
74 classifier
= get( m
.group( 6 ), "" );
75 version
= m
.group( 7 );
77 this.properties
= copyProperties( properties
);
80 private static String
get( String value
, String defaultValue
)
82 return ( value
== null || value
.length() <= 0 ) ? defaultValue
: value
;
86 * Creates a new artifact with the specified coordinates and no classifier. Passing {@code null} for any of the
87 * coordinates is equivalent to specifying an empty string.
89 * @param groupId The group identifier of the artifact, may be {@code null}.
90 * @param artifactId The artifact identifier of the artifact, may be {@code null}.
91 * @param extension The file extension of the artifact, may be {@code null}.
92 * @param version The version of the artifact, may be {@code null}.
94 public DefaultArtifact( String groupId
, String artifactId
, String extension
, String version
)
96 this( groupId
, artifactId
, "", extension
, version
);
100 * Creates a new artifact with the specified coordinates. Passing {@code null} for any of the coordinates is
101 * equivalent to specifying an empty string.
103 * @param groupId The group identifier of the artifact, may be {@code null}.
104 * @param artifactId The artifact identifier of the artifact, may be {@code null}.
105 * @param classifier The classifier of the artifact, may be {@code null}.
106 * @param extension The file extension of the artifact, may be {@code null}.
107 * @param version The version of the artifact, may be {@code null}.
109 public DefaultArtifact( String groupId
, String artifactId
, String classifier
, String extension
, String version
)
111 this( groupId
, artifactId
, classifier
, extension
, version
, null, (File
) null );
115 * Creates a new artifact with the specified coordinates. Passing {@code null} for any of the coordinates is
116 * equivalent to specifying an empty string. The optional artifact type provided to this constructor will be used to
117 * determine the artifact's classifier and file extension if the corresponding arguments for this constructor are
120 * @param groupId The group identifier of the artifact, may be {@code null}.
121 * @param artifactId The artifact identifier of the artifact, may be {@code null}.
122 * @param classifier The classifier of the artifact, may be {@code null}.
123 * @param extension The file extension of the artifact, may be {@code null}.
124 * @param version The version of the artifact, may be {@code null}.
125 * @param type The artifact type from which to query classifier, file extension and properties, may be {@code null}.
127 public DefaultArtifact( String groupId
, String artifactId
, String classifier
, String extension
, String version
,
130 this( groupId
, artifactId
, classifier
, extension
, version
, null, type
);
134 * Creates a new artifact with the specified coordinates and properties. Passing {@code null} for any of the
135 * coordinates is equivalent to specifying an empty string. The optional artifact type provided to this constructor
136 * will be used to determine the artifact's classifier and file extension if the corresponding arguments for this
137 * constructor are {@code null}. If the artifact type specifies properties, those will get merged with the
138 * properties passed directly into the constructor, with the latter properties taking precedence.
140 * @param groupId The group identifier of the artifact, may be {@code null}.
141 * @param artifactId The artifact identifier of the artifact, may be {@code null}.
142 * @param classifier The classifier of the artifact, may be {@code null}.
143 * @param extension The file extension of the artifact, may be {@code null}.
144 * @param version The version of the artifact, may be {@code null}.
145 * @param properties The properties of the artifact, may be {@code null} if none.
146 * @param type The artifact type from which to query classifier, file extension and properties, may be {@code null}.
148 public DefaultArtifact( String groupId
, String artifactId
, String classifier
, String extension
, String version
,
149 Map
<String
, String
> properties
, ArtifactType type
)
151 this.groupId
= emptify( groupId
);
152 this.artifactId
= emptify( artifactId
);
153 if ( classifier
!= null || type
== null )
155 this.classifier
= emptify( classifier
);
159 this.classifier
= emptify( type
.getClassifier() );
161 if ( extension
!= null || type
== null )
163 this.extension
= emptify( extension
);
167 this.extension
= emptify( type
.getExtension() );
169 this.version
= emptify( version
);
171 this.properties
= merge( properties
, ( type
!= null ) ? type
.getProperties() : null );
174 private static Map
<String
, String
> merge( Map
<String
, String
> dominant
, Map
<String
, String
> recessive
)
176 Map
<String
, String
> properties
;
178 if ( ( dominant
== null || dominant
.isEmpty() ) && ( recessive
== null || recessive
.isEmpty() ) )
180 properties
= Collections
.emptyMap();
184 properties
= new HashMap
<String
, String
>();
185 if ( recessive
!= null )
187 properties
.putAll( recessive
);
189 if ( dominant
!= null )
191 properties
.putAll( dominant
);
193 properties
= Collections
.unmodifiableMap( properties
);
200 * Creates a new artifact with the specified coordinates, properties and file. Passing {@code null} for any of the
201 * coordinates is equivalent to specifying an empty string.
203 * @param groupId The group identifier of the artifact, may be {@code null}.
204 * @param artifactId The artifact identifier of the artifact, may be {@code null}.
205 * @param classifier The classifier of the artifact, may be {@code null}.
206 * @param extension The file extension of the artifact, may be {@code null}.
207 * @param version The version of the artifact, may be {@code null}.
208 * @param properties The properties of the artifact, may be {@code null} if none.
209 * @param file The resolved file of the artifact, may be {@code null}.
211 public DefaultArtifact( String groupId
, String artifactId
, String classifier
, String extension
, String version
,
212 Map
<String
, String
> properties
, File file
)
214 this.groupId
= emptify( groupId
);
215 this.artifactId
= emptify( artifactId
);
216 this.classifier
= emptify( classifier
);
217 this.extension
= emptify( extension
);
218 this.version
= emptify( version
);
220 this.properties
= copyProperties( properties
);
223 DefaultArtifact( String groupId
, String artifactId
, String classifier
, String extension
, String version
, File file
,
224 Map
<String
, String
> properties
)
226 // NOTE: This constructor assumes immutability of the provided properties, for internal use only
227 this.groupId
= emptify( groupId
);
228 this.artifactId
= emptify( artifactId
);
229 this.classifier
= emptify( classifier
);
230 this.extension
= emptify( extension
);
231 this.version
= emptify( version
);
233 this.properties
= properties
;
236 private static String
emptify( String str
)
238 return ( str
== null ) ?
"" : str
;
241 public String
getGroupId()
246 public String
getArtifactId()
251 public String
getVersion()
256 public String
getClassifier()
261 public String
getExtension()
266 public File
getFile()
271 public Map
<String
, String
> getProperties()