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