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
27
28 @Experimental
29 @Generated @ThreadSafe @Immutable
30 public class ActivationProperty
31 implements Serializable, InputLocationTracker
32 {
33
34
35
36 final String name;
37
38
39
40 final String value;
41
42 final Map<Object, InputLocation> locations;
43
44 final InputLocation importedFrom;
45
46
47
48
49
50 protected ActivationProperty(Builder builder) {
51 this.name = builder.name != null ? builder.name : (builder.base != null ? builder.base.name : null);
52 this.value = builder.value != null ? builder.value : (builder.base != null ? builder.base.value : null);
53 this.locations = builder.computeLocations();
54 this.importedFrom = builder.importedFrom;
55 }
56
57
58
59
60
61
62 public String getName() {
63 return this.name;
64 }
65
66
67
68
69
70
71 public String getValue() {
72 return this.value;
73 }
74
75
76
77
78
79
80
81
82 public InputLocation getLocation(Object key) {
83 Objects.requireNonNull(key, "key");
84 return locations.get(key);
85 }
86
87
88
89
90 public Set<Object> getLocationKeys() {
91 return locations.keySet();
92 }
93
94 protected Stream<Object> getLocationKeyStream() {
95 return locations.keySet().stream();
96 }
97
98
99
100
101 public InputLocation getImportedFrom() {
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 ActivationProperty withName(String name) {
122 return newBuilder(this, true).name(name).build();
123 }
124
125
126
127
128
129
130 @Nonnull
131 public ActivationProperty withValue(String value) {
132 return newBuilder(this, true).value(value).build();
133 }
134
135
136
137
138
139
140
141
142 @Nonnull
143 public static ActivationProperty newInstance() {
144 return newInstance(true);
145 }
146
147
148
149
150
151
152
153
154 @Nonnull
155 public static ActivationProperty 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(ActivationProperty from) {
191 return newBuilder(from, false);
192 }
193
194
195
196
197
198
199
200
201 @Nonnull
202 public static Builder newBuilder(ActivationProperty 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 ActivationProperty base;
215 String name;
216 String value;
217 Map<Object, InputLocation> locations;
218 InputLocation importedFrom;
219
220 protected Builder(boolean withDefaults) {
221 if (withDefaults) {
222 }
223 }
224
225 protected Builder(ActivationProperty base, boolean forceCopy) {
226 if (forceCopy) {
227 this.name = base.name;
228 this.value = base.value;
229 this.locations = base.locations;
230 this.importedFrom = base.importedFrom;
231 } else {
232 this.base = base;
233 }
234 }
235
236 @Nonnull
237 public Builder name(String name) {
238 this.name = name;
239 return this;
240 }
241
242 @Nonnull
243 public Builder value(String value) {
244 this.value = value;
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 ActivationProperty build() {
268
269 if (base != null
270 && (name == null || name == base.name)
271 && (value == null || value == base.value)
272 ) {
273 return base;
274 }
275 return new ActivationProperty(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 }