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