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