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 public InputLocation getLocation(Object key) {
79 return locations.get(key);
80 }
81
82
83
84
85 public Set<Object> getLocationKeys() {
86 return locations.keySet();
87 }
88
89 protected Stream<Object> getLocationKeyStream() {
90 return locations.keySet().stream();
91 }
92
93
94
95
96 public InputLocation getImportedFrom()
97 {
98 return importedFrom;
99 }
100
101
102
103
104
105
106 @Nonnull
107 public Builder with() {
108 return newBuilder(this);
109 }
110
111
112
113
114
115
116 @Nonnull
117 public ActivationProperty withName(String name) {
118 return newBuilder(this, true).name(name).build();
119 }
120
121
122
123
124
125
126 @Nonnull
127 public ActivationProperty withValue(String value) {
128 return newBuilder(this, true).value(value).build();
129 }
130
131
132
133
134
135
136
137
138 @Nonnull
139 public static ActivationProperty newInstance() {
140 return newInstance(true);
141 }
142
143
144
145
146
147
148
149
150 @Nonnull
151 public static ActivationProperty newInstance(boolean withDefaults) {
152 return newBuilder(withDefaults).build();
153 }
154
155
156
157
158
159
160
161
162 @Nonnull
163 public static Builder newBuilder() {
164 return newBuilder(true);
165 }
166
167
168
169
170
171
172
173 @Nonnull
174 public static Builder newBuilder(boolean withDefaults) {
175 return new Builder(withDefaults);
176 }
177
178
179
180
181
182
183
184
185 @Nonnull
186 public static Builder newBuilder(ActivationProperty from) {
187 return newBuilder(from, false);
188 }
189
190
191
192
193
194
195
196
197 @Nonnull
198 public static Builder newBuilder(ActivationProperty from, boolean forceCopy) {
199 return new Builder(from, forceCopy);
200 }
201
202
203
204
205
206
207 @NotThreadSafe
208 public static class Builder
209 {
210 ActivationProperty base;
211 String name;
212 String value;
213 Map<Object, InputLocation> locations;
214 InputLocation importedFrom;
215
216 protected Builder(boolean withDefaults) {
217 if (withDefaults) {
218 }
219 }
220
221 protected Builder(ActivationProperty base, boolean forceCopy) {
222 if (forceCopy) {
223 this.name = base.name;
224 this.value = base.value;
225 this.locations = base.locations;
226 this.importedFrom = base.importedFrom;
227 } else {
228 this.base = base;
229 }
230 }
231
232 @Nonnull
233 public Builder name(String name) {
234 this.name = name;
235 return this;
236 }
237
238 @Nonnull
239 public Builder value(String value) {
240 this.value = value;
241 return this;
242 }
243
244
245 @Nonnull
246 public Builder location(Object key, InputLocation location) {
247 if (location != null) {
248 if (!(this.locations instanceof HashMap)) {
249 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
250 }
251 this.locations.put(key, location);
252 }
253 return this;
254 }
255
256 @Nonnull
257 public Builder importedFrom(InputLocation importedFrom) {
258 this.importedFrom = importedFrom;
259 return this;
260 }
261
262 @Nonnull
263 public ActivationProperty build() {
264
265 if (base != null
266 && (name == null || name == base.name)
267 && (value == null || value == base.value)
268 ) {
269 return base;
270 }
271 return new ActivationProperty(this);
272 }
273
274 Map<Object, InputLocation> computeLocations() {
275 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
276 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
277 if (newlocs.isEmpty()) {
278 return Map.copyOf(oldlocs);
279 }
280 if (oldlocs.isEmpty()) {
281 return Map.copyOf(newlocs);
282 }
283 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
284
285 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
286 }
287 }
288
289 }