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