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