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