View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //  Generated by Modello Velocity from model.vm
3   //  template, any modifications will be overwritten.
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   * Configures one method for notifying users/developers when a build breaks.
25   */
26  @Experimental
27  @Generated @ThreadSafe @Immutable
28  public class Notifier
29      implements Serializable, InputLocationTracker
30  {
31      /**
32       * The mechanism used to deliver notifications.
33       */
34      final String type;
35      /**
36       * Whether to send notifications on error.
37       */
38      final boolean sendOnError;
39      /**
40       * Whether to send notifications on failure.
41       */
42      final boolean sendOnFailure;
43      /**
44       * Whether to send notifications on success.
45       */
46      final boolean sendOnSuccess;
47      /**
48       * Whether to send notifications on warning.
49       */
50      final boolean sendOnWarning;
51      /**
52       * @deprecated Where to send the notification to - eg email address.
53       */
54      final String address;
55      /**
56       * Extended configuration specific to this notifier goes here.
57       */
58      final Map<String, String> configuration;
59      /** Locations */
60      final Map<Object, InputLocation> locations;
61      /** Location tracking */
62      final InputLocation importedFrom;
63  
64      /**
65        * Constructor for this class, to be called from its subclasses and {@link Builder}.
66        * @see Builder#build()
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       * The mechanism used to deliver notifications.
82       *
83       * @return a {@code String}
84       */
85      public String getType() {
86          return this.type;
87      }
88  
89      /**
90       * Whether to send notifications on error.
91       *
92       * @return a {@code boolean}
93       */
94      public boolean isSendOnError() {
95          return this.sendOnError;
96      }
97  
98      /**
99       * Whether to send notifications on failure.
100      *
101      * @return a {@code boolean}
102      */
103     public boolean isSendOnFailure() {
104         return this.sendOnFailure;
105     }
106 
107     /**
108      * Whether to send notifications on success.
109      *
110      * @return a {@code boolean}
111      */
112     public boolean isSendOnSuccess() {
113         return this.sendOnSuccess;
114     }
115 
116     /**
117      * Whether to send notifications on warning.
118      *
119      * @return a {@code boolean}
120      */
121     public boolean isSendOnWarning() {
122         return this.sendOnWarning;
123     }
124 
125     /**
126      * @deprecated Where to send the notification to - eg email address.
127      *
128      * @return a {@code String}
129      */
130     public String getAddress() {
131         return this.address;
132     }
133 
134     /**
135      * Extended configuration specific to this notifier goes here.
136      *
137      * @return a {@code Map<String, String>}
138      */
139     @Nonnull
140     public Map<String, String> getConfiguration() {
141         return this.configuration;
142     }
143 
144     /**
145      * Gets the location of the specified field in the input source.
146      */
147     public InputLocation getLocation(Object key) {
148         return locations.get(key);
149     }
150 
151     /**
152      * Gets the keys of the locations of the input source.
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      * Gets the input location that caused this model to be read.
164      */
165     public InputLocation getImportedFrom()
166     {
167         return importedFrom;
168     }
169 
170     /**
171      * Creates a new builder with this object as the basis.
172      *
173      * @return a {@code Builder}
174      */
175     @Nonnull
176     public Builder with() {
177         return newBuilder(this);
178     }
179     /**
180      * Creates a new {@code Notifier} instance using the specified type.
181      *
182      * @param type the new {@code String} to use
183      * @return a {@code Notifier} with the specified type
184      */
185     @Nonnull
186     public Notifier withType(String type) {
187         return newBuilder(this, true).type(type).build();
188     }
189     /**
190      * Creates a new {@code Notifier} instance using the specified sendOnError.
191      *
192      * @param sendOnError the new {@code boolean} to use
193      * @return a {@code Notifier} with the specified sendOnError
194      */
195     @Nonnull
196     public Notifier withSendOnError(boolean sendOnError) {
197         return newBuilder(this, true).sendOnError(sendOnError).build();
198     }
199     /**
200      * Creates a new {@code Notifier} instance using the specified sendOnFailure.
201      *
202      * @param sendOnFailure the new {@code boolean} to use
203      * @return a {@code Notifier} with the specified sendOnFailure
204      */
205     @Nonnull
206     public Notifier withSendOnFailure(boolean sendOnFailure) {
207         return newBuilder(this, true).sendOnFailure(sendOnFailure).build();
208     }
209     /**
210      * Creates a new {@code Notifier} instance using the specified sendOnSuccess.
211      *
212      * @param sendOnSuccess the new {@code boolean} to use
213      * @return a {@code Notifier} with the specified sendOnSuccess
214      */
215     @Nonnull
216     public Notifier withSendOnSuccess(boolean sendOnSuccess) {
217         return newBuilder(this, true).sendOnSuccess(sendOnSuccess).build();
218     }
219     /**
220      * Creates a new {@code Notifier} instance using the specified sendOnWarning.
221      *
222      * @param sendOnWarning the new {@code boolean} to use
223      * @return a {@code Notifier} with the specified sendOnWarning
224      */
225     @Nonnull
226     public Notifier withSendOnWarning(boolean sendOnWarning) {
227         return newBuilder(this, true).sendOnWarning(sendOnWarning).build();
228     }
229     /**
230      * Creates a new {@code Notifier} instance using the specified address.
231      *
232      * @param address the new {@code String} to use
233      * @return a {@code Notifier} with the specified address
234      */
235     @Nonnull
236     public Notifier withAddress(String address) {
237         return newBuilder(this, true).address(address).build();
238     }
239     /**
240      * Creates a new {@code Notifier} instance using the specified configuration.
241      *
242      * @param configuration the new {@code Map<String, String>} to use
243      * @return a {@code Notifier} with the specified configuration
244      */
245     @Nonnull
246     public Notifier withConfiguration(Map<String, String> configuration) {
247         return newBuilder(this, true).configuration(configuration).build();
248     }
249 
250     /**
251      * Creates a new {@code Notifier} instance.
252      * Equivalent to {@code newInstance(true)}.
253      * @see #newInstance(boolean)
254      *
255      * @return a new {@code Notifier}
256      */
257     @Nonnull
258     public static Notifier newInstance() {
259         return newInstance(true);
260     }
261 
262     /**
263      * Creates a new {@code Notifier} instance using default values or not.
264      * Equivalent to {@code newBuilder(withDefaults).build()}.
265      *
266      * @param withDefaults the boolean indicating whether default values should be used
267      * @return a new {@code Notifier}
268      */
269     @Nonnull
270     public static Notifier newInstance(boolean withDefaults) {
271         return newBuilder(withDefaults).build();
272     }
273 
274     /**
275      * Creates a new {@code Notifier} builder instance.
276      * Equivalent to {@code newBuilder(true)}.
277      * @see #newBuilder(boolean)
278      *
279      * @return a new {@code Builder}
280      */
281     @Nonnull
282     public static Builder newBuilder() {
283         return newBuilder(true);
284     }
285 
286     /**
287      * Creates a new {@code Notifier} builder instance using default values or not.
288      *
289      * @param withDefaults the boolean indicating whether default values should be used
290      * @return a new {@code Builder}
291      */
292     @Nonnull
293     public static Builder newBuilder(boolean withDefaults) {
294         return new Builder(withDefaults);
295     }
296 
297     /**
298      * Creates a new {@code Notifier} builder instance using the specified object as a basis.
299      * Equivalent to {@code newBuilder(from, false)}.
300      *
301      * @param from the {@code Notifier} instance to use as a basis
302      * @return a new {@code Builder}
303      */
304     @Nonnull
305     public static Builder newBuilder(Notifier from) {
306         return newBuilder(from, false);
307     }
308 
309     /**
310      * Creates a new {@code Notifier} builder instance using the specified object as a basis.
311      *
312      * @param from the {@code Notifier} instance to use as a basis
313      * @param forceCopy the boolean indicating if a copy should be forced
314      * @return a new {@code Builder}
315      */
316     @Nonnull
317     public static Builder newBuilder(Notifier from, boolean forceCopy) {
318         return new Builder(from, forceCopy);
319     }
320 
321     /**
322      * Builder class used to create Notifier instances.
323      * @see #with()
324      * @see #newBuilder()
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             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
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                     // Keep value from newlocs in case of duplicates
454                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
455         }
456     }
457 
458 }