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