1
2
3
4
5 package org.apache.maven.api.model;
6
7 import java.io.Serializable;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.Map;
11 import java.util.Objects;
12 import java.util.Optional;
13 import java.util.Set;
14 import java.util.stream.Collectors;
15 import java.util.stream.Stream;
16 import org.apache.maven.api.annotations.Experimental;
17 import org.apache.maven.api.annotations.Generated;
18 import org.apache.maven.api.annotations.Immutable;
19 import org.apache.maven.api.annotations.Nonnull;
20 import org.apache.maven.api.annotations.NotThreadSafe;
21 import org.apache.maven.api.annotations.ThreadSafe;
22
23
24
25
26
27 @Experimental
28 @Generated @ThreadSafe @Immutable
29 public class Relocation
30 implements Serializable, InputLocationTracker
31 {
32
33
34
35 final String groupId;
36
37
38
39 final String artifactId;
40
41
42
43 final String version;
44
45
46
47 final String message;
48
49 final Map<Object, InputLocation> locations;
50
51 final InputLocation importedFrom;
52
53
54
55
56
57 protected Relocation(Builder builder) {
58 this.groupId = builder.groupId != null ? builder.groupId : (builder.base != null ? builder.base.groupId : null);
59 this.artifactId = builder.artifactId != null ? builder.artifactId : (builder.base != null ? builder.base.artifactId : null);
60 this.version = builder.version != null ? builder.version : (builder.base != null ? builder.base.version : null);
61 this.message = builder.message != null ? builder.message : (builder.base != null ? builder.base.message : null);
62 this.locations = builder.computeLocations();
63 this.importedFrom = builder.importedFrom;
64 }
65
66
67
68
69
70
71 public String getGroupId() {
72 return this.groupId;
73 }
74
75
76
77
78
79
80 public String getArtifactId() {
81 return this.artifactId;
82 }
83
84
85
86
87
88
89 public String getVersion() {
90 return this.version;
91 }
92
93
94
95
96
97
98 public String getMessage() {
99 return this.message;
100 }
101
102
103
104
105
106
107
108
109 public InputLocation getLocation(Object key) {
110 Objects.requireNonNull(key, "key");
111 return locations.get(key);
112 }
113
114
115
116
117 public Set<Object> getLocationKeys() {
118 return locations.keySet();
119 }
120
121 protected Stream<Object> getLocationKeyStream() {
122 return locations.keySet().stream();
123 }
124
125
126
127
128 public InputLocation getImportedFrom() {
129 return importedFrom;
130 }
131
132
133
134
135
136
137 @Nonnull
138 public Builder with() {
139 return newBuilder(this);
140 }
141
142
143
144
145
146
147 @Nonnull
148 public Relocation withGroupId(String groupId) {
149 return newBuilder(this, true).groupId(groupId).build();
150 }
151
152
153
154
155
156
157 @Nonnull
158 public Relocation withArtifactId(String artifactId) {
159 return newBuilder(this, true).artifactId(artifactId).build();
160 }
161
162
163
164
165
166
167 @Nonnull
168 public Relocation withVersion(String version) {
169 return newBuilder(this, true).version(version).build();
170 }
171
172
173
174
175
176
177 @Nonnull
178 public Relocation withMessage(String message) {
179 return newBuilder(this, true).message(message).build();
180 }
181
182
183
184
185
186
187
188
189 @Nonnull
190 public static Relocation newInstance() {
191 return newInstance(true);
192 }
193
194
195
196
197
198
199
200
201 @Nonnull
202 public static Relocation newInstance(boolean withDefaults) {
203 return newBuilder(withDefaults).build();
204 }
205
206
207
208
209
210
211
212
213 @Nonnull
214 public static Builder newBuilder() {
215 return newBuilder(true);
216 }
217
218
219
220
221
222
223
224 @Nonnull
225 public static Builder newBuilder(boolean withDefaults) {
226 return new Builder(withDefaults);
227 }
228
229
230
231
232
233
234
235
236 @Nonnull
237 public static Builder newBuilder(Relocation from) {
238 return newBuilder(from, false);
239 }
240
241
242
243
244
245
246
247
248 @Nonnull
249 public static Builder newBuilder(Relocation from, boolean forceCopy) {
250 return new Builder(from, forceCopy);
251 }
252
253
254
255
256
257
258 @NotThreadSafe
259 public static class Builder
260 {
261 Relocation base;
262 String groupId;
263 String artifactId;
264 String version;
265 String message;
266 Map<Object, InputLocation> locations;
267 InputLocation importedFrom;
268
269 protected Builder(boolean withDefaults) {
270 if (withDefaults) {
271 }
272 }
273
274 protected Builder(Relocation base, boolean forceCopy) {
275 if (forceCopy) {
276 this.groupId = base.groupId;
277 this.artifactId = base.artifactId;
278 this.version = base.version;
279 this.message = base.message;
280 this.locations = base.locations;
281 this.importedFrom = base.importedFrom;
282 } else {
283 this.base = base;
284 }
285 }
286
287 @Nonnull
288 public Builder groupId(String groupId) {
289 this.groupId = groupId;
290 return this;
291 }
292
293 @Nonnull
294 public Builder artifactId(String artifactId) {
295 this.artifactId = artifactId;
296 return this;
297 }
298
299 @Nonnull
300 public Builder version(String version) {
301 this.version = version;
302 return this;
303 }
304
305 @Nonnull
306 public Builder message(String message) {
307 this.message = message;
308 return this;
309 }
310
311
312 @Nonnull
313 public Builder location(Object key, InputLocation location) {
314 if (location != null) {
315 if (!(this.locations instanceof HashMap)) {
316 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
317 }
318 this.locations.put(key, location);
319 }
320 return this;
321 }
322
323 @Nonnull
324 public Builder importedFrom(InputLocation importedFrom) {
325 this.importedFrom = importedFrom;
326 return this;
327 }
328
329 @Nonnull
330 public Relocation build() {
331
332 if (base != null
333 && (groupId == null || groupId == base.groupId)
334 && (artifactId == null || artifactId == base.artifactId)
335 && (version == null || version == base.version)
336 && (message == null || message == base.message)
337 ) {
338 return base;
339 }
340 return new Relocation(this);
341 }
342
343 Map<Object, InputLocation> computeLocations() {
344 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
345 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
346 if (newlocs.isEmpty()) {
347 return Map.copyOf(oldlocs);
348 }
349 if (oldlocs.isEmpty()) {
350 return Map.copyOf(newlocs);
351 }
352 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
353
354 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
355 }
356 }
357
358 }