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