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 {
148 return importedFrom;
149 }
150
151
152
153
154
155
156 @Nonnull
157 public Builder with() {
158 return newBuilder(this);
159 }
160
161
162
163
164
165
166 @Nonnull
167 public RepositoryBase withId(String id) {
168 return newBuilder(this, true).id(id).build();
169 }
170
171
172
173
174
175
176 @Nonnull
177 public RepositoryBase withName(String name) {
178 return newBuilder(this, true).name(name).build();
179 }
180
181
182
183
184
185
186 @Nonnull
187 public RepositoryBase withUrl(String url) {
188 return newBuilder(this, true).url(url).build();
189 }
190
191
192
193
194
195
196 @Nonnull
197 public RepositoryBase withLayout(String layout) {
198 return newBuilder(this, true).layout(layout).build();
199 }
200
201
202
203
204
205
206
207
208 @Nonnull
209 public static RepositoryBase newInstance() {
210 return newInstance(true);
211 }
212
213
214
215
216
217
218
219
220 @Nonnull
221 public static RepositoryBase newInstance(boolean withDefaults) {
222 return newBuilder(withDefaults).build();
223 }
224
225
226
227
228
229
230
231
232 @Nonnull
233 public static Builder newBuilder() {
234 return newBuilder(true);
235 }
236
237
238
239
240
241
242
243 @Nonnull
244 public static Builder newBuilder(boolean withDefaults) {
245 return new Builder(withDefaults);
246 }
247
248
249
250
251
252
253
254
255 @Nonnull
256 public static Builder newBuilder(RepositoryBase from) {
257 return newBuilder(from, false);
258 }
259
260
261
262
263
264
265
266
267 @Nonnull
268 public static Builder newBuilder(RepositoryBase from, boolean forceCopy) {
269 return new Builder(from, forceCopy);
270 }
271
272
273
274
275
276
277 @NotThreadSafe
278 public static class Builder
279 {
280 RepositoryBase base;
281 String id;
282 String name;
283 String url;
284 String layout;
285 Map<Object, InputLocation> locations;
286 InputLocation importedFrom;
287
288 protected Builder(boolean withDefaults) {
289 if (withDefaults) {
290 this.layout = "default";
291 }
292 }
293
294 protected Builder(RepositoryBase base, boolean forceCopy) {
295 if (forceCopy) {
296 this.id = base.id;
297 this.name = base.name;
298 this.url = base.url;
299 this.layout = base.layout;
300 this.locations = base.locations;
301 this.importedFrom = base.importedFrom;
302 } else {
303 this.base = base;
304 }
305 }
306
307 @Nonnull
308 public Builder id(String id) {
309 this.id = id;
310 return this;
311 }
312
313 @Nonnull
314 public Builder name(String name) {
315 this.name = name;
316 return this;
317 }
318
319 @Nonnull
320 public Builder url(String url) {
321 this.url = url;
322 return this;
323 }
324
325 @Nonnull
326 public Builder layout(String layout) {
327 this.layout = layout;
328 return this;
329 }
330
331
332 @Nonnull
333 public Builder location(Object key, InputLocation location) {
334 if (location != null) {
335 if (!(this.locations instanceof HashMap)) {
336 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
337 }
338 this.locations.put(key, location);
339 }
340 return this;
341 }
342
343 @Nonnull
344 public Builder importedFrom(InputLocation importedFrom) {
345 this.importedFrom = importedFrom;
346 return this;
347 }
348
349 @Nonnull
350 public RepositoryBase build() {
351
352 if (base != null
353 && (id == null || id == base.id)
354 && (name == null || name == base.name)
355 && (url == null || url == base.url)
356 && (layout == null || layout == base.layout)
357 ) {
358 return base;
359 }
360 return new RepositoryBase(this);
361 }
362
363 Map<Object, InputLocation> computeLocations() {
364 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
365 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
366 if (newlocs.isEmpty()) {
367 return Map.copyOf(oldlocs);
368 }
369 if (oldlocs.isEmpty()) {
370 return Map.copyOf(newlocs);
371 }
372 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
373
374 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
375 }
376 }
377
378 }