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