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.Set;
13  import org.apache.maven.api.annotations.Experimental;
14  import org.apache.maven.api.annotations.Generated;
15  import org.apache.maven.api.annotations.Immutable;
16  import org.apache.maven.api.annotations.Nonnull;
17  import org.apache.maven.api.annotations.NotThreadSafe;
18  import org.apache.maven.api.annotations.ThreadSafe;
19  
20  /**
21   * A repository contains the information needed for establishing connections with
22   * remote repository.
23   */
24  @Experimental
25  @Generated @ThreadSafe @Immutable
26  public class RepositoryBase
27      implements Serializable, InputLocationTracker
28  {
29      /**
30       * A unique identifier for a repository. This is used to match the repository
31       * to configuration in the {@code settings.xml} file, for example. Furthermore, the identifier is
32       * used during POM inheritance and profile injection to detect repositories that should be merged.
33       */
34      final String id;
35      /**
36       * Human readable name of the repository.
37       */
38      final String name;
39      /**
40       * The url of the repository, in the form {@code protocol://hostname/path}.
41       */
42      final String url;
43      /**
44       * The type of layout this repository uses for locating and storing artifacts -
45       * can be {@code legacy} or {@code default}.
46       */
47      final String layout;
48      /** Locations (this potentially hides the same name field from the super class) */
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 RepositoryBase(Builder builder) {
58          this.id = builder.id != null ? builder.id : (builder.base != null ? builder.base.id : null);
59          this.name = builder.name != null ? builder.name : (builder.base != null ? builder.base.name : null);
60          this.url = builder.url != null ? builder.url : (builder.base != null ? builder.base.url : null);
61          this.layout = builder.layout != null ? builder.layout : (builder.base != null ? builder.base.layout : null);
62          Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
63          Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
64          Map<Object, InputLocation> mutableLocations = new HashMap<>();
65          this.importedFrom = builder.importedFrom;
66          mutableLocations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
67          mutableLocations.put("id", newlocs.containsKey("id") ? newlocs.get("id") : oldlocs.get("id"));
68          mutableLocations.put("name", newlocs.containsKey("name") ? newlocs.get("name") : oldlocs.get("name"));
69          mutableLocations.put("url", newlocs.containsKey("url") ? newlocs.get("url") : oldlocs.get("url"));
70          mutableLocations.put("layout", newlocs.containsKey("layout") ? newlocs.get("layout") : oldlocs.get("layout"));
71          this.locations = Collections.unmodifiableMap(mutableLocations);
72      }
73  
74      @Override
75      public boolean equals(Object o) {
76          if (this == o) {
77              return true;
78          }
79          if (o == null || !(o instanceof RepositoryBase)) {
80              return false;
81          }
82          RepositoryBase that = (RepositoryBase) o;
83          return Objects.equals( this.id, that.id );
84      }
85  
86      @Override
87      public int hashCode() {
88          return Objects.hash(id);
89      }
90  
91      /**
92       * A unique identifier for a repository. This is used to match the repository
93       * to configuration in the {@code settings.xml} file, for example. Furthermore, the identifier is
94       * used during POM inheritance and profile injection to detect repositories that should be merged.
95       *
96       * @return a {@code String}
97       */
98      public String getId() {
99          return this.id;
100     }
101 
102     /**
103      * Human readable name of the repository.
104      *
105      * @return a {@code String}
106      */
107     public String getName() {
108         return this.name;
109     }
110 
111     /**
112      * The url of the repository, in the form {@code protocol://hostname/path}.
113      *
114      * @return a {@code String}
115      */
116     public String getUrl() {
117         return this.url;
118     }
119 
120     /**
121      * The type of layout this repository uses for locating and storing artifacts -
122      * can be {@code legacy} or {@code default}.
123      *
124      * @return a {@code String}
125      */
126     public String getLayout() {
127         return this.layout;
128     }
129 
130     /**
131      * Gets the location of the specified field in the input source.
132      */
133     public InputLocation getLocation(Object key) {
134         return locations != null ? locations.get(key) : null;
135     }
136 
137     /**
138     * Gets the keys of the locations of the input source.
139     */
140     public Set<Object> getLocationKeys() {
141         return locations != null ? locations.keySet() : null;
142     }
143 
144     /**
145      * Gets the input location that caused this model to be read.
146      */
147     public InputLocation getImportedFrom()
148     {
149         return importedFrom;
150     }
151 
152     /**
153      * Creates a new builder with this object as the basis.
154      *
155      * @return a {@code Builder}
156      */
157     @Nonnull
158     public Builder with() {
159         return newBuilder(this);
160     }
161     /**
162      * Creates a new {@code RepositoryBase} instance using the specified id.
163      *
164      * @param id the new {@code String} to use
165      * @return a {@code RepositoryBase} with the specified id
166      */
167     @Nonnull
168     public RepositoryBase withId(String id) {
169         return newBuilder(this, true).id(id).build();
170     }
171     /**
172      * Creates a new {@code RepositoryBase} instance using the specified name.
173      *
174      * @param name the new {@code String} to use
175      * @return a {@code RepositoryBase} with the specified name
176      */
177     @Nonnull
178     public RepositoryBase withName(String name) {
179         return newBuilder(this, true).name(name).build();
180     }
181     /**
182      * Creates a new {@code RepositoryBase} instance using the specified url.
183      *
184      * @param url the new {@code String} to use
185      * @return a {@code RepositoryBase} with the specified url
186      */
187     @Nonnull
188     public RepositoryBase withUrl(String url) {
189         return newBuilder(this, true).url(url).build();
190     }
191     /**
192      * Creates a new {@code RepositoryBase} instance using the specified layout.
193      *
194      * @param layout the new {@code String} to use
195      * @return a {@code RepositoryBase} with the specified layout
196      */
197     @Nonnull
198     public RepositoryBase withLayout(String layout) {
199         return newBuilder(this, true).layout(layout).build();
200     }
201 
202     /**
203      * Creates a new {@code RepositoryBase} instance.
204      * Equivalent to {@code newInstance(true)}.
205      * @see #newInstance(boolean)
206      *
207      * @return a new {@code RepositoryBase}
208      */
209     @Nonnull
210     public static RepositoryBase newInstance() {
211         return newInstance(true);
212     }
213 
214     /**
215      * Creates a new {@code RepositoryBase} instance using default values or not.
216      * Equivalent to {@code newBuilder(withDefaults).build()}.
217      *
218      * @param withDefaults the boolean indicating whether default values should be used
219      * @return a new {@code RepositoryBase}
220      */
221     @Nonnull
222     public static RepositoryBase newInstance(boolean withDefaults) {
223         return newBuilder(withDefaults).build();
224     }
225 
226     /**
227      * Creates a new {@code RepositoryBase} builder instance.
228      * Equivalent to {@code newBuilder(true)}.
229      * @see #newBuilder(boolean)
230      *
231      * @return a new {@code Builder}
232      */
233     @Nonnull
234     public static Builder newBuilder() {
235         return newBuilder(true);
236     }
237 
238     /**
239      * Creates a new {@code RepositoryBase} builder instance using default values or not.
240      *
241      * @param withDefaults the boolean indicating whether default values should be used
242      * @return a new {@code Builder}
243      */
244     @Nonnull
245     public static Builder newBuilder(boolean withDefaults) {
246         return new Builder(withDefaults);
247     }
248 
249     /**
250      * Creates a new {@code RepositoryBase} builder instance using the specified object as a basis.
251      * Equivalent to {@code newBuilder(from, false)}.
252      *
253      * @param from the {@code RepositoryBase} instance to use as a basis
254      * @return a new {@code Builder}
255      */
256     @Nonnull
257     public static Builder newBuilder(RepositoryBase from) {
258         return newBuilder(from, false);
259     }
260 
261     /**
262      * Creates a new {@code RepositoryBase} builder instance using the specified object as a basis.
263      *
264      * @param from the {@code RepositoryBase} instance to use as a basis
265      * @param forceCopy the boolean indicating if a copy should be forced
266      * @return a new {@code Builder}
267      */
268     @Nonnull
269     public static Builder newBuilder(RepositoryBase from, boolean forceCopy) {
270         return new Builder(from, forceCopy);
271     }
272 
273     /**
274      * Builder class used to create RepositoryBase instances.
275      * @see #with()
276      * @see #newBuilder()
277      */
278     @NotThreadSafe
279     public static class Builder
280     {
281         RepositoryBase base;
282         String id;
283         String name;
284         String url;
285         String layout;
286         Map<Object, InputLocation> locations;
287         InputLocation importedFrom;
288 
289         protected Builder(boolean withDefaults) {
290             if (withDefaults) {
291                 this.layout = "default";
292             }
293         }
294 
295         protected Builder(RepositoryBase base, boolean forceCopy) {
296             if (forceCopy) {
297                 this.id = base.id;
298                 this.name = base.name;
299                 this.url = base.url;
300                 this.layout = base.layout;
301                 this.locations = base.locations;
302                 this.importedFrom = base.importedFrom;
303             } else {
304                 this.base = base;
305             }
306         }
307 
308         @Nonnull
309         public Builder id(String id) {
310             this.id = id;
311             return this;
312         }
313 
314         @Nonnull
315         public Builder name(String name) {
316             this.name = name;
317             return this;
318         }
319 
320         @Nonnull
321         public Builder url(String url) {
322             this.url = url;
323             return this;
324         }
325 
326         @Nonnull
327         public Builder layout(String layout) {
328             this.layout = layout;
329             return this;
330         }
331 
332 
333         @Nonnull
334         public Builder location(Object key, InputLocation location) {
335             if (location != null) {
336                 if (!(this.locations instanceof HashMap)) {
337                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
338                 }
339                 this.locations.put(key, location);
340             }
341             return this;
342         }
343 
344         @Nonnull
345         public Builder importedFrom(InputLocation importedFrom) {
346             this.importedFrom = importedFrom;
347             return this;
348         }
349 
350         @Nonnull
351         public RepositoryBase build() {
352             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
353             if (base != null
354                     && (id == null || id == base.id)
355                     && (name == null || name == base.name)
356                     && (url == null || url == base.url)
357                     && (layout == null || layout == base.layout)
358             ) {
359                 return base;
360             }
361             return new RepositoryBase(this);
362         }
363     }
364 
365 }