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