View Javadoc
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.Objects;
15  import java.util.Optional;
16  import java.util.Set;
17  import java.util.stream.Collectors;
18  import java.util.stream.Stream;
19  import org.apache.maven.api.annotations.Experimental;
20  import org.apache.maven.api.annotations.Generated;
21  import org.apache.maven.api.annotations.Immutable;
22  import org.apache.maven.api.annotations.Nonnull;
23  import org.apache.maven.api.annotations.NotThreadSafe;
24  import org.apache.maven.api.annotations.ThreadSafe;
25  
26  /**
27   * Definition of include or exclude patterns.
28   */
29  @Experimental
30  @Generated @ThreadSafe @Immutable
31  public class PatternSet
32      implements Serializable, InputLocationTracker
33  {
34      /**
35       * A list of patterns to include, e.g. {@code **/*.xml}.
36       */
37      final List<String> includes;
38      /**
39       * A list of patterns to exclude, e.g. {@code **&#47;*.xml}
40       */
41      final List<String> excludes;
42      /** Locations */
43      final Map<Object, InputLocation> locations;
44      /** Location tracking */
45      final InputLocation importedFrom;
46  
47      /**
48        * Constructor for this class, to be called from its subclasses and {@link Builder}.
49        * @see Builder#build()
50        */
51      protected PatternSet(Builder builder) {
52          this.includes = ImmutableCollections.copy(builder.includes != null ? builder.includes : (builder.base != null ? builder.base.includes : null));
53          this.excludes = ImmutableCollections.copy(builder.excludes != null ? builder.excludes : (builder.base != null ? builder.base.excludes : null));
54          this.locations = builder.computeLocations();
55          this.importedFrom = builder.importedFrom;
56      }
57  
58      /**
59       * A list of patterns to include, e.g. {@code **&#47;*.xml}.
60       *
61       * @return a {@code List<String>}
62       */
63      @Nonnull
64      public List<String> getIncludes() {
65          return this.includes;
66      }
67  
68      /**
69       * A list of patterns to exclude, e.g. {@code **&#47;*.xml}
70       *
71       * @return a {@code List<String>}
72       */
73      @Nonnull
74      public List<String> getExcludes() {
75          return this.excludes;
76      }
77  
78      /**
79       * Gets the location of the specified field in the input source.
80       */
81      public InputLocation getLocation(Object key) {
82          return locations.get(key);
83      }
84  
85      /**
86       * Gets the keys of the locations of the input source.
87       */
88      public Set<Object> getLocationKeys() {
89          return locations.keySet();
90      }
91  
92      protected Stream<Object> getLocationKeyStream() {
93          return locations.keySet().stream();
94      }
95  
96      /**
97       * Gets the input location that caused this model to be read.
98       */
99      public InputLocation getImportedFrom()
100     {
101         return importedFrom;
102     }
103 
104     /**
105      * Creates a new builder with this object as the basis.
106      *
107      * @return a {@code Builder}
108      */
109     @Nonnull
110     public Builder with() {
111         return newBuilder(this);
112     }
113     /**
114      * Creates a new {@code PatternSet} instance using the specified includes.
115      *
116      * @param includes the new {@code Collection<String>} to use
117      * @return a {@code PatternSet} with the specified includes
118      */
119     @Nonnull
120     public PatternSet withIncludes(Collection<String> includes) {
121         return newBuilder(this, true).includes(includes).build();
122     }
123     /**
124      * Creates a new {@code PatternSet} instance using the specified excludes.
125      *
126      * @param excludes the new {@code Collection<String>} to use
127      * @return a {@code PatternSet} with the specified excludes
128      */
129     @Nonnull
130     public PatternSet withExcludes(Collection<String> excludes) {
131         return newBuilder(this, true).excludes(excludes).build();
132     }
133 
134     /**
135      * Creates a new {@code PatternSet} instance.
136      * Equivalent to {@code newInstance(true)}.
137      * @see #newInstance(boolean)
138      *
139      * @return a new {@code PatternSet}
140      */
141     @Nonnull
142     public static PatternSet newInstance() {
143         return newInstance(true);
144     }
145 
146     /**
147      * Creates a new {@code PatternSet} instance using default values or not.
148      * Equivalent to {@code newBuilder(withDefaults).build()}.
149      *
150      * @param withDefaults the boolean indicating whether default values should be used
151      * @return a new {@code PatternSet}
152      */
153     @Nonnull
154     public static PatternSet newInstance(boolean withDefaults) {
155         return newBuilder(withDefaults).build();
156     }
157 
158     /**
159      * Creates a new {@code PatternSet} builder instance.
160      * Equivalent to {@code newBuilder(true)}.
161      * @see #newBuilder(boolean)
162      *
163      * @return a new {@code Builder}
164      */
165     @Nonnull
166     public static Builder newBuilder() {
167         return newBuilder(true);
168     }
169 
170     /**
171      * Creates a new {@code PatternSet} builder instance using default values or not.
172      *
173      * @param withDefaults the boolean indicating whether default values should be used
174      * @return a new {@code Builder}
175      */
176     @Nonnull
177     public static Builder newBuilder(boolean withDefaults) {
178         return new Builder(withDefaults);
179     }
180 
181     /**
182      * Creates a new {@code PatternSet} builder instance using the specified object as a basis.
183      * Equivalent to {@code newBuilder(from, false)}.
184      *
185      * @param from the {@code PatternSet} instance to use as a basis
186      * @return a new {@code Builder}
187      */
188     @Nonnull
189     public static Builder newBuilder(PatternSet from) {
190         return newBuilder(from, false);
191     }
192 
193     /**
194      * Creates a new {@code PatternSet} builder instance using the specified object as a basis.
195      *
196      * @param from the {@code PatternSet} instance to use as a basis
197      * @param forceCopy the boolean indicating if a copy should be forced
198      * @return a new {@code Builder}
199      */
200     @Nonnull
201     public static Builder newBuilder(PatternSet from, boolean forceCopy) {
202         return new Builder(from, forceCopy);
203     }
204 
205     /**
206      * Builder class used to create PatternSet instances.
207      * @see #with()
208      * @see #newBuilder()
209      */
210     @NotThreadSafe
211     public static class Builder
212     {
213         PatternSet base;
214         Collection<String> includes;
215         Collection<String> excludes;
216         Map<Object, InputLocation> locations;
217         InputLocation importedFrom;
218 
219         protected Builder(boolean withDefaults) {
220             if (withDefaults) {
221             }
222         }
223 
224         protected Builder(PatternSet base, boolean forceCopy) {
225             if (forceCopy) {
226                 this.includes = base.includes;
227                 this.excludes = base.excludes;
228                 this.locations = base.locations;
229                 this.importedFrom = base.importedFrom;
230             } else {
231                 this.base = base;
232             }
233         }
234 
235         @Nonnull
236         public Builder includes(Collection<String> includes) {
237             this.includes = includes;
238             return this;
239         }
240 
241         @Nonnull
242         public Builder excludes(Collection<String> excludes) {
243             this.excludes = excludes;
244             return this;
245         }
246 
247 
248         @Nonnull
249         public Builder location(Object key, InputLocation location) {
250             if (location != null) {
251                 if (!(this.locations instanceof HashMap)) {
252                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
253                 }
254                 this.locations.put(key, location);
255             }
256             return this;
257         }
258 
259         @Nonnull
260         public Builder importedFrom(InputLocation importedFrom) {
261             this.importedFrom = importedFrom;
262             return this;
263         }
264 
265         @Nonnull
266         public PatternSet build() {
267             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
268             if (base != null
269                     && (includes == null || includes == base.includes)
270                     && (excludes == null || excludes == base.excludes)
271             ) {
272                 return base;
273             }
274             return new PatternSet(this);
275         }
276 
277         Map<Object, InputLocation> computeLocations() {
278             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
279             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
280             if (newlocs.isEmpty()) {
281                 return Map.copyOf(oldlocs);
282             }
283             if (oldlocs.isEmpty()) {
284                 return Map.copyOf(newlocs);
285             }
286             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
287                     // Keep value from newlocs in case of duplicates
288                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
289         }
290     }
291 
292 
293             
294     /**
295      * @see java.lang.Object#toString()
296      */
297     public String toString() {
298         return "PatternSet [" +
299             "includes: {" + getIncludes().stream().collect(java.util.stream.Collectors.joining(", ")) + "}, " +
300             "excludes: {" + getExcludes().stream().collect(java.util.stream.Collectors.joining(", ")) + "}]";
301     }
302             
303           
304 }