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 2 and 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 2 and 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 public InputLocation getLocation(Object key) {
109 return locations.get(key);
110 }
111
112 /**
113 * Gets the keys of the locations of the input source.
114 */
115 public Set<Object> getLocationKeys() {
116 return locations.keySet();
117 }
118
119 protected Stream<Object> getLocationKeyStream() {
120 return locations.keySet().stream();
121 }
122
123 /**
124 * Gets the input location that caused this model to be read.
125 */
126 public InputLocation getImportedFrom() {
127 return importedFrom;
128 }
129
130 /**
131 * Creates a new builder with this object as the basis.
132 *
133 * @return a {@code Builder}
134 */
135 @Nonnull
136 public Builder with() {
137 return newBuilder(this);
138 }
139 /**
140 * Creates a new {@code RepositoryPolicy} instance using the specified enabled.
141 *
142 * @param enabled the new {@code String} to use
143 * @return a {@code RepositoryPolicy} with the specified enabled
144 */
145 @Nonnull
146 public RepositoryPolicy withEnabled(String enabled) {
147 return newBuilder(this, true).enabled(enabled).build();
148 }
149 /**
150 * Creates a new {@code RepositoryPolicy} instance using the specified updatePolicy.
151 *
152 * @param updatePolicy the new {@code String} to use
153 * @return a {@code RepositoryPolicy} with the specified updatePolicy
154 */
155 @Nonnull
156 public RepositoryPolicy withUpdatePolicy(String updatePolicy) {
157 return newBuilder(this, true).updatePolicy(updatePolicy).build();
158 }
159 /**
160 * Creates a new {@code RepositoryPolicy} instance using the specified checksumPolicy.
161 *
162 * @param checksumPolicy the new {@code String} to use
163 * @return a {@code RepositoryPolicy} with the specified checksumPolicy
164 */
165 @Nonnull
166 public RepositoryPolicy withChecksumPolicy(String checksumPolicy) {
167 return newBuilder(this, true).checksumPolicy(checksumPolicy).build();
168 }
169
170 /**
171 * Creates a new {@code RepositoryPolicy} instance.
172 * Equivalent to {@code newInstance(true)}.
173 * @see #newInstance(boolean)
174 *
175 * @return a new {@code RepositoryPolicy}
176 */
177 @Nonnull
178 public static RepositoryPolicy newInstance() {
179 return newInstance(true);
180 }
181
182 /**
183 * Creates a new {@code RepositoryPolicy} instance using default values or not.
184 * Equivalent to {@code newBuilder(withDefaults).build()}.
185 *
186 * @param withDefaults the boolean indicating whether default values should be used
187 * @return a new {@code RepositoryPolicy}
188 */
189 @Nonnull
190 public static RepositoryPolicy newInstance(boolean withDefaults) {
191 return newBuilder(withDefaults).build();
192 }
193
194 /**
195 * Creates a new {@code RepositoryPolicy} builder instance.
196 * Equivalent to {@code newBuilder(true)}.
197 * @see #newBuilder(boolean)
198 *
199 * @return a new {@code Builder}
200 */
201 @Nonnull
202 public static Builder newBuilder() {
203 return newBuilder(true);
204 }
205
206 /**
207 * Creates a new {@code RepositoryPolicy} builder instance using default values or not.
208 *
209 * @param withDefaults the boolean indicating whether default values should be used
210 * @return a new {@code Builder}
211 */
212 @Nonnull
213 public static Builder newBuilder(boolean withDefaults) {
214 return new Builder(withDefaults);
215 }
216
217 /**
218 * Creates a new {@code RepositoryPolicy} builder instance using the specified object as a basis.
219 * Equivalent to {@code newBuilder(from, false)}.
220 *
221 * @param from the {@code RepositoryPolicy} instance to use as a basis
222 * @return a new {@code Builder}
223 */
224 @Nonnull
225 public static Builder newBuilder(RepositoryPolicy from) {
226 return newBuilder(from, false);
227 }
228
229 /**
230 * Creates a new {@code RepositoryPolicy} builder instance using the specified object as a basis.
231 *
232 * @param from the {@code RepositoryPolicy} instance to use as a basis
233 * @param forceCopy the boolean indicating if a copy should be forced
234 * @return a new {@code Builder}
235 */
236 @Nonnull
237 public static Builder newBuilder(RepositoryPolicy from, boolean forceCopy) {
238 return new Builder(from, forceCopy);
239 }
240
241 /**
242 * Builder class used to create RepositoryPolicy instances.
243 * @see #with()
244 * @see #newBuilder()
245 */
246 @NotThreadSafe
247 public static class Builder
248 {
249 RepositoryPolicy base;
250 String enabled;
251 String updatePolicy;
252 String checksumPolicy;
253 Map<Object, InputLocation> locations;
254 InputLocation importedFrom;
255
256 protected Builder(boolean withDefaults) {
257 if (withDefaults) {
258 }
259 }
260
261 protected Builder(RepositoryPolicy base, boolean forceCopy) {
262 if (forceCopy) {
263 this.enabled = base.enabled;
264 this.updatePolicy = base.updatePolicy;
265 this.checksumPolicy = base.checksumPolicy;
266 this.locations = base.locations;
267 this.importedFrom = base.importedFrom;
268 } else {
269 this.base = base;
270 }
271 }
272
273 @Nonnull
274 public Builder enabled(String enabled) {
275 this.enabled = enabled;
276 return this;
277 }
278
279 @Nonnull
280 public Builder updatePolicy(String updatePolicy) {
281 this.updatePolicy = updatePolicy;
282 return this;
283 }
284
285 @Nonnull
286 public Builder checksumPolicy(String checksumPolicy) {
287 this.checksumPolicy = checksumPolicy;
288 return this;
289 }
290
291
292 @Nonnull
293 public Builder location(Object key, InputLocation location) {
294 if (location != null) {
295 if (!(this.locations instanceof HashMap)) {
296 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
297 }
298 this.locations.put(key, location);
299 }
300 return this;
301 }
302
303 @Nonnull
304 public Builder importedFrom(InputLocation importedFrom) {
305 this.importedFrom = importedFrom;
306 return this;
307 }
308
309 @Nonnull
310 public RepositoryPolicy build() {
311 // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
312 if (base != null
313 && (enabled == null || enabled == base.enabled)
314 && (updatePolicy == null || updatePolicy == base.updatePolicy)
315 && (checksumPolicy == null || checksumPolicy == base.checksumPolicy)
316 ) {
317 return base;
318 }
319 return new RepositoryPolicy(this);
320 }
321
322 Map<Object, InputLocation> computeLocations() {
323 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
324 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
325 if (newlocs.isEmpty()) {
326 return Map.copyOf(oldlocs);
327 }
328 if (oldlocs.isEmpty()) {
329 return Map.copyOf(newlocs);
330 }
331 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
332 // Keep value from newlocs in case of duplicates
333 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
334 }
335 }
336
337
338
339 public boolean isEnabled() {
340 return (getEnabled() != null) ? Boolean.parseBoolean(getEnabled()) : true;
341 }
342
343
344
345 }