1
2
3
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
25
26 @Experimental
27 @Generated @ThreadSafe @Immutable
28 public class RepositoryPolicy
29 implements Serializable, InputLocationTracker
30 {
31
32
33
34
35
36 final String enabled;
37
38
39
40
41
42
43
44 final String updatePolicy;
45
46
47
48
49
50
51 final String checksumPolicy;
52
53 final Map<Object, InputLocation> locations;
54
55 final InputLocation importedFrom;
56
57
58
59
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
71
72
73
74
75
76 public String getEnabled() {
77 return this.enabled;
78 }
79
80
81
82
83
84
85
86
87
88
89 public String getUpdatePolicy() {
90 return this.updatePolicy;
91 }
92
93
94
95
96
97
98
99
100
101 public String getChecksumPolicy() {
102 return this.checksumPolicy;
103 }
104
105
106
107
108 public InputLocation getLocation(Object key) {
109 return locations.get(key);
110 }
111
112
113
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
125
126 public InputLocation getImportedFrom()
127 {
128 return importedFrom;
129 }
130
131
132
133
134
135
136 @Nonnull
137 public Builder with() {
138 return newBuilder(this);
139 }
140
141
142
143
144
145
146 @Nonnull
147 public RepositoryPolicy withEnabled(String enabled) {
148 return newBuilder(this, true).enabled(enabled).build();
149 }
150
151
152
153
154
155
156 @Nonnull
157 public RepositoryPolicy withUpdatePolicy(String updatePolicy) {
158 return newBuilder(this, true).updatePolicy(updatePolicy).build();
159 }
160
161
162
163
164
165
166 @Nonnull
167 public RepositoryPolicy withChecksumPolicy(String checksumPolicy) {
168 return newBuilder(this, true).checksumPolicy(checksumPolicy).build();
169 }
170
171
172
173
174
175
176
177
178 @Nonnull
179 public static RepositoryPolicy newInstance() {
180 return newInstance(true);
181 }
182
183
184
185
186
187
188
189
190 @Nonnull
191 public static RepositoryPolicy newInstance(boolean withDefaults) {
192 return newBuilder(withDefaults).build();
193 }
194
195
196
197
198
199
200
201
202 @Nonnull
203 public static Builder newBuilder() {
204 return newBuilder(true);
205 }
206
207
208
209
210
211
212
213 @Nonnull
214 public static Builder newBuilder(boolean withDefaults) {
215 return new Builder(withDefaults);
216 }
217
218
219
220
221
222
223
224
225 @Nonnull
226 public static Builder newBuilder(RepositoryPolicy from) {
227 return newBuilder(from, false);
228 }
229
230
231
232
233
234
235
236
237 @Nonnull
238 public static Builder newBuilder(RepositoryPolicy from, boolean forceCopy) {
239 return new Builder(from, forceCopy);
240 }
241
242
243
244
245
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
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
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 }