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