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.Set;
12 import org.apache.maven.api.annotations.Experimental;
13 import org.apache.maven.api.annotations.Generated;
14 import org.apache.maven.api.annotations.Immutable;
15 import org.apache.maven.api.annotations.Nonnull;
16 import org.apache.maven.api.annotations.NotThreadSafe;
17 import org.apache.maven.api.annotations.ThreadSafe;
18
19
20
21
22 @Experimental
23 @Generated @ThreadSafe @Immutable
24 public class RepositoryPolicy
25 implements Serializable, InputLocationTracker
26 {
27
28
29
30
31 final boolean enabled;
32
33
34
35
36
37 final String updatePolicy;
38
39
40
41
42 final String checksumPolicy;
43
44 final Map<Object, InputLocation> locations;
45
46 final InputLocation importedFrom;
47
48
49
50
51
52 protected RepositoryPolicy(Builder builder) {
53 this.enabled = builder.enabled != null ? builder.enabled : (builder.base != null ? builder.base.enabled : true);
54 this.updatePolicy = builder.updatePolicy != null ? builder.updatePolicy : (builder.base != null ? builder.base.updatePolicy : null);
55 this.checksumPolicy = builder.checksumPolicy != null ? builder.checksumPolicy : (builder.base != null ? builder.base.checksumPolicy : null);
56 Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
57 Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
58 Map<Object, InputLocation> mutableLocations = new HashMap<>();
59 this.importedFrom = builder.importedFrom;
60 mutableLocations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
61 mutableLocations.put("enabled", newlocs.containsKey("enabled") ? newlocs.get("enabled") : oldlocs.get("enabled"));
62 mutableLocations.put("updatePolicy", newlocs.containsKey("updatePolicy") ? newlocs.get("updatePolicy") : oldlocs.get("updatePolicy"));
63 mutableLocations.put("checksumPolicy", newlocs.containsKey("checksumPolicy") ? newlocs.get("checksumPolicy") : oldlocs.get("checksumPolicy"));
64 this.locations = Collections.unmodifiableMap(mutableLocations);
65 }
66
67
68
69
70
71
72
73 public boolean isEnabled() {
74 return this.enabled;
75 }
76
77
78
79
80
81
82
83
84 public String getUpdatePolicy() {
85 return this.updatePolicy;
86 }
87
88
89
90
91
92
93
94 public String getChecksumPolicy() {
95 return this.checksumPolicy;
96 }
97
98
99
100
101 public InputLocation getLocation(Object key) {
102 return locations != null ? locations.get(key) : null;
103 }
104
105
106
107
108 public Set<Object> getLocationKeys() {
109 return locations != null ? locations.keySet() : null;
110 }
111
112
113
114
115 public InputLocation getImportedFrom()
116 {
117 return importedFrom;
118 }
119
120
121
122
123
124
125 @Nonnull
126 public Builder with() {
127 return newBuilder(this);
128 }
129
130
131
132
133
134
135 @Nonnull
136 public RepositoryPolicy withEnabled(boolean enabled) {
137 return newBuilder(this, true).enabled(enabled).build();
138 }
139
140
141
142
143
144
145 @Nonnull
146 public RepositoryPolicy withUpdatePolicy(String updatePolicy) {
147 return newBuilder(this, true).updatePolicy(updatePolicy).build();
148 }
149
150
151
152
153
154
155 @Nonnull
156 public RepositoryPolicy withChecksumPolicy(String checksumPolicy) {
157 return newBuilder(this, true).checksumPolicy(checksumPolicy).build();
158 }
159
160
161
162
163
164
165
166
167 @Nonnull
168 public static RepositoryPolicy newInstance() {
169 return newInstance(true);
170 }
171
172
173
174
175
176
177
178
179 @Nonnull
180 public static RepositoryPolicy newInstance(boolean withDefaults) {
181 return newBuilder(withDefaults).build();
182 }
183
184
185
186
187
188
189
190
191 @Nonnull
192 public static Builder newBuilder() {
193 return newBuilder(true);
194 }
195
196
197
198
199
200
201
202 @Nonnull
203 public static Builder newBuilder(boolean withDefaults) {
204 return new Builder(withDefaults);
205 }
206
207
208
209
210
211
212
213
214 @Nonnull
215 public static Builder newBuilder(RepositoryPolicy from) {
216 return newBuilder(from, false);
217 }
218
219
220
221
222
223
224
225
226 @Nonnull
227 public static Builder newBuilder(RepositoryPolicy from, boolean forceCopy) {
228 return new Builder(from, forceCopy);
229 }
230
231
232
233
234
235
236 @NotThreadSafe
237 public static class Builder
238 {
239 RepositoryPolicy base;
240 Boolean enabled;
241 String updatePolicy;
242 String checksumPolicy;
243 Map<Object, InputLocation> locations;
244 InputLocation importedFrom;
245
246 protected Builder(boolean withDefaults) {
247 if (withDefaults) {
248 this.enabled = true;
249 }
250 }
251
252 protected Builder(RepositoryPolicy base, boolean forceCopy) {
253 if (forceCopy) {
254 this.enabled = base.enabled;
255 this.updatePolicy = base.updatePolicy;
256 this.checksumPolicy = base.checksumPolicy;
257 this.locations = base.locations;
258 this.importedFrom = base.importedFrom;
259 } else {
260 this.base = base;
261 }
262 }
263
264 @Nonnull
265 public Builder enabled(boolean enabled) {
266 this.enabled = enabled;
267 return this;
268 }
269
270 @Nonnull
271 public Builder updatePolicy(String updatePolicy) {
272 this.updatePolicy = updatePolicy;
273 return this;
274 }
275
276 @Nonnull
277 public Builder checksumPolicy(String checksumPolicy) {
278 this.checksumPolicy = checksumPolicy;
279 return this;
280 }
281
282
283 @Nonnull
284 public Builder location(Object key, InputLocation location) {
285 if (location != null) {
286 if (!(this.locations instanceof HashMap)) {
287 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
288 }
289 this.locations.put(key, location);
290 }
291 return this;
292 }
293
294 @Nonnull
295 public Builder importedFrom(InputLocation importedFrom) {
296 this.importedFrom = importedFrom;
297 return this;
298 }
299
300 @Nonnull
301 public RepositoryPolicy build() {
302
303 if (base != null
304 && (enabled == null || enabled == base.enabled)
305 && (updatePolicy == null || updatePolicy == base.updatePolicy)
306 && (checksumPolicy == null || checksumPolicy == base.checksumPolicy)
307 ) {
308 return base;
309 }
310 return new RepositoryPolicy(this);
311 }
312 }
313
314 }