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