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         return importedFrom;
148     }
149 
150     /**
151      * Creates a new builder with this object as the basis.
152      *
153      * @return a {@code Builder}
154      */
155     @Nonnull
156     public Builder with() {
157         return newBuilder(this);
158     }
159     /**
160      * Creates a new {@code RepositoryBase} instance using the specified id.
161      *
162      * @param id the new {@code String} to use
163      * @return a {@code RepositoryBase} with the specified id
164      */
165     @Nonnull
166     public RepositoryBase withId(String id) {
167         return newBuilder(this, true).id(id).build();
168     }
169     /**
170      * Creates a new {@code RepositoryBase} instance using the specified name.
171      *
172      * @param name the new {@code String} to use
173      * @return a {@code RepositoryBase} with the specified name
174      */
175     @Nonnull
176     public RepositoryBase withName(String name) {
177         return newBuilder(this, true).name(name).build();
178     }
179     /**
180      * Creates a new {@code RepositoryBase} instance using the specified url.
181      *
182      * @param url the new {@code String} to use
183      * @return a {@code RepositoryBase} with the specified url
184      */
185     @Nonnull
186     public RepositoryBase withUrl(String url) {
187         return newBuilder(this, true).url(url).build();
188     }
189     /**
190      * Creates a new {@code RepositoryBase} instance using the specified layout.
191      *
192      * @param layout the new {@code String} to use
193      * @return a {@code RepositoryBase} with the specified layout
194      */
195     @Nonnull
196     public RepositoryBase withLayout(String layout) {
197         return newBuilder(this, true).layout(layout).build();
198     }
199 
200     /**
201      * Creates a new {@code RepositoryBase} instance.
202      * Equivalent to {@code newInstance(true)}.
203      * @see #newInstance(boolean)
204      *
205      * @return a new {@code RepositoryBase}
206      */
207     @Nonnull
208     public static RepositoryBase newInstance() {
209         return newInstance(true);
210     }
211 
212     /**
213      * Creates a new {@code RepositoryBase} instance using default values or not.
214      * Equivalent to {@code newBuilder(withDefaults).build()}.
215      *
216      * @param withDefaults the boolean indicating whether default values should be used
217      * @return a new {@code RepositoryBase}
218      */
219     @Nonnull
220     public static RepositoryBase newInstance(boolean withDefaults) {
221         return newBuilder(withDefaults).build();
222     }
223 
224     /**
225      * Creates a new {@code RepositoryBase} builder instance.
226      * Equivalent to {@code newBuilder(true)}.
227      * @see #newBuilder(boolean)
228      *
229      * @return a new {@code Builder}
230      */
231     @Nonnull
232     public static Builder newBuilder() {
233         return newBuilder(true);
234     }
235 
236     /**
237      * Creates a new {@code RepositoryBase} builder instance using default values or not.
238      *
239      * @param withDefaults the boolean indicating whether default values should be used
240      * @return a new {@code Builder}
241      */
242     @Nonnull
243     public static Builder newBuilder(boolean withDefaults) {
244         return new Builder(withDefaults);
245     }
246 
247     /**
248      * Creates a new {@code RepositoryBase} builder instance using the specified object as a basis.
249      * Equivalent to {@code newBuilder(from, false)}.
250      *
251      * @param from the {@code RepositoryBase} instance to use as a basis
252      * @return a new {@code Builder}
253      */
254     @Nonnull
255     public static Builder newBuilder(RepositoryBase from) {
256         return newBuilder(from, false);
257     }
258 
259     /**
260      * Creates a new {@code RepositoryBase} builder instance using the specified object as a basis.
261      *
262      * @param from the {@code RepositoryBase} instance to use as a basis
263      * @param forceCopy the boolean indicating if a copy should be forced
264      * @return a new {@code Builder}
265      */
266     @Nonnull
267     public static Builder newBuilder(RepositoryBase from, boolean forceCopy) {
268         return new Builder(from, forceCopy);
269     }
270 
271     /**
272      * Builder class used to create RepositoryBase instances.
273      * @see #with()
274      * @see #newBuilder()
275      */
276     @NotThreadSafe
277     public static class Builder
278     {
279         RepositoryBase base;
280         String id;
281         String name;
282         String url;
283         String layout;
284         Map<Object, InputLocation> locations;
285         InputLocation importedFrom;
286 
287         protected Builder(boolean withDefaults) {
288             if (withDefaults) {
289                 this.layout = "default";
290             }
291         }
292 
293         protected Builder(RepositoryBase base, boolean forceCopy) {
294             if (forceCopy) {
295                 this.id = base.id;
296                 this.name = base.name;
297                 this.url = base.url;
298                 this.layout = base.layout;
299                 this.locations = base.locations;
300                 this.importedFrom = base.importedFrom;
301             } else {
302                 this.base = base;
303             }
304         }
305 
306         @Nonnull
307         public Builder id(String id) {
308             this.id = id;
309             return this;
310         }
311 
312         @Nonnull
313         public Builder name(String name) {
314             this.name = name;
315             return this;
316         }
317 
318         @Nonnull
319         public Builder url(String url) {
320             this.url = url;
321             return this;
322         }
323 
324         @Nonnull
325         public Builder layout(String layout) {
326             this.layout = layout;
327             return this;
328         }
329 
330 
331         @Nonnull
332         public Builder location(Object key, InputLocation location) {
333             if (location != null) {
334                 if (!(this.locations instanceof HashMap)) {
335                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
336                 }
337                 this.locations.put(key, location);
338             }
339             return this;
340         }
341 
342         @Nonnull
343         public Builder importedFrom(InputLocation importedFrom) {
344             this.importedFrom = importedFrom;
345             return this;
346         }
347 
348         @Nonnull
349         public RepositoryBase build() {
350             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
351             if (base != null
352                     && (id == null || id == base.id)
353                     && (name == null || name == base.name)
354                     && (url == null || url == base.url)
355                     && (layout == null || layout == base.layout)
356             ) {
357                 return base;
358             }
359             return new RepositoryBase(this);
360         }
361 
362         Map<Object, InputLocation> computeLocations() {
363             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
364             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
365             if (newlocs.isEmpty()) {
366                 return Map.copyOf(oldlocs);
367             }
368             if (oldlocs.isEmpty()) {
369                 return Map.copyOf(newlocs);
370             }
371             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
372                     // Keep value from newlocs in case of duplicates
373                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
374         }
375     }
376 
377 }