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