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