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