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