1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.artifact;
20
21 import java.io.File;
22 import java.nio.file.Path;
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 implements Artifact {
34
35 private static final String SNAPSHOT = "SNAPSHOT";
36
37 private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile("^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$");
38
39
40
41
42 public AbstractArtifact() {}
43
44 @Override
45 public boolean isSnapshot() {
46 return isSnapshot(getVersion());
47 }
48
49 private static boolean isSnapshot(String version) {
50 return version.endsWith(SNAPSHOT) || SNAPSHOT_TIMESTAMP.matcher(version).matches();
51 }
52
53 @Override
54 public String getBaseVersion() {
55 return toBaseVersion(getVersion());
56 }
57
58 private static String toBaseVersion(String version) {
59 String baseVersion;
60
61 if (version == null) {
62 baseVersion = null;
63 } else if (version.startsWith("[") || version.startsWith("(")) {
64 baseVersion = version;
65 } else {
66 Matcher m = SNAPSHOT_TIMESTAMP.matcher(version);
67 if (m.matches()) {
68 if (m.group(1) != null) {
69 baseVersion = m.group(1) + SNAPSHOT;
70 } else {
71 baseVersion = SNAPSHOT;
72 }
73 } else {
74 baseVersion = version;
75 }
76 }
77
78 return baseVersion;
79 }
80
81
82
83
84
85
86
87
88
89
90 private Artifact newInstance(String version, Map<String, String> properties, Path path) {
91 return new DefaultArtifact(
92 getGroupId(), getArtifactId(), getClassifier(), getExtension(), version, path, properties);
93 }
94
95 @Override
96 public Artifact setVersion(String version) {
97 String current = getVersion();
98 if (current.equals(version) || (version == null && current.isEmpty())) {
99 return this;
100 }
101 return newInstance(version, getProperties(), getPath());
102 }
103
104
105
106
107
108
109 @SuppressWarnings("deprecation")
110 public Path getPath() {
111 File file = getFile();
112 return file != null ? file.toPath() : null;
113 }
114
115 @Deprecated
116 @Override
117 public Artifact setFile(File file) {
118 return setPath(file != null ? file.toPath() : null);
119 }
120
121 @Override
122 public Artifact setPath(Path path) {
123 Path current = getPath();
124 if (Objects.equals(current, path)) {
125 return this;
126 }
127 return newInstance(getVersion(), getProperties(), path);
128 }
129
130 public Artifact setProperties(Map<String, String> properties) {
131 Map<String, String> current = getProperties();
132 if (current.equals(properties) || (properties == null && current.isEmpty())) {
133 return this;
134 }
135 return newInstance(getVersion(), copyProperties(properties), getPath());
136 }
137
138 public String getProperty(String key, String defaultValue) {
139 String value = getProperties().get(key);
140 return (value != null) ? value : defaultValue;
141 }
142
143
144
145
146
147
148
149
150 protected static Map<String, String> copyProperties(Map<String, String> properties) {
151 if (properties != null && !properties.isEmpty()) {
152 return Collections.unmodifiableMap(new HashMap<>(properties));
153 } else {
154 return Collections.emptyMap();
155 }
156 }
157
158 @Override
159 public String toString() {
160 StringBuilder buffer = new StringBuilder(128);
161 buffer.append(getGroupId());
162 buffer.append(':').append(getArtifactId());
163 buffer.append(':').append(getExtension());
164 if (!getClassifier().isEmpty()) {
165 buffer.append(':').append(getClassifier());
166 }
167 buffer.append(':').append(getVersion());
168 return buffer.toString();
169 }
170
171
172
173
174
175
176
177
178 @Override
179 public boolean equals(Object obj) {
180 if (obj == this) {
181 return true;
182 } else if (!(obj instanceof Artifact)) {
183 return false;
184 }
185
186 Artifact that = (Artifact) obj;
187
188 return Objects.equals(getArtifactId(), that.getArtifactId())
189 && Objects.equals(getGroupId(), that.getGroupId())
190 && Objects.equals(getVersion(), that.getVersion())
191 && Objects.equals(getExtension(), that.getExtension())
192 && Objects.equals(getClassifier(), that.getClassifier())
193 && Objects.equals(getPath(), that.getPath())
194 && Objects.equals(getProperties(), that.getProperties());
195 }
196
197
198
199
200
201
202 @Override
203 public int hashCode() {
204 int hash = 17;
205 hash = hash * 31 + getGroupId().hashCode();
206 hash = hash * 31 + getArtifactId().hashCode();
207 hash = hash * 31 + getExtension().hashCode();
208 hash = hash * 31 + getClassifier().hashCode();
209 hash = hash * 31 + getVersion().hashCode();
210 hash = hash * 31 + hash(getPath());
211 return hash;
212 }
213
214 private static int hash(Object obj) {
215 return (obj != null) ? obj.hashCode() : 0;
216 }
217 }