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