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