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   * Describes where an artifact has moved to. If any of the values are omitted, it is
25   * assumed to be the same as it was before.
26   */
27  @Experimental
28  @Generated @ThreadSafe @Immutable
29  public class Relocation
30      implements Serializable, InputLocationTracker
31  {
32      /**
33       * The group ID the artifact has moved to.
34       */
35      final String groupId;
36      /**
37       * The new artifact ID of the artifact.
38       */
39      final String artifactId;
40      /**
41       * The new version of the artifact.
42       */
43      final String version;
44      /**
45       * An additional message to show the user about the move, such as the reason.
46       */
47      final String message;
48      /** Locations */
49      final Map<Object, InputLocation> locations;
50      /** Location tracking */
51      final InputLocation importedFrom;
52  
53      /**
54        * Constructor for this class, to be called from its subclasses and {@link Builder}.
55        * @see Builder#build()
56        */
57      protected Relocation(Builder builder) {
58          this.groupId = builder.groupId != null ? builder.groupId : (builder.base != null ? builder.base.groupId : null);
59          this.artifactId = builder.artifactId != null ? builder.artifactId : (builder.base != null ? builder.base.artifactId : null);
60          this.version = builder.version != null ? builder.version : (builder.base != null ? builder.base.version : null);
61          this.message = builder.message != null ? builder.message : (builder.base != null ? builder.base.message : null);
62          this.locations = builder.computeLocations();
63          this.importedFrom = builder.importedFrom;
64      }
65  
66      /**
67       * The group ID the artifact has moved to.
68       *
69       * @return a {@code String}
70       */
71      public String getGroupId() {
72          return this.groupId;
73      }
74  
75      /**
76       * The new artifact ID of the artifact.
77       *
78       * @return a {@code String}
79       */
80      public String getArtifactId() {
81          return this.artifactId;
82      }
83  
84      /**
85       * The new version of the artifact.
86       *
87       * @return a {@code String}
88       */
89      public String getVersion() {
90          return this.version;
91      }
92  
93      /**
94       * An additional message to show the user about the move, such as the reason.
95       *
96       * @return a {@code String}
97       */
98      public String getMessage() {
99          return this.message;
100     }
101 
102     /**
103      * Gets the location of the specified field in the input source.
104      */
105     public InputLocation getLocation(Object key) {
106         return locations.get(key);
107     }
108 
109     /**
110      * Gets the keys of the locations of the input source.
111      */
112     public Set<Object> getLocationKeys() {
113         return locations.keySet();
114     }
115 
116     protected Stream<Object> getLocationKeyStream() {
117         return locations.keySet().stream();
118     }
119 
120     /**
121      * Gets the input location that caused this model to be read.
122      */
123     public InputLocation getImportedFrom() {
124         return importedFrom;
125     }
126 
127     /**
128      * Creates a new builder with this object as the basis.
129      *
130      * @return a {@code Builder}
131      */
132     @Nonnull
133     public Builder with() {
134         return newBuilder(this);
135     }
136     /**
137      * Creates a new {@code Relocation} instance using the specified groupId.
138      *
139      * @param groupId the new {@code String} to use
140      * @return a {@code Relocation} with the specified groupId
141      */
142     @Nonnull
143     public Relocation withGroupId(String groupId) {
144         return newBuilder(this, true).groupId(groupId).build();
145     }
146     /**
147      * Creates a new {@code Relocation} instance using the specified artifactId.
148      *
149      * @param artifactId the new {@code String} to use
150      * @return a {@code Relocation} with the specified artifactId
151      */
152     @Nonnull
153     public Relocation withArtifactId(String artifactId) {
154         return newBuilder(this, true).artifactId(artifactId).build();
155     }
156     /**
157      * Creates a new {@code Relocation} instance using the specified version.
158      *
159      * @param version the new {@code String} to use
160      * @return a {@code Relocation} with the specified version
161      */
162     @Nonnull
163     public Relocation withVersion(String version) {
164         return newBuilder(this, true).version(version).build();
165     }
166     /**
167      * Creates a new {@code Relocation} instance using the specified message.
168      *
169      * @param message the new {@code String} to use
170      * @return a {@code Relocation} with the specified message
171      */
172     @Nonnull
173     public Relocation withMessage(String message) {
174         return newBuilder(this, true).message(message).build();
175     }
176 
177     /**
178      * Creates a new {@code Relocation} instance.
179      * Equivalent to {@code newInstance(true)}.
180      * @see #newInstance(boolean)
181      *
182      * @return a new {@code Relocation}
183      */
184     @Nonnull
185     public static Relocation newInstance() {
186         return newInstance(true);
187     }
188 
189     /**
190      * Creates a new {@code Relocation} instance using default values or not.
191      * Equivalent to {@code newBuilder(withDefaults).build()}.
192      *
193      * @param withDefaults the boolean indicating whether default values should be used
194      * @return a new {@code Relocation}
195      */
196     @Nonnull
197     public static Relocation newInstance(boolean withDefaults) {
198         return newBuilder(withDefaults).build();
199     }
200 
201     /**
202      * Creates a new {@code Relocation} builder instance.
203      * Equivalent to {@code newBuilder(true)}.
204      * @see #newBuilder(boolean)
205      *
206      * @return a new {@code Builder}
207      */
208     @Nonnull
209     public static Builder newBuilder() {
210         return newBuilder(true);
211     }
212 
213     /**
214      * Creates a new {@code Relocation} builder instance using default values or not.
215      *
216      * @param withDefaults the boolean indicating whether default values should be used
217      * @return a new {@code Builder}
218      */
219     @Nonnull
220     public static Builder newBuilder(boolean withDefaults) {
221         return new Builder(withDefaults);
222     }
223 
224     /**
225      * Creates a new {@code Relocation} builder instance using the specified object as a basis.
226      * Equivalent to {@code newBuilder(from, false)}.
227      *
228      * @param from the {@code Relocation} instance to use as a basis
229      * @return a new {@code Builder}
230      */
231     @Nonnull
232     public static Builder newBuilder(Relocation from) {
233         return newBuilder(from, false);
234     }
235 
236     /**
237      * Creates a new {@code Relocation} builder instance using the specified object as a basis.
238      *
239      * @param from the {@code Relocation} instance to use as a basis
240      * @param forceCopy the boolean indicating if a copy should be forced
241      * @return a new {@code Builder}
242      */
243     @Nonnull
244     public static Builder newBuilder(Relocation from, boolean forceCopy) {
245         return new Builder(from, forceCopy);
246     }
247 
248     /**
249      * Builder class used to create Relocation instances.
250      * @see #with()
251      * @see #newBuilder()
252      */
253     @NotThreadSafe
254     public static class Builder
255     {
256         Relocation base;
257         String groupId;
258         String artifactId;
259         String version;
260         String message;
261         Map<Object, InputLocation> locations;
262         InputLocation importedFrom;
263 
264         protected Builder(boolean withDefaults) {
265             if (withDefaults) {
266             }
267         }
268 
269         protected Builder(Relocation base, boolean forceCopy) {
270             if (forceCopy) {
271                 this.groupId = base.groupId;
272                 this.artifactId = base.artifactId;
273                 this.version = base.version;
274                 this.message = base.message;
275                 this.locations = base.locations;
276                 this.importedFrom = base.importedFrom;
277             } else {
278                 this.base = base;
279             }
280         }
281 
282         @Nonnull
283         public Builder groupId(String groupId) {
284             this.groupId = groupId;
285             return this;
286         }
287 
288         @Nonnull
289         public Builder artifactId(String artifactId) {
290             this.artifactId = artifactId;
291             return this;
292         }
293 
294         @Nonnull
295         public Builder version(String version) {
296             this.version = version;
297             return this;
298         }
299 
300         @Nonnull
301         public Builder message(String message) {
302             this.message = message;
303             return this;
304         }
305 
306 
307         @Nonnull
308         public Builder location(Object key, InputLocation location) {
309             if (location != null) {
310                 if (!(this.locations instanceof HashMap)) {
311                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
312                 }
313                 this.locations.put(key, location);
314             }
315             return this;
316         }
317 
318         @Nonnull
319         public Builder importedFrom(InputLocation importedFrom) {
320             this.importedFrom = importedFrom;
321             return this;
322         }
323 
324         @Nonnull
325         public Relocation build() {
326             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
327             if (base != null
328                     && (groupId == null || groupId == base.groupId)
329                     && (artifactId == null || artifactId == base.artifactId)
330                     && (version == null || version == base.version)
331                     && (message == null || message == base.message)
332             ) {
333                 return base;
334             }
335             return new Relocation(this);
336         }
337 
338         Map<Object, InputLocation> computeLocations() {
339             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
340             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
341             if (newlocs.isEmpty()) {
342                 return Map.copyOf(oldlocs);
343             }
344             if (oldlocs.isEmpty()) {
345                 return Map.copyOf(newlocs);
346             }
347             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
348                     // Keep value from newlocs in case of duplicates
349                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
350         }
351     }
352 
353 }