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