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      * @param key the key of the field, must not be {@code null}
106      * @return the location of the field in the input source or {@code null} if unknown
107      * @throws NullPointerException if {@code key} is {@code null}
108      */
109     public InputLocation getLocation(Object key) {
110         Objects.requireNonNull(key, "key");
111         return locations.get(key);
112     }
113 
114     /**
115      * Gets the keys of the locations of the input source.
116      */
117     public Set<Object> getLocationKeys() {
118         return locations.keySet();
119     }
120 
121     protected Stream<Object> getLocationKeyStream() {
122         return locations.keySet().stream();
123     }
124 
125     /**
126      * Gets the input location that caused this model to be read.
127      */
128     public InputLocation getImportedFrom() {
129         return importedFrom;
130     }
131 
132     /**
133      * Creates a new builder with this object as the basis.
134      *
135      * @return a {@code Builder}
136      */
137     @Nonnull
138     public Builder with() {
139         return newBuilder(this);
140     }
141     /**
142      * Creates a new {@code Relocation} instance using the specified groupId.
143      *
144      * @param groupId the new {@code String} to use
145      * @return a {@code Relocation} with the specified groupId
146      */
147     @Nonnull
148     public Relocation withGroupId(String groupId) {
149         return newBuilder(this, true).groupId(groupId).build();
150     }
151     /**
152      * Creates a new {@code Relocation} instance using the specified artifactId.
153      *
154      * @param artifactId the new {@code String} to use
155      * @return a {@code Relocation} with the specified artifactId
156      */
157     @Nonnull
158     public Relocation withArtifactId(String artifactId) {
159         return newBuilder(this, true).artifactId(artifactId).build();
160     }
161     /**
162      * Creates a new {@code Relocation} instance using the specified version.
163      *
164      * @param version the new {@code String} to use
165      * @return a {@code Relocation} with the specified version
166      */
167     @Nonnull
168     public Relocation withVersion(String version) {
169         return newBuilder(this, true).version(version).build();
170     }
171     /**
172      * Creates a new {@code Relocation} instance using the specified message.
173      *
174      * @param message the new {@code String} to use
175      * @return a {@code Relocation} with the specified message
176      */
177     @Nonnull
178     public Relocation withMessage(String message) {
179         return newBuilder(this, true).message(message).build();
180     }
181 
182     /**
183      * Creates a new {@code Relocation} instance.
184      * Equivalent to {@code newInstance(true)}.
185      * @see #newInstance(boolean)
186      *
187      * @return a new {@code Relocation}
188      */
189     @Nonnull
190     public static Relocation newInstance() {
191         return newInstance(true);
192     }
193 
194     /**
195      * Creates a new {@code Relocation} instance using default values or not.
196      * Equivalent to {@code newBuilder(withDefaults).build()}.
197      *
198      * @param withDefaults the boolean indicating whether default values should be used
199      * @return a new {@code Relocation}
200      */
201     @Nonnull
202     public static Relocation newInstance(boolean withDefaults) {
203         return newBuilder(withDefaults).build();
204     }
205 
206     /**
207      * Creates a new {@code Relocation} builder instance.
208      * Equivalent to {@code newBuilder(true)}.
209      * @see #newBuilder(boolean)
210      *
211      * @return a new {@code Builder}
212      */
213     @Nonnull
214     public static Builder newBuilder() {
215         return newBuilder(true);
216     }
217 
218     /**
219      * Creates a new {@code Relocation} builder instance using default values or not.
220      *
221      * @param withDefaults the boolean indicating whether default values should be used
222      * @return a new {@code Builder}
223      */
224     @Nonnull
225     public static Builder newBuilder(boolean withDefaults) {
226         return new Builder(withDefaults);
227     }
228 
229     /**
230      * Creates a new {@code Relocation} builder instance using the specified object as a basis.
231      * Equivalent to {@code newBuilder(from, false)}.
232      *
233      * @param from the {@code Relocation} instance to use as a basis
234      * @return a new {@code Builder}
235      */
236     @Nonnull
237     public static Builder newBuilder(Relocation from) {
238         return newBuilder(from, false);
239     }
240 
241     /**
242      * Creates a new {@code Relocation} builder instance using the specified object as a basis.
243      *
244      * @param from the {@code Relocation} instance to use as a basis
245      * @param forceCopy the boolean indicating if a copy should be forced
246      * @return a new {@code Builder}
247      */
248     @Nonnull
249     public static Builder newBuilder(Relocation from, boolean forceCopy) {
250         return new Builder(from, forceCopy);
251     }
252 
253     /**
254      * Builder class used to create Relocation instances.
255      * @see #with()
256      * @see #newBuilder()
257      */
258     @NotThreadSafe
259     public static class Builder
260     {
261         Relocation base;
262         String groupId;
263         String artifactId;
264         String version;
265         String message;
266         Map<Object, InputLocation> locations;
267         InputLocation importedFrom;
268 
269         protected Builder(boolean withDefaults) {
270             if (withDefaults) {
271             }
272         }
273 
274         protected Builder(Relocation base, boolean forceCopy) {
275             if (forceCopy) {
276                 this.groupId = base.groupId;
277                 this.artifactId = base.artifactId;
278                 this.version = base.version;
279                 this.message = base.message;
280                 this.locations = base.locations;
281                 this.importedFrom = base.importedFrom;
282             } else {
283                 this.base = base;
284             }
285         }
286 
287         @Nonnull
288         public Builder groupId(String groupId) {
289             this.groupId = groupId;
290             return this;
291         }
292 
293         @Nonnull
294         public Builder artifactId(String artifactId) {
295             this.artifactId = artifactId;
296             return this;
297         }
298 
299         @Nonnull
300         public Builder version(String version) {
301             this.version = version;
302             return this;
303         }
304 
305         @Nonnull
306         public Builder message(String message) {
307             this.message = message;
308             return this;
309         }
310 
311 
312         @Nonnull
313         public Builder location(Object key, InputLocation location) {
314             if (location != null) {
315                 if (!(this.locations instanceof HashMap)) {
316                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
317                 }
318                 this.locations.put(key, location);
319             }
320             return this;
321         }
322 
323         @Nonnull
324         public Builder importedFrom(InputLocation importedFrom) {
325             this.importedFrom = importedFrom;
326             return this;
327         }
328 
329         @Nonnull
330         public Relocation build() {
331             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
332             if (base != null
333                     && (groupId == null || groupId == base.groupId)
334                     && (artifactId == null || artifactId == base.artifactId)
335                     && (version == null || version == base.version)
336                     && (message == null || message == base.message)
337             ) {
338                 return base;
339             }
340             return new Relocation(this);
341         }
342 
343         Map<Object, InputLocation> computeLocations() {
344             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
345             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
346             if (newlocs.isEmpty()) {
347                 return Map.copyOf(oldlocs);
348             }
349             if (oldlocs.isEmpty()) {
350                 return Map.copyOf(newlocs);
351             }
352             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
353                     // Keep value from newlocs in case of duplicates
354                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
355         }
356     }
357 
358 }