1 // =================== DO NOT EDIT THIS FILE ====================
2 // Generated by Modello Velocity from model.vm
3 // template, any modifications will be overwritten.
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 import org.apache.maven.api.xml.XmlNode;
23
24 /**
25 * Contains the configuration information of the container like Plugin.
26 */
27 @Experimental
28 @Generated @ThreadSafe @Immutable
29 public class ConfigurationContainer
30 implements Serializable, InputLocationTracker
31 {
32 /**
33 * Whether any configuration should be propagated to child POMs. Note: While the type
34 * of this field is {@code String} for technical reasons, the semantic type is actually
35 * {@code Boolean}. Default value is {@code true}.
36 */
37 final String inherited;
38 /**
39 * <p>The configuration as DOM object.</p>
40 * <p>By default, every element content is trimmed, but starting with Maven 3.1.0, you can add
41 * {@code xml:space="preserve"} to elements you want to preserve whitespace.</p>
42 * <p>You can control how child POMs inherit configuration from parent POMs by adding {@code combine.children}
43 * or {@code combine.self} attributes to the children of the configuration element:</p>
44 * <ul>
45 * <li>{@code combine.children}: available values are {@code merge} (default) and {@code append},</li>
46 * <li>{@code combine.self}: available values are {@code merge} (default) and {@code override}.</li>
47 * </ul>
48 *
49 * @see <a href="https://maven.apache.org/pom.html#Plugins">POM Reference documentation</a>
50 * @see <a href="https://codehaus-plexus.github.io/plexus-utils/apidocs/org/codehaus/plexus/util/xml/Xpp3DomUtils.html">Xpp3DomUtils</a>
51 */
52 final XmlNode configuration;
53 /** Locations */
54 final Map<Object, InputLocation> locations;
55 /** Location tracking */
56 final InputLocation importedFrom;
57
58 /**
59 * Constructor for this class, to be called from its subclasses and {@link Builder}.
60 * @see Builder#build()
61 */
62 protected ConfigurationContainer(Builder builder) {
63 this.inherited = builder.inherited != null ? builder.inherited : (builder.base != null ? builder.base.inherited : null);
64 this.configuration = builder.configuration != null ? builder.configuration : (builder.base != null ? builder.base.configuration : null);
65 this.locations = builder.computeLocations();
66 this.importedFrom = builder.importedFrom;
67 }
68
69 /**
70 * Whether any configuration should be propagated to child POMs. Note: While the type
71 * of this field is {@code String} for technical reasons, the semantic type is actually
72 * {@code Boolean}. Default value is {@code true}.
73 *
74 * @return a {@code String}
75 */
76 public String getInherited() {
77 return this.inherited;
78 }
79
80 /**
81 * <p>The configuration as DOM object.</p>
82 * <p>By default, every element content is trimmed, but starting with Maven 3.1.0, you can add
83 * {@code xml:space="preserve"} to elements you want to preserve whitespace.</p>
84 * <p>You can control how child POMs inherit configuration from parent POMs by adding {@code combine.children}
85 * or {@code combine.self} attributes to the children of the configuration element:</p>
86 * <ul>
87 * <li>{@code combine.children}: available values are {@code merge} (default) and {@code append},</li>
88 * <li>{@code combine.self}: available values are {@code merge} (default) and {@code override}.</li>
89 * </ul>
90 *
91 * @see <a href="https://maven.apache.org/pom.html#Plugins">POM Reference documentation</a>
92 * @see <a href="https://codehaus-plexus.github.io/plexus-utils/apidocs/org/codehaus/plexus/util/xml/Xpp3DomUtils.html">Xpp3DomUtils</a>
93 *
94 * @return a {@code XmlNode}
95 */
96 public XmlNode getConfiguration() {
97 return this.configuration;
98 }
99
100 /**
101 * Gets the location of the specified field in the input source.
102 */
103 public InputLocation getLocation(Object key) {
104 return locations.get(key);
105 }
106
107 /**
108 * Gets the keys of the locations of the input source.
109 */
110 public Set<Object> getLocationKeys() {
111 return locations.keySet();
112 }
113
114 protected Stream<Object> getLocationKeyStream() {
115 return locations.keySet().stream();
116 }
117
118 /**
119 * Gets the input location that caused this model to be read.
120 */
121 public InputLocation getImportedFrom()
122 {
123 return importedFrom;
124 }
125
126 /**
127 * Creates a new builder with this object as the basis.
128 *
129 * @return a {@code Builder}
130 */
131 @Nonnull
132 public Builder with() {
133 return newBuilder(this);
134 }
135 /**
136 * Creates a new {@code ConfigurationContainer} instance using the specified inherited.
137 *
138 * @param inherited the new {@code String} to use
139 * @return a {@code ConfigurationContainer} with the specified inherited
140 */
141 @Nonnull
142 public ConfigurationContainer withInherited(String inherited) {
143 return newBuilder(this, true).inherited(inherited).build();
144 }
145 /**
146 * Creates a new {@code ConfigurationContainer} instance using the specified configuration.
147 *
148 * @param configuration the new {@code XmlNode} to use
149 * @return a {@code ConfigurationContainer} with the specified configuration
150 */
151 @Nonnull
152 public ConfigurationContainer withConfiguration(XmlNode configuration) {
153 return newBuilder(this, true).configuration(configuration).build();
154 }
155
156 /**
157 * Creates a new {@code ConfigurationContainer} instance.
158 * Equivalent to {@code newInstance(true)}.
159 * @see #newInstance(boolean)
160 *
161 * @return a new {@code ConfigurationContainer}
162 */
163 @Nonnull
164 public static ConfigurationContainer newInstance() {
165 return newInstance(true);
166 }
167
168 /**
169 * Creates a new {@code ConfigurationContainer} instance using default values or not.
170 * Equivalent to {@code newBuilder(withDefaults).build()}.
171 *
172 * @param withDefaults the boolean indicating whether default values should be used
173 * @return a new {@code ConfigurationContainer}
174 */
175 @Nonnull
176 public static ConfigurationContainer newInstance(boolean withDefaults) {
177 return newBuilder(withDefaults).build();
178 }
179
180 /**
181 * Creates a new {@code ConfigurationContainer} builder instance.
182 * Equivalent to {@code newBuilder(true)}.
183 * @see #newBuilder(boolean)
184 *
185 * @return a new {@code Builder}
186 */
187 @Nonnull
188 public static Builder newBuilder() {
189 return newBuilder(true);
190 }
191
192 /**
193 * Creates a new {@code ConfigurationContainer} builder instance using default values or not.
194 *
195 * @param withDefaults the boolean indicating whether default values should be used
196 * @return a new {@code Builder}
197 */
198 @Nonnull
199 public static Builder newBuilder(boolean withDefaults) {
200 return new Builder(withDefaults);
201 }
202
203 /**
204 * Creates a new {@code ConfigurationContainer} builder instance using the specified object as a basis.
205 * Equivalent to {@code newBuilder(from, false)}.
206 *
207 * @param from the {@code ConfigurationContainer} instance to use as a basis
208 * @return a new {@code Builder}
209 */
210 @Nonnull
211 public static Builder newBuilder(ConfigurationContainer from) {
212 return newBuilder(from, false);
213 }
214
215 /**
216 * Creates a new {@code ConfigurationContainer} builder instance using the specified object as a basis.
217 *
218 * @param from the {@code ConfigurationContainer} instance to use as a basis
219 * @param forceCopy the boolean indicating if a copy should be forced
220 * @return a new {@code Builder}
221 */
222 @Nonnull
223 public static Builder newBuilder(ConfigurationContainer from, boolean forceCopy) {
224 return new Builder(from, forceCopy);
225 }
226
227 /**
228 * Builder class used to create ConfigurationContainer instances.
229 * @see #with()
230 * @see #newBuilder()
231 */
232 @NotThreadSafe
233 public static class Builder
234 {
235 ConfigurationContainer base;
236 String inherited;
237 XmlNode configuration;
238 Map<Object, InputLocation> locations;
239 InputLocation importedFrom;
240
241 protected Builder(boolean withDefaults) {
242 if (withDefaults) {
243 }
244 }
245
246 protected Builder(ConfigurationContainer base, boolean forceCopy) {
247 if (forceCopy) {
248 this.inherited = base.inherited;
249 this.configuration = base.configuration;
250 this.locations = base.locations;
251 this.importedFrom = base.importedFrom;
252 } else {
253 this.base = base;
254 }
255 }
256
257 @Nonnull
258 public Builder inherited(String inherited) {
259 this.inherited = inherited;
260 return this;
261 }
262
263 @Nonnull
264 public Builder configuration(XmlNode configuration) {
265 this.configuration = configuration;
266 return this;
267 }
268
269
270 @Nonnull
271 public Builder location(Object key, InputLocation location) {
272 if (location != null) {
273 if (!(this.locations instanceof HashMap)) {
274 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
275 }
276 this.locations.put(key, location);
277 }
278 return this;
279 }
280
281 @Nonnull
282 public Builder importedFrom(InputLocation importedFrom) {
283 this.importedFrom = importedFrom;
284 return this;
285 }
286
287 @Nonnull
288 public ConfigurationContainer build() {
289 // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
290 if (base != null
291 && (inherited == null || inherited == base.inherited)
292 && (configuration == null || configuration == base.configuration)
293 ) {
294 return base;
295 }
296 return new ConfigurationContainer(this);
297 }
298
299 Map<Object, InputLocation> computeLocations() {
300 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
301 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
302 if (newlocs.isEmpty()) {
303 return Map.copyOf(oldlocs);
304 }
305 if (oldlocs.isEmpty()) {
306 return Map.copyOf(newlocs);
307 }
308 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
309 // Keep value from newlocs in case of duplicates
310 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
311 }
312 }
313
314
315
316 public boolean isInherited() {
317 return ( getInherited() != null ) ? Boolean.parseBoolean( getInherited() ) : true;
318 }
319
320
321
322 }