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.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15 import org.apache.maven.api.annotations.Experimental;
16 import org.apache.maven.api.annotations.Generated;
17 import org.apache.maven.api.annotations.Immutable;
18 import org.apache.maven.api.annotations.Nonnull;
19 import org.apache.maven.api.annotations.NotThreadSafe;
20 import org.apache.maven.api.annotations.ThreadSafe;
21
22 /**
23 * This element describes all of the classpath resources associated with a project
24 * or unit tests.
25 */
26 @Experimental
27 @Generated @ThreadSafe @Immutable
28 public class Resource
29 extends FileSet
30 implements Serializable, InputLocationTracker
31 {
32 /**
33 * Describe the resource target path. The path is relative to the target/classes
34 * directory (i.e. {@code ${project.build.outputDirectory}}).
35 * For example, if you want that resource to appear in a specific package
36 * ({@code org.apache.maven.messages}), you must specify this
37 * element with this value: {@code org/apache/maven/messages}.
38 * This is not required if you simply put the resources in that directory
39 * structure at the source, however.
40 */
41 final String targetPath;
42 /**
43 * Whether resources are filtered to replace tokens with parameterised values or not.
44 * The values are taken from the {@code properties} element and from the
45 * properties in the files listed in the {@code filters} element. Note: While the type
46 * of this field is {@code String} for technical reasons, the semantic type is actually
47 * {@code Boolean}. Default value is {@code false}.
48 */
49 final String filtering;
50 /**
51 * FOR INTERNAL USE ONLY. This is a unique identifier assigned to each
52 * resource to allow Maven to merge changes to this resource that take
53 * place during the execution of a plugin. This field must be managed
54 * by the generated parser and formatter classes in order to allow it
55 * to survive model interpolation.
56 */
57 final String mergeId;
58 /** Locations (this potentially hides the same name field from the super class) */
59 final Map<Object, InputLocation> locations;
60
61 /**
62 * Constructor for this class, to be called from its subclasses and {@link Builder}.
63 * @see Builder#build()
64 */
65 protected Resource(Builder builder) {
66 super(builder);
67 this.targetPath = builder.targetPath != null ? builder.targetPath : (builder.base != null ? builder.base.targetPath : null);
68 this.filtering = builder.filtering != null ? builder.filtering : (builder.base != null ? builder.base.filtering : null);
69 this.mergeId = builder.mergeId != null ? builder.mergeId : (builder.base != null ? builder.base.mergeId : null);
70 Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
71 Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
72 Map<Object, InputLocation> mutableLocations = new HashMap<>(super.locations);
73 mutableLocations.put("targetPath", newlocs.containsKey("targetPath") ? newlocs.get("targetPath") : oldlocs.get("targetPath"));
74 mutableLocations.put("filtering", newlocs.containsKey("filtering") ? newlocs.get("filtering") : oldlocs.get("filtering"));
75 mutableLocations.put("mergeId", newlocs.containsKey("mergeId") ? newlocs.get("mergeId") : oldlocs.get("mergeId"));
76 this.locations = Collections.unmodifiableMap(mutableLocations);
77 }
78
79 /**
80 * Describe the resource target path. The path is relative to the target/classes
81 * directory (i.e. {@code ${project.build.outputDirectory}}).
82 * For example, if you want that resource to appear in a specific package
83 * ({@code org.apache.maven.messages}), you must specify this
84 * element with this value: {@code org/apache/maven/messages}.
85 * This is not required if you simply put the resources in that directory
86 * structure at the source, however.
87 *
88 * @return a {@code String}
89 */
90 public String getTargetPath() {
91 return this.targetPath;
92 }
93
94 /**
95 * Whether resources are filtered to replace tokens with parameterised values or not.
96 * The values are taken from the {@code properties} element and from the
97 * properties in the files listed in the {@code filters} element. Note: While the type
98 * of this field is {@code String} for technical reasons, the semantic type is actually
99 * {@code Boolean}. Default value is {@code false}.
100 *
101 * @return a {@code String}
102 */
103 public String getFiltering() {
104 return this.filtering;
105 }
106
107 /**
108 * FOR INTERNAL USE ONLY. This is a unique identifier assigned to each
109 * resource to allow Maven to merge changes to this resource that take
110 * place during the execution of a plugin. This field must be managed
111 * by the generated parser and formatter classes in order to allow it
112 * to survive model interpolation.
113 *
114 * @return a {@code String}
115 */
116 public String getMergeId() {
117 return this.mergeId;
118 }
119
120 /**
121 * Gets the location of the specified field in the input source.
122 */
123 public InputLocation getLocation(Object key) {
124 return locations != null ? locations.get(key) : null;
125 }
126
127 /**
128 * Gets the keys of the locations of the input source.
129 */
130 public Set<Object> getLocationKeys() {
131 return locations != null ? locations.keySet() : null;
132 }
133
134 /**
135 * Creates a new builder with this object as the basis.
136 *
137 * @return a {@code Builder}
138 */
139 @Nonnull
140 public Builder with() {
141 return newBuilder(this);
142 }
143 /**
144 * Creates a new {@code Resource} instance using the specified includes.
145 *
146 * @param includes the new {@code Collection<String>} to use
147 * @return a {@code Resource} with the specified includes
148 */
149 @Nonnull
150 public Resource withIncludes(Collection<String> includes) {
151 return newBuilder(this, true).includes(includes).build();
152 }
153 /**
154 * Creates a new {@code Resource} instance using the specified excludes.
155 *
156 * @param excludes the new {@code Collection<String>} to use
157 * @return a {@code Resource} with the specified excludes
158 */
159 @Nonnull
160 public Resource withExcludes(Collection<String> excludes) {
161 return newBuilder(this, true).excludes(excludes).build();
162 }
163 /**
164 * Creates a new {@code Resource} instance using the specified directory.
165 *
166 * @param directory the new {@code String} to use
167 * @return a {@code Resource} with the specified directory
168 */
169 @Nonnull
170 public Resource withDirectory(String directory) {
171 return newBuilder(this, true).directory(directory).build();
172 }
173 /**
174 * Creates a new {@code Resource} instance using the specified targetPath.
175 *
176 * @param targetPath the new {@code String} to use
177 * @return a {@code Resource} with the specified targetPath
178 */
179 @Nonnull
180 public Resource withTargetPath(String targetPath) {
181 return newBuilder(this, true).targetPath(targetPath).build();
182 }
183 /**
184 * Creates a new {@code Resource} instance using the specified filtering.
185 *
186 * @param filtering the new {@code String} to use
187 * @return a {@code Resource} with the specified filtering
188 */
189 @Nonnull
190 public Resource withFiltering(String filtering) {
191 return newBuilder(this, true).filtering(filtering).build();
192 }
193 /**
194 * Creates a new {@code Resource} instance using the specified mergeId.
195 *
196 * @param mergeId the new {@code String} to use
197 * @return a {@code Resource} with the specified mergeId
198 */
199 @Nonnull
200 public Resource withMergeId(String mergeId) {
201 return newBuilder(this, true).mergeId(mergeId).build();
202 }
203
204 /**
205 * Creates a new {@code Resource} instance.
206 * Equivalent to {@code newInstance(true)}.
207 * @see #newInstance(boolean)
208 *
209 * @return a new {@code Resource}
210 */
211 @Nonnull
212 public static Resource newInstance() {
213 return newInstance(true);
214 }
215
216 /**
217 * Creates a new {@code Resource} instance using default values or not.
218 * Equivalent to {@code newBuilder(withDefaults).build()}.
219 *
220 * @param withDefaults the boolean indicating whether default values should be used
221 * @return a new {@code Resource}
222 */
223 @Nonnull
224 public static Resource newInstance(boolean withDefaults) {
225 return newBuilder(withDefaults).build();
226 }
227
228 /**
229 * Creates a new {@code Resource} builder instance.
230 * Equivalent to {@code newBuilder(true)}.
231 * @see #newBuilder(boolean)
232 *
233 * @return a new {@code Builder}
234 */
235 @Nonnull
236 public static Builder newBuilder() {
237 return newBuilder(true);
238 }
239
240 /**
241 * Creates a new {@code Resource} builder instance using default values or not.
242 *
243 * @param withDefaults the boolean indicating whether default values should be used
244 * @return a new {@code Builder}
245 */
246 @Nonnull
247 public static Builder newBuilder(boolean withDefaults) {
248 return new Builder(withDefaults);
249 }
250
251 /**
252 * Creates a new {@code Resource} builder instance using the specified object as a basis.
253 * Equivalent to {@code newBuilder(from, false)}.
254 *
255 * @param from the {@code Resource} instance to use as a basis
256 * @return a new {@code Builder}
257 */
258 @Nonnull
259 public static Builder newBuilder(Resource from) {
260 return newBuilder(from, false);
261 }
262
263 /**
264 * Creates a new {@code Resource} builder instance using the specified object as a basis.
265 *
266 * @param from the {@code Resource} instance to use as a basis
267 * @param forceCopy the boolean indicating if a copy should be forced
268 * @return a new {@code Builder}
269 */
270 @Nonnull
271 public static Builder newBuilder(Resource from, boolean forceCopy) {
272 return new Builder(from, forceCopy);
273 }
274
275 /**
276 * Builder class used to create Resource instances.
277 * @see #with()
278 * @see #newBuilder()
279 */
280 @NotThreadSafe
281 public static class Builder
282 extends FileSet.Builder
283 {
284 Resource base;
285 String targetPath;
286 String filtering;
287 String mergeId;
288
289 protected Builder(boolean withDefaults) {
290 super(withDefaults);
291 if (withDefaults) {
292 }
293 }
294
295 protected Builder(Resource base, boolean forceCopy) {
296 super(base, forceCopy);
297 if (forceCopy) {
298 this.targetPath = base.targetPath;
299 this.filtering = base.filtering;
300 this.mergeId = base.mergeId;
301 this.locations = base.locations;
302 this.importedFrom = base.importedFrom;
303 } else {
304 this.base = base;
305 }
306 }
307
308 @Nonnull
309 public Builder includes(Collection<String> includes) {
310 this.includes = includes;
311 return this;
312 }
313
314 @Nonnull
315 public Builder excludes(Collection<String> excludes) {
316 this.excludes = excludes;
317 return this;
318 }
319
320 @Nonnull
321 public Builder directory(String directory) {
322 this.directory = directory;
323 return this;
324 }
325
326 @Nonnull
327 public Builder targetPath(String targetPath) {
328 this.targetPath = targetPath;
329 return this;
330 }
331
332 @Nonnull
333 public Builder filtering(String filtering) {
334 this.filtering = filtering;
335 return this;
336 }
337
338 @Nonnull
339 public Builder mergeId(String mergeId) {
340 this.mergeId = mergeId;
341 return this;
342 }
343
344
345 @Nonnull
346 public Builder location(Object key, InputLocation location) {
347 if (location != null) {
348 if (!(this.locations instanceof HashMap)) {
349 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
350 }
351 this.locations.put(key, location);
352 }
353 return this;
354 }
355
356 @Nonnull
357 public Builder importedFrom(InputLocation importedFrom) {
358 this.importedFrom = importedFrom;
359 return this;
360 }
361
362 @Nonnull
363 public Resource build() {
364 // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
365 if (base != null
366 && (includes == null || includes == base.includes)
367 && (excludes == null || excludes == base.excludes)
368 && (directory == null || directory == base.directory)
369 && (targetPath == null || targetPath == base.targetPath)
370 && (filtering == null || filtering == base.filtering)
371 && (mergeId == null || mergeId == base.mergeId)
372 ) {
373 return base;
374 }
375 return new Resource(this);
376 }
377 }
378
379
380
381 public boolean isFiltering() {
382 return (getFiltering() != null) ? Boolean.parseBoolean(getFiltering()) : false;
383 }
384
385
386
387
388 /**
389 * @see java.lang.Object#toString()
390 */
391 public String toString() {
392 return "Resource {targetPath: " + getTargetPath() + ", filtering: " + isFiltering() + ", " + super.toString() + "}";
393 }
394
395
396 }