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 Notifier
29 implements Serializable, InputLocationTracker
30 {
31
32
33
34 final String type;
35
36
37
38 final boolean sendOnError;
39
40
41
42 final boolean sendOnFailure;
43
44
45
46 final boolean sendOnSuccess;
47
48
49
50 final boolean sendOnWarning;
51
52
53
54 final String address;
55
56
57
58 final Map<String, String> configuration;
59
60 final Map<Object, InputLocation> locations;
61
62 final InputLocation importedFrom;
63
64
65
66
67
68 protected Notifier(Builder builder) {
69 this.type = builder.type != null ? builder.type : (builder.base != null ? builder.base.type : null);
70 this.sendOnError = builder.sendOnError != null ? builder.sendOnError : (builder.base != null ? builder.base.sendOnError : true);
71 this.sendOnFailure = builder.sendOnFailure != null ? builder.sendOnFailure : (builder.base != null ? builder.base.sendOnFailure : true);
72 this.sendOnSuccess = builder.sendOnSuccess != null ? builder.sendOnSuccess : (builder.base != null ? builder.base.sendOnSuccess : true);
73 this.sendOnWarning = builder.sendOnWarning != null ? builder.sendOnWarning : (builder.base != null ? builder.base.sendOnWarning : true);
74 this.address = builder.address != null ? builder.address : (builder.base != null ? builder.base.address : null);
75 this.configuration = ImmutableCollections.copy(builder.configuration != null ? builder.configuration : (builder.base != null ? builder.base.configuration : null));
76 this.locations = builder.computeLocations();
77 this.importedFrom = builder.importedFrom;
78 }
79
80
81
82
83
84
85 public String getType() {
86 return this.type;
87 }
88
89
90
91
92
93
94 public boolean isSendOnError() {
95 return this.sendOnError;
96 }
97
98
99
100
101
102
103 public boolean isSendOnFailure() {
104 return this.sendOnFailure;
105 }
106
107
108
109
110
111
112 public boolean isSendOnSuccess() {
113 return this.sendOnSuccess;
114 }
115
116
117
118
119
120
121 public boolean isSendOnWarning() {
122 return this.sendOnWarning;
123 }
124
125
126
127
128
129
130 public String getAddress() {
131 return this.address;
132 }
133
134
135
136
137
138
139 @Nonnull
140 public Map<String, String> getConfiguration() {
141 return this.configuration;
142 }
143
144
145
146
147 public InputLocation getLocation(Object key) {
148 return locations.get(key);
149 }
150
151
152
153
154 public Set<Object> getLocationKeys() {
155 return locations.keySet();
156 }
157
158 protected Stream<Object> getLocationKeyStream() {
159 return locations.keySet().stream();
160 }
161
162
163
164
165 public InputLocation getImportedFrom()
166 {
167 return importedFrom;
168 }
169
170
171
172
173
174
175 @Nonnull
176 public Builder with() {
177 return newBuilder(this);
178 }
179
180
181
182
183
184
185 @Nonnull
186 public Notifier withType(String type) {
187 return newBuilder(this, true).type(type).build();
188 }
189
190
191
192
193
194
195 @Nonnull
196 public Notifier withSendOnError(boolean sendOnError) {
197 return newBuilder(this, true).sendOnError(sendOnError).build();
198 }
199
200
201
202
203
204
205 @Nonnull
206 public Notifier withSendOnFailure(boolean sendOnFailure) {
207 return newBuilder(this, true).sendOnFailure(sendOnFailure).build();
208 }
209
210
211
212
213
214
215 @Nonnull
216 public Notifier withSendOnSuccess(boolean sendOnSuccess) {
217 return newBuilder(this, true).sendOnSuccess(sendOnSuccess).build();
218 }
219
220
221
222
223
224
225 @Nonnull
226 public Notifier withSendOnWarning(boolean sendOnWarning) {
227 return newBuilder(this, true).sendOnWarning(sendOnWarning).build();
228 }
229
230
231
232
233
234
235 @Nonnull
236 public Notifier withAddress(String address) {
237 return newBuilder(this, true).address(address).build();
238 }
239
240
241
242
243
244
245 @Nonnull
246 public Notifier withConfiguration(Map<String, String> configuration) {
247 return newBuilder(this, true).configuration(configuration).build();
248 }
249
250
251
252
253
254
255
256
257 @Nonnull
258 public static Notifier newInstance() {
259 return newInstance(true);
260 }
261
262
263
264
265
266
267
268
269 @Nonnull
270 public static Notifier newInstance(boolean withDefaults) {
271 return newBuilder(withDefaults).build();
272 }
273
274
275
276
277
278
279
280
281 @Nonnull
282 public static Builder newBuilder() {
283 return newBuilder(true);
284 }
285
286
287
288
289
290
291
292 @Nonnull
293 public static Builder newBuilder(boolean withDefaults) {
294 return new Builder(withDefaults);
295 }
296
297
298
299
300
301
302
303
304 @Nonnull
305 public static Builder newBuilder(Notifier from) {
306 return newBuilder(from, false);
307 }
308
309
310
311
312
313
314
315
316 @Nonnull
317 public static Builder newBuilder(Notifier from, boolean forceCopy) {
318 return new Builder(from, forceCopy);
319 }
320
321
322
323
324
325
326 @NotThreadSafe
327 public static class Builder
328 {
329 Notifier base;
330 String type;
331 Boolean sendOnError;
332 Boolean sendOnFailure;
333 Boolean sendOnSuccess;
334 Boolean sendOnWarning;
335 String address;
336 Map<String, String> configuration;
337 Map<Object, InputLocation> locations;
338 InputLocation importedFrom;
339
340 protected Builder(boolean withDefaults) {
341 if (withDefaults) {
342 this.type = "mail";
343 this.sendOnError = true;
344 this.sendOnFailure = true;
345 this.sendOnSuccess = true;
346 this.sendOnWarning = true;
347 }
348 }
349
350 protected Builder(Notifier base, boolean forceCopy) {
351 if (forceCopy) {
352 this.type = base.type;
353 this.sendOnError = base.sendOnError;
354 this.sendOnFailure = base.sendOnFailure;
355 this.sendOnSuccess = base.sendOnSuccess;
356 this.sendOnWarning = base.sendOnWarning;
357 this.address = base.address;
358 this.configuration = base.configuration;
359 this.locations = base.locations;
360 this.importedFrom = base.importedFrom;
361 } else {
362 this.base = base;
363 }
364 }
365
366 @Nonnull
367 public Builder type(String type) {
368 this.type = type;
369 return this;
370 }
371
372 @Nonnull
373 public Builder sendOnError(boolean sendOnError) {
374 this.sendOnError = sendOnError;
375 return this;
376 }
377
378 @Nonnull
379 public Builder sendOnFailure(boolean sendOnFailure) {
380 this.sendOnFailure = sendOnFailure;
381 return this;
382 }
383
384 @Nonnull
385 public Builder sendOnSuccess(boolean sendOnSuccess) {
386 this.sendOnSuccess = sendOnSuccess;
387 return this;
388 }
389
390 @Nonnull
391 public Builder sendOnWarning(boolean sendOnWarning) {
392 this.sendOnWarning = sendOnWarning;
393 return this;
394 }
395
396 @Nonnull
397 public Builder address(String address) {
398 this.address = address;
399 return this;
400 }
401
402 @Nonnull
403 public Builder configuration(Map<String, String> configuration) {
404 this.configuration = configuration;
405 return this;
406 }
407
408
409 @Nonnull
410 public Builder location(Object key, InputLocation location) {
411 if (location != null) {
412 if (!(this.locations instanceof HashMap)) {
413 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
414 }
415 this.locations.put(key, location);
416 }
417 return this;
418 }
419
420 @Nonnull
421 public Builder importedFrom(InputLocation importedFrom) {
422 this.importedFrom = importedFrom;
423 return this;
424 }
425
426 @Nonnull
427 public Notifier build() {
428
429 if (base != null
430 && (type == null || type == base.type)
431 && (sendOnError == null || sendOnError == base.sendOnError)
432 && (sendOnFailure == null || sendOnFailure == base.sendOnFailure)
433 && (sendOnSuccess == null || sendOnSuccess == base.sendOnSuccess)
434 && (sendOnWarning == null || sendOnWarning == base.sendOnWarning)
435 && (address == null || address == base.address)
436 && (configuration == null || configuration == base.configuration)
437 ) {
438 return base;
439 }
440 return new Notifier(this);
441 }
442
443 Map<Object, InputLocation> computeLocations() {
444 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
445 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
446 if (newlocs.isEmpty()) {
447 return Map.copyOf(oldlocs);
448 }
449 if (oldlocs.isEmpty()) {
450 return Map.copyOf(newlocs);
451 }
452 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
453
454 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
455 }
456 }
457
458 }