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