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