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