1 package org.eclipse.aether.artifact;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Objects;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30
31
32
33 public abstract class AbstractArtifact
34 implements Artifact
35 {
36
37 private static final String SNAPSHOT = "SNAPSHOT";
38
39 private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile( "^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$" );
40
41 public boolean isSnapshot()
42 {
43 return isSnapshot( getVersion() );
44 }
45
46 private static boolean isSnapshot( String version )
47 {
48 return version.endsWith( SNAPSHOT ) || SNAPSHOT_TIMESTAMP.matcher( version ).matches();
49 }
50
51 public String getBaseVersion()
52 {
53 return toBaseVersion( getVersion() );
54 }
55
56 private static String toBaseVersion( String version )
57 {
58 String baseVersion;
59
60 if ( version == null )
61 {
62 baseVersion = version;
63 }
64 else if ( version.startsWith( "[" ) || version.startsWith( "(" ) )
65 {
66 baseVersion = version;
67 }
68 else
69 {
70 Matcher m = SNAPSHOT_TIMESTAMP.matcher( version );
71 if ( m.matches() )
72 {
73 if ( m.group( 1 ) != null )
74 {
75 baseVersion = m.group( 1 ) + SNAPSHOT;
76 }
77 else
78 {
79 baseVersion = SNAPSHOT;
80 }
81 }
82 else
83 {
84 baseVersion = version;
85 }
86 }
87
88 return baseVersion;
89 }
90
91
92
93
94
95
96
97
98
99
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
144
145
146
147
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
178
179
180
181
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
208
209
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 }